【问题标题】:Altering height range of matplotlib histogram2d改变 matplotlib histogram2d 的高度范围
【发布时间】:2015-05-28 00:23:54
【问题描述】:

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

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

【问题讨论】:

    标签: python matplotlib histogram2d


    【解决方案1】:

    一般来说,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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      相关资源
      最近更新 更多