如何在Python

时间:2016-04-18 11:40:40

标签: python matplotlib graph

我有一年的数据集及其数字描述。例如:

X     Y
1890  6
1900  4
2000  1
2010  9

我绘制了一个像plt.bar(X,Y)这样的栏,它看起来像: enter image description here

如何使X标度的步骤更加轻松,例如,2年? 我可以每隔5年用另一种颜色,红色,以保持不稳定的边界?

1 个答案:

答案 0 :(得分:1)

有一些不同的方法可以做到这一点。这是一个可能的解决方案:

import matplotlib.pyplot as plt
x = [1890,1900,2000,2010]
y = [6,4,1,9]
stepsize = 10 # Chose your step here
fig, ax = plt.subplots()
ax.bar(x,y)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, stepsize))
plt.show()

,结果是:

Setting xticks for bar plot