Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Your First Plot

Give Feedback

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 be plt.subplots ?

Austin Cepalia RP Team on Oct. 27, 2019

Oh yeah, there is a minor typo in the description. Unfortunately I can’t edit that directly, but I’ll see if I can get that fixed

Dan Bader RP Team on Oct. 27, 2019

I just fixed the typo, thanks for the heads up @avinashhm :)

avinashhm on Oct. 29, 2019

no worries .. thanks for fixing @dan, @austin !!

Become a Member to join the conversation.