Python ProgrammingPython Programming

Save plot to image file using Python Matplotlib

Save plot to image file:

import matplotlib.pyplot as plt

plt.grid(True, linewidth=0.5, color='#ff0000', linestyle='-')

#Plot a line graph
plt.plot([10, 20, 30, 40, 50])
# Add labels and title
plt.title("Interactive Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.savefig("foo.png", bbox_inches='tight')

The following is the output that will be obtained:


Save plot to image file