首页 > 解决方案 > 不明白为什么 matplotlib 条形宽度不恒定

问题描述

嗨,我想有很多关于这个的帖子。但我无法真正理解发生了什么。基于其他堆栈溢出问题,我猜测我的条形图宽度不恒定的原因是由于“非线性轴?”

我真的不明白为什么这会导致我的 matplotlib barplot 宽度不一样:

# Initialize Data Frame
df = pd.DataFrame({r'$l$': range(1,4)}, index = range(1, 4))

# Add other stuff
df[r'Frequency (emitted photon) / $\frac{k_B \Theta_R}{h}$'] = (df[r'$l$'] * (df[r'$l$'] + 1) - 
                                                                (df[r'$l$'] - 2)*(df[r'$l$'] - 1))

df[r'Probabilities $p(l)$'] = (np.exp((-df[r'$l$']/300) * (df[r'$l$'] + 1)) / 
                              (((2*df[r'$l$'] + 1) * np.exp((-df[r'$l$']/300) * (df[r'$l$'] + 1))) + 
                              ((2 * df[r'$l$'] - 1) * np.exp(-((df[r'$l$'] - 2)/300) * (df[r'$l$'] - 1)))
                              ))

# Graph Settings
fig, axes = plt.subplots(nrows = 1, ncols = 1, figsize = (16,8))


axes.bar(df[r'Frequency (emitted photon) / $\frac{k_B \Theta_R}{h}$'],
         df[r'Probabilities $p(l)$'], label = 'Probability vs Frequency', color = 'red', width = 0.1)

# Graph Settings
axes.set_title('Probabilities against frequency(emitted photon)', fontsize = 20)
axes.xaxis.set_ticks(range(0, 13))


# Display Graph
plt.show()

即使我将宽度设置为 0.1,条形图也非常难看

在此处输入图像描述

为什么第一个条的宽度还可以,但其余的那么胖?我如何纠正它以使所有宽度都相同?

标签: pythonmatplotlib

解决方案


推荐阅读