xxxxxxxxxx
# Basic syntax:
plt.savefig("/path/to/output/directory/figure.png")
# Example usage:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(range(5))
plt.savefig("~/Documents/figure.png", dpi=300)
xxxxxxxxxx
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
# Control figure size or aspect ratio
fig.set_size_inches([width_val, height_val])
# Formats can be saved in lossless .png and vector .svg
fig.savefig("fig_name.jpg", quality=50) # Avoid values above 90
# Control resolution rendering
fig.savefig("fig_name.png", dpi=300)
xxxxxxxxxx
Copied to clipboard
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
xxxxxxxxxx
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1, 1, 100)
y = x**2
fig, ax = plt.subplots()
ax.plot(x, y)
fig.savefig('fig1.pdf')
plt.show()
fig.savefig('fig2.pdf')