← Back to Python Course | Chapter 13: Data Science & Web | Lesson 5 of 14

Python Matplotlib की मूल बातें

Matplotlib आपके numbers से charts और graphs बनाता है, जैसे scores की list को एक picture में बदलना। आप titles, labels, और colors जोड़ सकते हैं।
Syntax
python
import matplotlib.pyplot as plt

plt.plot(x_values, y_values)
plt.title("title")
plt.show()

Matplotlib क्या है?

Matplotlib Python की foundational 2D plotting library है, और इसका pyplot मॉड्यूल charts को step by step बनाने के लिए MATLAB से प्रेरित एक interface देता है।

इस tutorial के examples non-interactive Agg backend इस्तेमाल करते हैं, जो कोई graphical window खोलने की बजाय सीधे image files में render करता है, ताकि code बिना किसी display के भी सुरक्षित रूप से चले।

उदाहरण: What is Matplotlib?

python
import matplotlib
matplotlib.use("Agg")  # non-interactive backend, renders to a file instead of a window
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])  # x values, y values
plt.savefig("chart.png")
print("Chart saved")

Labels और Titles को Customize करना

plt.title(), plt.xlabel(), और plt.ylabel() किसी plot के आस-पास descriptive text जोड़ते हैं, और plt.legend() एक key जोड़ता है जो चार्ट में एक से ज़्यादा line या series होने पर हर एक की पहचान बताती है।

इनके बिना कोई chart अक्सर उसे बनाने वाले के अलावा किसी और को समझ नहीं आता, क्योंकि axes और series खुद कोई context नहीं देते।

उदाहरण: Customizing Labels and Titles

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], label="Sales")
plt.title("Monthly Sales")  # chart title
plt.xlabel("Month")  # x-axis label
plt.ylabel("Amount")  # y-axis label
plt.legend()  # shows the "Sales" label as a key
plt.savefig("chart.png")

Line Customization

plt.plot() को दिए जाने वाले color, linewidth, और linestyle arguments किसी line की visual दिखावट control करते हैं -- जिससे आप एक ही chart में कई series को सिर्फ उनके आकार से ज़्यादा में अलग कर सकते हैं, जो खासकर तब मायने रखता है जब किसी plot में तीन या ज़्यादा lines एक-दूसरे पर overlap कर रही हों।

उदाहरण: Line Customization

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], color="red", linewidth=2, linestyle="--")  # customizes the line's appearance
plt.savefig("chart.png")
print("Styled line saved")

Gridlines बनाना

plt.grid(True) plotting area पर हल्की reference lines overlay करता है, जिससे किसी viewer के लिए bare axis tick marks के बीच मानसिक रूप से interpolate करने की बजाय एक नज़र में approximate values पढ़ना कहीं आसान हो जाता है।

उदाहरण: Creating Gridlines

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.grid(True)  # overlays reference lines for easier reading
plt.savefig("chart.png")

Figures को Save करना

plt.savefig('filename.png') मौजूदा figure को disk पर एक image file के रूप में लिखता है, और आपके दिए extension के आधार पर PNG, PDF, और SVG जैसे formats support करता है।

यही तरीका है जिससे generate किए गए charts केवल एक interactive window में दिखने की बजाय reports, dashboards, या web pages में embed होते हैं।

उदाहरण: Saving Figures

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.savefig("chart.png")  # writes the current figure to disk as an image
print("Saved as PNG")
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. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.