Different ways of Multiple Subplots with Variable Gridspecs

1. Data Importing and Preprocessing

In [3]:
import pandas as pd

# read .csv, I've downloaded csv file here. https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data
file_url = 'https://raw.githubusercontent.com/actruce/PythonLibraries/master/Matplotlib/data/time_series_covid19_gloabal.csv'
df_org = pd.read_csv(file_url)

Let's do some pre-processing !

In [4]:
df_china = df_org.copy()
df_china = df_china[df_china['Country/Region'].isin(['China'])]
df_china = df_china.groupby('Country/Region').agg('sum')
df_china.drop(['Lat', 'Long'], axis=1, inplace=True)
In [5]:
df = df_org.copy()
df = df[df['Country/Region'].isin(['Korea, South', 'Japan', 'US', 'Italy'])]
df = df.iloc[:, 1:]
df.drop(['Lat', 'Long'], axis=1, inplace=True)
df.set_index(['Country/Region'], inplace=True)
In [6]:
df = pd.concat((df, df_china))
df = df.T
df.index.rename('Date', inplace=True)
In [7]:
df.head()
Out[7]:
Country/Region Italy Japan Korea, South US China
Date
1/22/20 0 2 1 1 548
1/23/20 0 2 1 1 643
1/24/20 0 2 2 2 920
1/25/20 0 2 2 2 1406
1/26/20 0 4 3 5 2075

2-1. Using fig.add_subplot()

In [23]:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8))

ax1 = fig.add_subplot(1,2,1)
df.plot(ax=ax1)
ax1.set_ylabel('Number of Confirms')
ax1.set_title('COVID-19 Trends by major Countries')
ax1.legend(loc='upper left', bbox_to_anchor=(0.05, 0.95))

ax2 = fig.add_subplot(2,2,2)
df.plot(ax=ax2)
ax2.set_ylabel('log. scale')
ax2.set_yscale('log')
ax2.axvline(x=64, ymin=0, ymax=0.85, c='k', linestyle='--') # add vertical line at ax2
ax2.text(0.56, 0.9, 'Big Change', transform=ax2.transAxes)
ax2.legend(loc='lower right', prop={'size': 9})

ax3 = fig.add_subplot(2,2,4)
df.plot(ax=ax3)
ax3.set_ylim([-1, 100000.])
ax3.set_ylabel('y-axis lim=100,000')
ax3.legend(loc='center left', bbox_to_anchor=(0.66, 0.5), prop={'size': 9})
Out[23]:
<matplotlib.legend.Legend at 0x18263f102c8>

2-2. Using fig.add_axes()

In [260]:
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8))
ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.9])
ax2 = fig.add_axes([0.6, 0.6, 0.35, 0.4])
ax3 = fig.add_axes([0.6, 0.1, 0.35, 0.4])

df.plot(ax=ax1)
ax1.set_ylabel('Number of Confirms')
ax1.set_title('COVID-19 Trends by major Countries')

df.plot(ax=ax2)
ax2.set_ylabel('log. scale')
ax2.set_yscale('log')
ax2.axvline(x=64, ymin=0, ymax=0.85, c='k', linestyle='--') # add vertical line at ax2
ax2.text(0.56, 0.9, 'Big Change', transform=ax2.transAxes)

df.plot(ax=ax3)
ax3.set_ylim([-1, 100000.])
ax3.set_ylabel('y-axis lim=100,000')
Out[260]:
Text(0, 0.5, 'y-axis lim=100,000')

2-3. Using Gridspec

In [266]:
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(12, 8))
spec = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.1)

ax1 = fig.add_subplot(spec[:, 0])
ax2 = fig.add_subplot(spec[0, 1])
ax3 = fig.add_subplot(spec[1, 1])

df.plot(ax=ax1)
ax1.set_ylabel('Number of Confirms')
ax1.set_title('COVID-19 Trends by major Countries')

df.plot(ax=ax2)
ax2.set_ylabel('log. scale')
ax2.set_yscale('log')
ax2.axvline(x=64, ymin=0, ymax=0.85, c='k', linestyle='--') # add vertical line at ax2
ax2.text(0.56, 0.9, 'Big Change', transform=ax2.transAxes)

df.plot(ax=ax3)
ax3.set_ylim([-1, 100000.])
ax3.set_ylabel('y-axis lim=100,000')
Out[266]:
Text(0, 0.5, 'y-axis lim=100,000')
In [ ]: