In this tutorial, we will introduce how to save matplotlib plot to an image with different dpi and background color.
1.Import library
import matplotlib.pyplot as plt import numpy as np
2.Create a plot
fig = plt.figure() x = np.arange(0, 10, 0.1) y = np.sin(x) plt.plot(x, y)
3.Save plot image with different dpi
fig.savefig('saved_figure-50pi.png', dpi = 50) fig.savefig('saved_figure-100dpi.png', dpi = 100) fig.savefig('saved_figure-1000dpi.png', dpi = 1000)
We can set dpi to save plot image in fig.savefig().
4.Save plot image with different background
plt.savefig('saved_figure-tight.png', bbox_inches = 'tight', facecolor='red')
We can set facecolor to save image in plt.savefig().