Figures को Save करना
savefig current figure को PNG, PDF या SVG file में लिखता है।
In this page:
Syntax
import matplotlib.pyplot as plt
df.plot()
plt.savefig("chart.png", dpi=150)
plt.show()
Saving figures
plt.savefig("chart.png", dpi=150) active figure को save करता है; इसे plt.show से पहले call करें, जो figure को clear कर देता है। Format file extension का अनुसरण करता है। bbox_inches="tight" अतिरिक्त whitespace हटाता है। यहां हम disk के बजाय memory में save करते हैं।
Note:
रंगीन backgrounds पर PNGs के लिए transparent=True का उपयोग करें।
उदाहरण: Saving figures
import io
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd
ax = pd.Series([1, 3, 2]).plot(title="Demo")
for fmt in ["png", "svg"]:
buf = io.BytesIO()
plt.savefig(buf, format=fmt, dpi=100, bbox_inches="tight")
print(fmt, "saved:", buf.getbuffer().nbytes > 0)
# Output:
# png saved: True
# svg saved: True
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- show के बाद savefig call करना
- कम dpi से धुंधली images बनना
- bbox_inches के बिना कटे हुए labels
चैप्टर सारांश
- savefig files लिखता है
- Format extension का अनुसरण करता है
- dpi resolution सेट करता है
- show से पहले इसे call करें
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: