我想知道如何使用双对数图来可视化列表中元素的频率,例如:
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
: IfTrue
, 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/