gpt4 book ai didi

python - 为什么我的 matplotlib 条形图在我执行 'log' 时压缩 x 轴

转载 作者:行者123 更新时间:2023-11-28 22:35:19 24 4
gpt4 key购买 nike

我正在尝试使用 python 的 matplotlib 制作条形图。我可以制作普通图表,但当我将其置于对数模式以便更好地查看数据时,x 轴看起来好像在压缩。到底是怎么回事?注意:所有数据记录的最小值为1。

    x = [t[0] for t in data]
y = [t[1] for t in data]

x_pos = np.arange(len(x))

plt.bar(x_pos, y, color='blue', log=False)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
plt.xlim([0, len(x)])
#plt.yscale('log')
#plt.semilogy(x_pos, np.exp(-x_pos/5.0))
plt.savefig(output_path + '/' + filename)

产量:enter image description here

但只需将 log=False 更改为 log=True 我得到: enter image description here

我做错了什么?我只想获得 y 轴上第一张图的压缩 View 。如您所见,我也尝试了 yscale('log') 但我得到了相同的结果。

谢谢!

编辑:看起来它与前几行有关,当我删除第一行时它工作正常,但未排序:

data = sorted(data, key=lambda tup: tup[1], reverse=True)

# tuples (pair,count)
x = [t[0] for t in data]
y = [t[1] for t in data]

最佳答案

如果您只想在 y 轴上使用对数刻度而不是 x 轴,请使用:

import matplotlib.pyplot as plt

plt.semilogy(x,y) #plots log on y axis

plt.semilogx(x,y) #plots log on x axis

或者,您可以将轴设置为记录:

ax = plt.subplot(224)

ax.set_xscale("log")
ax.set_yscale("log")

plt.bar(x,y)

关于python - 为什么我的 matplotlib 条形图在我执行 'log' 时压缩 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38273946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com