headings=[['Jan to Feb'], ['Feb to Mar'], ['Mar to Apr'], ['Apr to May'], ['May to June'], ['Jun to July'], ['July to Aug'], ['Aug to Sep'], ['Sep to Oct'], ['Oct to Nov']]
incoming=[[27, 42, 66, 85, 65, 64, 68, 68, 77, 58],
[24, 39, 58, 79, 60, 62, 67, 62, 55, 35],
[3, 3, 8, 6, 5, 2, 1, 6, 22, 23],
[3, 3, 8, 6, 5, 2, 1, 6, 22, 23],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
joint=[[486, 511, 615, 825, 760, 693, 848, 639, 651, 657]
[444, 482, 562, 793, 729, 666, 812, 599, 592, 597]
[42, 29, 53, 32, 31, 27, 36, 40, 59, 60]
[29, 28, 35, 32, 31, 27, 36, 40, 59, 60]
[13, 1, 18, 0, 0, 0, 0, 0, 0, 0]
]
见照片: http://s1362.photobucket.com/user/superempl/media/figure_1_zps782354d7.png.html
我需要将x轴标签向右移动。我试过了:
plot.set_xticklabels(headings,multialignment='right')
但这没有效果。
有人可以解释必须采取哪些措施来改变这些标签在每个酒吧中心的位置。
我的代码如下:
import pylab as p
from matplotlib.ticker import MaxNLocator
fig = p.figure(figsize=(20,8))
plot = fig.add_subplot(111)
ind = range(len(headings))
bar1 = plot.bar(ind,joint[0],facecolor='#777777')
bar2 = plot.bar(ind,outgoing[0],facecolor='#B0E0E6')
# x-axis
plot.xaxis.set_major_locator(MaxNLocator(10))
plot.set_xticklabels(headings)
p.show()
答案 0 :(得分:2)
或许,请尝试以下方法:
# headings should be simple list
headings=['Jan to Feb', 'Feb to Mar', 'Mar to Apr', 'Apr to May', 'May to June',
'Jun to July', 'July to Aug', 'Aug to Sep', 'Sep to Oct', 'Oct to Nov']
align='center'
上的中心栏:
bar1 = plot.bar(ind,joint[0],facecolor='#777777', align='center')
然后设置刻度位置并勾选标签:
ax.xaxis.set_ticks(ind)
ax.xaxis.set_ticklabels(h)
ax.set_xlim(ind[0]-.5,ind[-1]+.5) # set width of axis
fig.canvas.draw() # don't forget to draw changes
这样标签将在条形下方居中: