You’re ready to start coding your first plot! This will be a stack plot showing the combined debt growth over time for three different regions. You’ll be using random numbers rather than real data.
You’ll import the necessary modules, generate random data with numpy
, and plot that data with matplotlib
:
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(444)
rng = np.arange(50)
rnd = np.random.randint(0, 10, size=(3, rng.size))
yrs = 1950 + rng
print(rng + rnd)
fig, ax = plt.subplots(figsize=(5, 3))
ax.stackplot(yrs, rng+rnd, labels=['Eastasia', 'Eurasia', 'Oceania'])
ax.set_title('Combined debt growth over time')
ax.legend(loc='upper left')
ax.set_ylabel('Total debt')
ax.set_xlim(xmin=yrs[0], xmax=yrs[-1])
fig.tight_layout()
plt.show()
avinashhm on Oct. 25, 2019
Hi There , great tutorial !
minor correction .. i guess
plt.sobplots
was intended to beplt.subplots
?