Bar chart with Python
A bar chart is a chart in which each category is represented by a horizontal rectangle, with the length of the rectangle proportional to the values being plotted. The horizontal axis shows data value, and the vertical axis displays the categories being compared.
More about: Bar chart
Basic bar chart
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle','bar'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/bar.csv')
df
#sort value in descending order
'displaced_number',inplace=True)
df.sort_values(
#prepare data array for plotting
= df['country_origin']
x = df['displaced_number']
y
#wrap long labels
= [ '\n'.join(wrap(l, 20)) for l in x ]
x
#plot the chart
= plt.subplots()
fig, ax = ax.barh(x, y)
bar_plot
#set chart title
'People displaced across borders by country of origin | 2020')
ax.set_title(
#set x-axis title
'Displaced population (millions)')
ax.set_xlabel(
#set x-axis label
=True)
ax.tick_params(labelbottom
#set x-axis limit
= plt.xlim(0, 7000000)
xlimit
#show grid below the bars
='x')
ax.grid(axis
#format x-axis tick labels
def number_formatter(x, pos):
if x >= 1e6:
= '{:1.0f}M'.format(x*1e-6)
s elif x < 1e6 and x >= 1e3:
= '{:1.0f}K'.format(x*1e-3)
s else:
= '{:1.0f}'.format(x)
s return s
ax.xaxis.set_major_formatter(number_formatter)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -40), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -50), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate(
#adjust chart margin and layout
fig.tight_layout()
#show chart
plt.show()
Bar chart with data label
# import libraries
import matplotlib.pyplot as plt
import pandas as pd
from textwrap import wrap
'unhcrpyplotstyle','bar'])
plt.style.use([
#load data set
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/comparison/bar.csv')
df
#sort value in descending order
'displaced_number', inplace=True)
df.sort_values(
#prepare data array for plotting
= df['country_origin']
x = df['displaced_number']
y
#wrap long labels
= [ '\n'.join(wrap(l, 20)) for l in x ]
x
#plot the chart
= plt.subplots()
fig, ax = ax.barh(x, y)
bar_plot
#set chart title
'People displaced across borders by country of origin | 2020')
ax.set_title(
#set subtitle
'Number of people in millions', x=0.36, y=0.88)
plt.suptitle(
# set formatted data label
=[f'{x*1e-6:,.1f}' for x in bar_plot.datavalues], padding=3)
ax.bar_label(bar_plot, labels
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -5), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('© UNHCR, The UN Refugee Agency', (0,0), (0, -15), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate(
#adjust chart margin and layout
fig.tight_layout()
#show charts
plt.show()