import numpy as np
import matplotlib.pyplot as plt
N = 3
Start_means = (100, 50, 50)
Start_std = (2, 3, 4)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, Start_means, width, color='xkcd:cyan', yerr=Start_std)
End_means = (80, 30, 30)
End_std = (3, 5, 2)
rects2 = ax.bar(ind + width, End_means, width, color='xkcd:red', yerr=End_std)
# add some text for labels, title and axes ticks
ax.set_ylabel('Available')
ax.set_title('Travel availability, by tour')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('Italy', 'China', 'France',))
如何在此处获取多列?例如Underneath Italy我想要“ID:12345”代表中国“ID:13579”和法国“ID:24680”
ax.legend((rects1[0], rects2[0]), ('Start', 'End'))
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()
答案 0 :(得分:0)
除非出于某种原因你真的想要一个新的x轴,我认为只需制作一组中间有'\n'
的x标签就可以做得更好。
import numpy as np
import matplotlib.pyplot as plt
N = 3
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
Start_means = (100, 50, 50)
Start_std = (2, 3, 4)
End_means = (80, 30, 30)
End_std = (3, 5, 2)
fig, ax = plt.subplots()
rects1 = ax.bar(ind, Start_means, width, color='xkcd:cyan', yerr=Start_std)
rects2 = ax.bar(ind+width, End_means, width, color='xkcd:red', yerr=End_std)
ax.set_ylabel('Available')
ax.set_title('Travel availability, by tour')
ax.set_xticks(ind + width/2)
countries = ['Italy', 'China', 'France']
ids = ['ID:12345', 'ID:48900', 'ID:56789']
xlabels = []
for i, j in zip(countries, ids):
xlabels.append(i + '\n' + j)
ax.set_xticklabels(xlabels)
plt.show()
如果您想要单独的xaxis,请参阅this answer。