【问题标题】:Multiple step histograms in matplotlibmatplotlib 中的多步直方图
【发布时间】:2014-11-01 18:46:45
【问题描述】:

亲爱的 python/matplotlib 社区,

我在 matplotlib 中遇到问题:我似乎无法使用以下方法在同一绘图空间中绘制多个重叠直方图:

binsize = 0.05

min_x_data_sey, max_x_data_sey    = np.min(logOII_OIII_sey), np.max(logOII_OIII_sey)
num_x_bins_sey                    = np.floor((max_x_data_sey - min_x_data_sey) / binsize)

min_x_data_comp, max_x_data_comp  = np.min(logOII_OIII_comp), np.max(logOII_OIII_comp)
num_x_bins_comp                   = np.floor((max_x_data_comp - min_x_data_comp) / binsize)

min_x_data_sf, max_x_data_sf      = np.min(logOII_OIII_sf), np.max(logOII_OIII_sf)
num_x_bins_sf                     = np.floor((max_x_data_sf - min_x_data_sf) / binsize)

axScatter_farright = fig.add_subplot(gs_right[0,0])

axScatter_farright.tick_params(axis='both', which='major', labelsize=10)
axScatter_farright.tick_params(axis='both', which='minor', labelsize=10)
axScatter_farright.set_ylabel(r'$\mathrm{N}$', fontsize='medium')
axScatter_farright.set_xlim(-1.5, 1.0)
axScatter_farright.set_xlabel(r'$\mathrm{log([OII]/[OIII])}$', fontsize='medium')

axScatter_farright.hist(logOII_OIII_sey, num_x_bins_sey, ec='0.3', fc='none', histtype='step')
axScatter_farright.hist(logOII_OIII_comp, num_x_bins_comp, ec='0.3', fc='none', histtype='step')
axScatter_farright.hist(logOII_OIII_sf, num_x_bins_sf, ec='0.3', fc='none', histtype='step')

好像轴类不能处理多个直方图?如果和/或哪里出了问题,请纠正我。

我的整体情节是 1 行 3 列的绘图空间。我想使用网格规范为地块提供良好的布局。

这是我的情节到目前为止的样子:

这就是我希望图形的直方图部分在步骤类型直方图叠加(带有图例)方面看起来像:

我将数据集作为从 csv 文件生成的三个不同的元组类型数组。即,使用x, y = np.genfromtext(datafile.csv)

如果有人能够解释如何做到这一点,我将非常感激。

【问题讨论】:

    标签: python matplotlib plot histogram


    【解决方案1】:

    您正在做的事情应该完美无缺。是否有可能只有一个分布在您之前设置的几行的 -1.5 到 1 的 x 范围内? (即尝试删除手动 set_xlim 语句并查看其他发行版是否出现。)

    作为一个快速、独立的示例来证明事情应该有效:

    import numpy as np
    import matplotlib.pyplot as plt
    
    num = 1000
    d1 = np.random.normal(-1, 1, num)
    d2 = np.random.normal(1, 1, num)
    d3 = np.random.normal(0, 3, num)
    
    fig, ax = plt.subplots()
    ax.hist(d1, 50, ec='red', fc='none', lw=1.5, histtype='step', label='Dist A')
    ax.hist(d2, 50, ec='green', fc='none', lw=1.5, histtype='step', label='Dist B')
    ax.hist(d3, 100, ec='blue', fc='none', lw=1.5, histtype='step', label='Dist C')
    ax.legend(loc='upper left')
    plt.show()
    

    (如果您希望图例显示线条而不是框,则需要使用代理艺术家。如果您愿意,我可以添加一个示例。不过,这超出了这个问题的范围。)

    【讨论】:

    • 在打开和关闭 xlim 时都出现以下错误:AttributeError: max must be greater than min in range parameter.
    • 我认为我的问题来自数据的最大值和最小值。
    • 惊喜,我的一个元组数组中有一些 NaN。玩玩玩玩!
    • 嗨@Joe,您提到了图例行而不是框,如何实现?你会扩展它吗?非常感谢。
    猜你喜欢
    • 2014-03-17
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2014-02-14
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多