Bar Charts#
One of the common uses for bar charts is to plot categorial variables. A bar chart presents categorical data with rectangular bars with heights or lengths proportional to the values they represent.
%matplotlib inline
import matplotlib.pyplot as plt, numpy as np # import statements
# basic vertical bar chart
data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20} # dictionary with categories and amounts
names = list(data.keys()) # get category names
values = list(data.values()) # get values
fig, axs = plt.subplots() # create figure
axs.bar(names, values) # create plot
plt.show() # show output
# horizontal bar chart
data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20} # dictionary with categories and amounts
names = list(data.keys()) # get category names
values = list(data.values()) # get values
fig, axs = plt.subplots() # create figure
axs.barh(names, values) # create plot
plt.show() # show output
Additional Resources#
For more on bar charts and plotting categorical data: