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

# Your First Plot

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, xmax=yrs[-1])

fig.tight_layout()
plt.show()
`````` avinashhm

Hi There , great tutorial !

minor correction .. i guess `plt.sobplots` was intended to be `plt.subplots` ? Austin Cepalia RP Team

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

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

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

Dear Austin,

I tried the code, but the error message pops up:

``````AttributeError                            Traceback (most recent call last)
<ipython-input-2-d96da0308728> in <module>
9 print(rng+rnd)
10
---> 11 fig, ax = plt.subplots(figsize =(5,3))
12
13 ax.stackplot(yrs, rnd+rng , labels =["Asia", "Europe", "Oceania"])

AttributeError: module 'matplotlib' has no attribute 'subplots'
``````

What I’m doing wrong? A Bartosz Zaczyński RP Team

@alazejha It looks like you might have aliased the wrong module. Make sure you have the following import statement at the top of your code:

``````import matplotlib.pyplot as plt
``````

Judging by the error message, I’m guessing you did this instead:

``````import matplotlib as plt
``````

However, this is only my guess since you haven’t shared the full code snippet. Let me know if that helps. rachelannkirkland

I am using Jupyter notebooks and the plot does not appear with `plt.show()`. How do I view the figure? I have previously used pyplot to create simple plots and it has worked with Jupyter notebooks.

I also have Visual Studio Code and PyCharm free versions on my computer which I could use instead. What do you recommend? I looked for LearningMPL but couldn’t find it on Google.

Thanks for your help. jwsayyes

@rachelannkirkland, you may want to try just type fig instead of plt.show().

to join the conversation.