# 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“.

## 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.

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.

Conclusion! When get an figure instead of blank page when we call savefig() method before show() method in 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.