Matplotlib savefig blank image

Here we are going to discuss a very common problem that was faced by many new matplotlib learners or programmers. And we will try to cover some of the solutions to overcome this issue “matplotlib savefig blank image“.

Check out, What is Matplotlib and how to use it in Python

matplotlib savefig blank image

Problem: When a programmer plot a graph or chart in matplotlib and try to save it as an image in their local system but the figure doesn’t show.

Firstly I should clear to you that this is not an error. Sometime’s when a user saves a figure by using the “savefig()” method they get a black page.

Let’s see an example of the above-described problem:

# Import Library

import matplotlib.pyplot as plt

# Define Data

student = [3,2,0,5,9,1]
weight = [30,35,40,45,50,55]

# Plot Graph
  
plt.scatter(weight,student) 

# Define axes label

plt.xlabel("Weight of the students")
plt.ylabel("Number of students")

# show plot

plt.show()

# Save image

plt.savefig('figure not shown', dpi = 150)
  • In the above example, we firstly import matplotlib.pyplot library.
  • After this, we define data coordinates and use plt.scatter() method to draw a scatter plot.
  • We use plt.xlabel() and plt.ylabel() method to define x-axis labels and y-axis labels respectively.
  • After this we use plt.show() method to visualize the plot on the screen.
  • Finally, we use plt.savefig() method to save your plot as an image.
matplotlib savefig not show
” Figure show in jupyter notebook or on your screen”
matplotlib figure not show
“Directory where the figure is shown in your local system”
matlotlib savefig having blank page
” Output when you open saved figure from your system”

Above you see that when we open the save figure we get a blank page instead of a plot.

Check out, Matplotlib subplot tutorial

Solution#1: By using plt.savefig()

The solution for this problem, is you have to call plt.show() method after the plt.savefig() method.

Let’s have a look at a code:

# Import Library

import matplotlib.pyplot as plt

# Define Data

student = [3,2,0,5,9,1]
weight = [30,35,40,45,50,55]

# Plot Graph
  
plt.scatter(weight,student) 

# Define axes label

plt.xlabel("Weight of the students")
plt.ylabel("Number of students")

# Save image

plt.savefig('figure shown', dpi = 150)

# show plot

plt.show()

In the above example, we use the savefig() method before the show() method.

matplotlib savefig shown
” Output on your screen”
matplotlib savefig figure show
“Directory where image saved”
matplotlib savefig() fig show
“Output when you open saved image from your system”

Conclusion! When get an figure instead of blank page when we call savefig() method before show() method in matplotlib.

Also, Read: modulenotfounderror: no module named ‘matplotlib’

Solution#2: By using plt.figure()

The next solution for this problem is to use the figure() method to save figures as images. In this, we use the show() method before the savefig() method.

Let’s see the code:

# Import Library

import matplotlib.pyplot as plt

# Define Data

student = [3,2,0,5,9,1]
weight = [30,35,40,45,50,55]

# Plot Graph

fig = plt.figure()
  
plt.scatter(weight,student) 

# Define axes label

plt.xlabel("Weight of the students")
plt.ylabel("Number of students")

# show plot

plt.show()

# Save image

fig.savefig('figure', dpi = 150)
  • In the above example, we import matplotlib.pyplot library and define the data coordinates.
  • After that we use plt.figure() method and plt.scatter() method to plot a scatter plot.
  • Then we define axes labels by using plt.xlabel() and plt.ylabel() method.
  • plt.show() method is used to visualize the plot.
  • Finally, in last we use fig.savefig() method to save a figure as an image.
matplotlib savefig blank image
plt.figure()

Conclusion! Here we use plt.figure() method and fig.savefig() method to save figure as an image.

So, in this tutorial, we learned about Matplotlib savefig() “figure” not show error or matplotlib savefig blank image issue.

You may also like to read the following Matplotlib tutorials.