Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

The bar graph from the preceding section plots bars vertically. We can also plot a horizontal bar graph, as shown here. Most of the code is the same as the code used in the previous section, except that plt.xticks() and plt.bar() are replaced with plt.yticks() and plt.barh() respectively:

# matplotlib accepts only floating point data types as its arguments 
for data.

# So months have to be represented in numerical format
month_num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
units_sold = [500, 600, 750, 900, 1100, 1050, 1000, 950, 800, 700, 550,
450]

fig, ax = plt.subplots()

# change the month number to month name on y axis
plt.yticks(month_num, calendar.month_name[1:13], rotation=20)

# plot horizontal bar graph
plot = plt.barh(month_num, units_sold)

# Display the graph on the screen
plt.show()

Here is how the output graph looks: