python - 如何在 matplotlib 中绘制双对数直方图

标签 python matplotlib

我想知道如何使用双对数图来可视化列表中元素的频率,例如:

my_list=[1,2,3,3,3,4,1,2,5,2,10,4,5,3,3,3,2,1]

我使用直方图绘制数据:

plt.hist(my_list, label='Frequency Distribution of matches')
plt.legend()
plt.ylabel('Frequency')

但最好将其可视化为双对数。

最佳答案

plt.hist包含一个 log 参数,其行为类似于 plt.yscale('log')因为它只缩放 y 轴:

log: If True, the histogram axis will be set to a log scale.

要同时缩放 x 轴,请将其与 plt.xscale('log') 结合使用:

plt.hist(my_list, log=True)
plt.xscale('log')

如果您想要等宽条形,请将bins定义为10 **边:

start, stop = np.log10(min(my_list)), np.log10(max(my_list))
bins = 10 ** np.linspace(start, stop, 10)

plt.hist(my_list, log=True, bins=bins)
ax.set_xscale('log')

要获取频率的双对数线图,请使用 plt.stairs (需要 matplotlib 3.4+)与 np.histogram :

plt.stairs(*np.histogram(my_list))
plt.yscale('log')
plt.xscale('log')

关于python - 如何在 matplotlib 中绘制双对数直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70088437/

相关文章:

python - 将 alpha 添加到现有的 matplotlib 颜色图

python - 如何使用 Deform 和 Colander 使文件上传成为兼性?

python - 以元组形式将列表添加到字典中

python - Matplotlib 中figure.add_axes() 中的left 和bottom 有何作用?

python - 从 MatPlotLib Canvas 获取二进制图像数据?

python - 如何在 matplotlib 中的 FigureCanvas 顶部显示 GTK 小部件

python - 想要胖错误栏

python - 在 Python 3.6 中 - 使用 XPath 表达式获取文本

python - Pandas :计算数据框中的唯一值

python - 注释堆叠水平条形图的值