python 用连续整数标记 x 轴绘制频率

python plot frequency with continuous integer labeled x-axis

我正在尝试根据以下 pandas table.

使用特殊的 x 轴请求制作频率图
     pos               freq
0     67                  1
1    285                  5
2    288                  1
3    397                  4
4    592                  1
5    640                  3

由于我的频率本身是整数(pos 列),我希望 x 轴只带有正常的连续数字标签。但是,使用简单的条形图:

plot_pos=pos_freq.plot(kind='bar',x='pos')

我只会得到标有 67、285、288、397、592、640 的 x 轴。相反,我希望 x 轴是一系列连续的 integers/intervals,例如60, 65, 70, 75, ....645, 650;并且频率条仍然显示在正确的位置。

我不确定此时条形图是否是一个不错的选择,而且我是 python 中的绘图新手。 (我可以在 excel 中完成;或者我可以添加另一个 x 轴...)任何关于更好方法的建议都将受到赞赏!

快速而肮脏:

pos_freq.reindex(range(pos_freq['pos'].min(), pos_freq['pos'].max())).plot(kind='bar')

matplotlib,

import matplotlib.pyplot as plt
plt.bar(pos_freq['pos'], pos_freq['freq'])
plt.show()

我无法通过 pandas dataframe.plot() 获取 x 标签,所以我直接使用了 matplotlib

import matplotlib.pyplot as plt
ticks = range(0,700,5)  #this could whatever interval you wanted
fig, ax = plt.subplots()
ax.bar(pos_freq["pos"],pos_freq["freq"])
ax.set_xticks(ticks)

你可能还想用所有这些额外的刻度把它做得更大一些

fig.set_size_inches(30,10.5)