1

me very new to programming I have problem with bar chart. Here is my bar chart:

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',))

How do I get multiple columns here? For example Underneath Italy I want "ID: 12345" for china "ID: 13579" and for France "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()
TeemoMain
  • 105
  • 1
  • 1
  • 8

1 Answers1

0

Unless for some reason you really want a new x-axis, I think this can be better done by just making a set of x-labels with an '\n' in between.

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()

enter image description here

If you want to have a separate xaxis, see this answer.

krm
  • 847
  • 8
  • 13