Bar Chart

The best way to visualize a comparision of categorical data is by using a bar chart.

I. Vertical bar chart

To create a bar chart in Matplotlib, we provide 2 arguments to the function

  • x-values: a list of x-positions for each bar
  • y-values: a list of heights for each bar, y_values)

For example, df.population)

II. Horizontal bar chart

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)

For example

plt.barh(df.cities, df.population)

III. Stacked bar charts

In stacked bar chart, we display two different sets of bars.

To create stacked bar charts with df.table1 as bottom, we use:, df.table1), df.table2, bottom=df.table1)

For example

# The bottom bars are df.male, df.male, label='Male')

# The top bars are df.female, df.female, bottom=df.male, label='Female')

# Add a legend

# Display the plot

IV. Adding error bars

To create error bars, we can use the argument yerr after our first two positional arguments in, df.population, yerr=df.error)