October 2018
Beginner to intermediate
676 pages
18h 30m
English
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 formatmonth_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 axisplt.yticks(month_num, calendar.month_name[1:13], rotation=20)# plot horizontal bar graphplot = plt.barh(month_num, ...
Read now
Unlock full access