Line chart with Python
A line chart is a type of chart that displays the evolution of one or several numeric variables over a continuous interval or time period. Typically, the x-axis is used for a timescale or a sequence of intervals, while the y-axis reports values across that progression.
More about: Line chart
Single line chart
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','line'])
plt.style.use([
#compute data array for plotting
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/line.csv')
df
= df.pivot(index='year', columns='population_type', values='population_number')
df = df.reset_index()
df
#compute data for plotting
= df['year']
x = df['Refugees']
y
#plot the chart
= plt.subplots()
fig, ax = ax.plot(x, y)
bar_plot
#set chart title
'Number of refugees | 1990-2021')
ax.set_title(
#set y-axis label
'Number of people (millions)')
ax.set_ylabel(
#set y-axis limit
= plt.ylim(8 * 1e6, 22 * 1e6)
ylimit
#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 > 0:
= '{:1.0f}K'.format(x*1e-3)
s else:
= '{:1.0f}'.format(x)
s return s
ax.yaxis.set_major_formatter(number_formatter)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -25), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -35), 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()
Multiple line chart
# import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
'unhcrpyplotstyle','line'])
plt.style.use([
#compute data array for plotting
= pd.read_csv('https://raw.githubusercontent.com/GDS-ODSSS/unhcr-dataviz-platform/master/data/change_over_time/line.csv')
df
= df.pivot(index='year', columns='population_type', values='population_number')
df = df.reset_index()
df
#compute data for plotting
= df['year']
x = df['IDPs']
y1 = df['Refugees']
y2
#plot the chart
= plt.subplots()
fig, ax = ax.plot(x, y1)
bar_plot = ax.plot(x, y2)
bar_plot
#set chart title
'Number of refugees and IDPs of concern to UNHCR | 1990-2021')
ax.set_title(
#set y-axis label
'Number of people (millions)')
ax.set_ylabel(
#set y-axis limit
= plt.ylim(0, 60 * 1e6)
ylimit
#set direct labeling for lines
=x.iloc[-1]
idp_xpos=y1.iloc[-1]
idp_ypos"IDPs", (idp_xpos,idp_ypos),
plt.annotate(="offset points",
textcoords=(0,10),
xytext='center')
ha=x.iloc[-1]
ref_xpos=y2.iloc[-1]
ref_ypos"Refugees", (ref_xpos,ref_ypos),
plt.annotate(="offset points",
textcoords=(0,10),
xytext='left')
ha
#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 > 0:
= '{:1.0f}K'.format(x*1e-3)
s else:
= '{:1.0f}'.format(x)
s return s
ax.yaxis.set_major_formatter(number_formatter)
#set chart source and copyright
'Source: UNHCR Refugee Data Finder', (0,0), (0, -25), xycoords='axes fraction', textcoords='offset points', va='top', color = '#666666', fontsize=9)
plt.annotate('©UNHCR, The UN Refugee Agency', (0,0), (0, -35), 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()