The best way to visualize a comparision of categorical data is by using a bar chart.
To create a bar chart in Matplotlib, we provide 2 arguments to the function plt.bar()
:
plt.bar(x_values, y_values)
plt.show()
For example
plt.bar(df.cities, df.population)
plt.ylabel("Population")
plt.show()
To create Horizontal bar chart, we use function plt.barh()
Horizontal bar chart can be useful if we have many bars because it can be easier to fit all the labels.
plt.barh(x_values, y_values)
plt.ylabel(value)
plt.show()
For example
plt.barh(df.cities, df.population)
plt.ylabel("Population")
plt.show()
In stacked bar chart, we display two different sets of bars.
To create stacked bar charts with df.table1 as bottom, we use:
plt.bar(df.value, df.table1)
plt.bar(df.value, df.table2, bottom=df.table1)
For example
# The bottom bars are df.male
plt.bar(df.population, df.male, label='Male')
# The top bars are df.female
plt.bar(df.population, df.female, bottom=df.male, label='Female')
# Add a legend
plt.legend()
# Display the plot
plt.show()
To create error bars, we can use the argument yerr
after our first two positional arguments in plt.bar()
.
plt.bar(df.cities, df.population, yerr=df.error)
plt.ylabel("Population")
plt.show()