当使用matplotlib / seaborn绘制直方图时,我想将x轴更改为log2标度。绘制的数据为here
当我取值的log2并制作直方图时,如果我绘制未记录的值并使用import model
更改x轴,它会给出错误的结果。代码是:
set_xscale
情节:
这是一个错误还是我错误地改变了轴?
答案 0 :(得分:4)
既不是!看看如果你增加垃圾箱的数量会发生什么:
plt.hist(df["y"], bins = 300)
ax1.set_xscale("log", basex=2)
ax2 = plt.subplot(2, 1, 2)
plt.hist(np.log2(df["y"]), bins=300)
直方图的数据是相同的,但是在大写的情况下,bin大小分布仍然是线性的。
如何使这两种情况理想化?将日志空间中的自定义bin大小传递给plt.hist
:
plt.figure()
sns.set_style("ticks")
ax1 = plt.subplot(2, 1, 1)
logbins = np.logspace(np.log2(df["y"].min()),
np.log2(df["y"].max()),
300, base=2)
plt.hist(df["y"], bins = logbins)
ax1.set_xscale("log", basex=2)
ax2 = plt.subplot(2, 1, 2)
plt.hist(np.log2(df["y"]), bins=300)
这两个地块之间仍然存在一些细微差别,但我认为它们与您的原始问题无关。