Python Matplotlib की मूल बातें
In this page:
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?
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
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
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
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
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")
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB