Save Plot as Image with Matplotlib

python_tutorials

Introduction

Matplotlib is one of the most widely used data visualization libraries in Python. It’s common to share Matplotlib plots and visualizations with others.

In this article, we’ll take a look at how to save a plot/graph as an image file using Matplotlib.

Creating a Plot

Let’s first create a simple plot:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.1)
y = np.sin(x)

plt.plot(x, y)
plt.show()

Here, we’ve plotted a sine function, starting at 0 and ending at 10 with a step of 0.1. Running this code yields:

sine visualization python

Now, let’s take a look at how we can save this figure as an image.

Save Plot as Image in Matplotlib

In the previous example, we’ve generated the plot via the plot() function, passing in the data we’d like to visualize.

This plot is generated, but

To finish reading, please visit source site