python - 改变matplotlib histogram2d的高度范围

标签 python matplotlib histogram2d

我正在尝试使用 matplotlib 的 histogram2d 绘制一些 2D 经验概率分布。我希望颜色在几个不同的图中具有相同的比例,但即使我知道结果分布的全局上限和下限,也找不到设置比例的方法。按原样,每个色标将从直方图箱的最小高度到最大高度运行,但每个图的这个范围都会不同。

一种可能的解决方案是强制一个容器采用我的下限高度,另一个容器采用我的上限高度。即使这看起来也不是一项非常简单的任务。

最佳答案

一般来说,matplotlib 中大多数内容的颜色缩放都是由 vminvmax 关键字参数控制的。

您必须稍微阅读一下字里行间的内容,但正如文档提到的,hist2d 中的其他 kwargs 会传递给 pcolorfast。因此,您可以通过 vminvmax 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()

enter image description here

关于python - 改变matplotlib histogram2d的高度范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236907/

相关文章:

javascript - 计算直方图图形列高

python - 使用opencv查找两个多边形之间的最大和最小距离

python - 当 QApplication 退出时,QT 引发段错误(核心已转储)

python - 使用 HDFStore 压缩数据

python - 离散 pyplot 散点颜色条

python - 如何在 Matplotlib 中与线程一起执行动画?

python - 为什么我可以使用按位 AND 检查列表是否包含字典的键?

python - 绘制排除 pandas 或 matplotlib 中缺失值的图表

python - 使用散点数据集生成热图