FAQ: Line Graphs in Matplotlib - Figures

This community-built FAQ covers the “Figures” exercise from the lesson “Line Graphs in Matplotlib”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does Python use inches (used in only three countries) ? Can it use centimetres (used in the 192 countries)?

That’s not necessarily Python but the 3rd party Matplotlib library. I’m not aware of any built-in method (at least not since I last checked) of using cm instead of inches and I can’t see it happening since it’d likely cause problems with code behaving differently on different machines. If you have a pressing need to size figures in cm you might have to write a quick function or something along those lines to convert the two.

I have 3 questions. Hope someone could help me.
①Why only the latter figure is shown when I call plt.show()? What happened to the first figure?
②What is `plt.close('all')` for?
③Why do I need to call `plt.figure()` before `plt.plot()` and if I reverse the order, no figure will be displayed?

``````import codecademylib
from matplotlib import pyplot as plt

word_length = [8, 11, 12, 11, 13, 12, 9, 9, 7, 9]
power_generated = [753.9, 768.8, 780.1, 763.7, 788.5, 782, 787.2, 806.4, 806.2, 798.9]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]

plt.close('all')
plt.plot(years, word_length)
plt.savefig('winning_word_lengths.png')

plt.figure(figsize=(7,3))
plt.plot(years, power_generated)
plt.savefig('power_generated.png')

plt.show()
``````

Thanks in advance

Hi there,

When I run this code I get the 2 graphs but I dont get the green tick?

``````import codecademylib
from matplotlib import pyplot as plt

word_length = [8, 11, 12, 11, 13, 12, 9, 9, 7, 9]
power_generated = [753.9, 768.8, 780.1, 763.7, 788.5, 782, 787.2, 806.4, 806.2, 798.9]
years = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]

plt.close('all')

plt.subplot(2,1,1)
plt.plot(years, word_length)
plt.savefig('tall_and_narrow.png')
plt.subplot(2,1,2)
plt.plot(years, power_generated)
plt.show()
plt.savefig('power_generated.png')
``````

Id forgotten to add:

``````plt.figure(figsize=(7,3))
``````

I’m confused on why it is not in pixels… This makes me think that this is then intended for printing purposes

If someone is interested in difference between plt.clf() and plt.close(), I found this very helpful

python - When to use cla(), clf() or close() for clearing a plot in matplotlib? - Stack Overflow