我正在尝试使用 matplotlib 的 histogram2d 绘制一些 2D 经验概率分布。我希望颜色在几个不同的图中具有相同的比例,但即使我知道结果分布的全局上限和下限,也找不到设置比例的方法。按原样,每个色标将从直方图箱的最小高度到最大高度运行,但每个图的这个范围都会不同。
一种可能的解决方案是强制一个容器采用我的下限高度,另一个容器采用我的上限高度。即使这看起来也不是一项非常简单的任务。
最佳答案
一般来说,matplotlib 中大多数内容的颜色缩放都是由 vmin
和 vmax
关键字参数控制的。
您必须稍微阅读一下字里行间的内容,但正如文档提到的,hist2d
中的其他 kwargs 会传递给 pcolorfast
。因此,您可以通过 vmin
和 vmax
kwargs 指定颜色限制。
例如:
import numpy as np
import matplotlib.pyplot as plt
small_data = np.random.random((2, 10))
large_data = np.random.random((2, 100))
fig, axes = plt.subplots(ncols=2, figsize=(10, 5), sharex=True, sharey=True)
# For consistency's sake, we'll set the bins to be identical
bins = np.linspace(0, 1, 10)
axes[0].hist2d(*small_data, bins=bins, vmin=0, vmax=5)
axes[1].hist2d(*large_data, bins=bins, vmin=0, vmax=5)
plt.show()
关于python - 改变matplotlib histogram2d的高度范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236907/