Where do images get saved in Matplotlib when using savefig?

Question

Where do images get saved in Matplotlib when using savefig?

Answer

When running in the Codecademy interface, the savefig() function will not save the image files anywhere, due to the interface being completely online with no implementation to save on your computer.

However, if you run this outside of Codecademy, such as on Jupyter Notebook, it will save the image file to the current directory by default. If needed, you can also specify a file path instead of a file name to save the image in a specified directory. For example, if the directory is a path within your current directory, you can save it like so,

plt.savefig('subfolder/filename.png')

4 Likes

What is the difference between subplot and figure, if both are used to show or plot multiple plots.

5 Likes

subplot creates new plots within the same figure,

figure creates a separate figure with its own size and everything.

6 Likes

so do we still need

plt.show()

because when i tap on Give solution to this exercise… Both plots show without using ‘show’ command.
but when you hit ‘Run’ again only one graph shows. Can somebody tell me why this is happening. @jephos249

A little late to be replying, but since your question might be one others would have, it might be worth providing an answer even if you have already found it.

If you are creating two plots, you must include a plt.show() command for both.

For example, consider the following code which creates two separate plots and saves each as their own image file.

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')

In order to display these two plots, we would need to give each of the two plots their own plt.show() function.

plt.close('all')

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

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

This would then save both of plots to image files and also display both of the plots

3 Likes

I noticed the same thing too and I came across this solution using plt.show() once


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.figure(figsize=(8, 8))
plt.close()
plt.subplot(1,2,1)
plt.plot(years, word_length, color='green', marker='s')
plt.legend(['word length'])
plt.savefig('winning_word_lengths.png')


plt.figure(figsize=(7, 3))
plt.close()
plt.subplot(2,2,2)
plt.plot(years, power_generated, color='purple')

plt.subplots_adjust(wspace=0.35)
plt.legend(['power generated'])
plt.savefig('power_generated.png')

plt.show()

i noticed when you run it on jupyter notebook without the plt.close() underneath the figure, there are empty plots that take the shape of the figsize argument.

1 Like

If you’re familiar with Photoshop terminology, a figure is a canvas and the subplots are the layers on the canvas.

1 Like