matplotlib中的多步直方图

2024-09-28 21:55:00 发布

您现在位置:Python中文网/ 问答频道 /正文

尊敬的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')

axes类似乎不能处理多个直方图?如果我错了,请纠正我。在

我的整体绘图是一个1行3列的绘图空间。我想使用网格规范给绘图一个好的布局。在

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

enter image description here

这就是我想要的图的直方图部分在阶梯型直方图叠加(带图例)方面的外观:

enter image description here

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

如果有人能解释这是怎么做到的,我将非常感激。在


Tags: 绘图datanpsf直方图minnummax
1条回答
网友
1楼 · 发布于 2024-09-28 21:55:00

你所做的应该是完美的。有没有可能只有一个分布在-1.5到1的x范围内,你之前已经设置了几行?(例如,尝试删除manualset_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()

enter image description here

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

相关问题 更多 >