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 `plt.bar()`:

• x-values: a list of x-positions for each bar
• y-values: a list of heights for each bar
``````plt.bar(x_values, y_values)
plt.show()
``````

For example

``````plt.bar(df.cities, df.population)
plt.ylabel("Population")
plt.show()
``````

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)
plt.ylabel(value)
plt.show()
``````

For example

``````plt.barh(df.cities, df.population)
plt.ylabel("Population")
plt.show()
``````

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:

``````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')

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)