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`:

Python
``````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()
``````
Copied!

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

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.

jwsayyes

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

Alireza637

That zoom thingy at the end is specific to your IDE / env? Cause I don’t have it in my Jupyter Notebook!

Bartosz Zaczyński RP Team

@Alireza637 When you call `plt.show()` in a Jupyter Notebook, it renders the plot using an alternative backend. Normally, the plot ends up shown in Matplotlib’s popup window, which has this toolbar with the zoom icon.

Alireza637

@Bartosz Zaczyński Thanks for your response. Still, it renders the plot in the notebook itself and not in a popup. Tho, it’s not that big of a deal, was wondering what may be different in my env than the one in the tutorial.

Bartosz Zaczyński RP Team

@Alireza637 Matplotlib renders your plot in the notebook because that’s the default backend it uses in Jupyter notebooks. You can change the backend using the `%matplotlib` magic in your cell:

``````%matplotlib qt
import matplotlib.pyplot as plt
plt.plot([5, 3, 6, 2, 8, 5])
plt.show()
``````

This will display the popup from the video with the toolbar and the zoom icon. Before you try it, however, make sure that you have one of these libraries installed in your virtual environment: PyQt6, PySide6, PyQt5, PySide2. If not, then try this command:

``````(venv) \$ python -m pip install pyqt6
``````

Available Matplotlib backends are:

• `tk`
• `gtk`
• `gtk3`
• `gtk4`
• `wx`
• `qt4`
• `qt5`
• `qt6`
• `qt`
• `osx`
• `nbagg`
• `webagg`
• `notebook`
• `agg`
• `svg`
• `pdf`
• `ps`
• `inline`
• `ipympl`
• `widget`

Hope this helps!

Alireza637

@Bartosz Zaczyński It did, thanks a bunch

Bartosz Zaczyński RP Team

You’re welcome 😊

to join the conversation.

Lesson Completed!
Lesson Bookmarked
Request Failed :(