Python Matplotlib क्या है? Line, Bar, Pie & Scatter Chart in Hindi | RBSE Class 12

इस लेख में: Matplotlib क्या है, इसे install और import कैसे करते हैं, Line Chart, Bar Chart, Pie Chart और Scatter Plot कैसे बनाते हैं, तथा title, labels, legend, grid और chart को save करने का तरीका examples के साथ समझेंगे।

Matplotlib क्या है?

Matplotlib Python की एक popular library है जिसका उपयोग data को graphs और charts के रूप में visualize करने के लिए किया जाता है।

Data को केवल numbers या tables के रूप में देखने की बजाय graph के रूप में दिखाने से trends और comparisons को समझना आसान हो सकता है।

Important: Matplotlib Python की standard library का हिस्सा नहीं है। इसे अलग से install किया जा सकता है।

Matplotlib का उपयोग क्यों किया जाता है?

  • Data को graphically represent करने के लिए।
  • Trends को समझने के लिए।
  • Different values की comparison के लिए।
  • Charts और graphs बनाने के लिए।
  • Data analysis और visualization में।

Matplotlib Install कैसे करें?

pip install matplotlib

Matplotlib Import कैसे करें?

import matplotlib.pyplot as plt

यहाँ pyplot module को सामान्यतः plt alias के साथ use किया जाता है।

पहला Line Chart

Line chart बनाने के लिए plt.plot() का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.show()

Chart में Title लगाना

Chart का title बताने के लिए plt.title() का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.title("Student Performance")

plt.show()

X-axis और Y-axis Labels

X-axis और Y-axis को label देने के लिए xlabel() और ylabel() methods का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.title("Student Performance")
plt.xlabel("Test")
plt.ylabel("Marks")

plt.show()

Grid लगाना

Graph को पढ़ने में आसान बनाने के लिए grid दिखाई जा सकती है।

plt.plot(x, y)

plt.title("Student Performance")
plt.xlabel("Test")
plt.ylabel("Marks")

plt.grid()

plt.show()

Line Chart में Marker

Data points को highlight करने के लिए marker का उपयोग किया जा सकता है।

plt.plot(x, y, marker="o")

plt.show()

Bar Chart क्या है?

Bar Chart का उपयोग अलग-अलग categories की values की comparison करने के लिए किया जा सकता है।

Matplotlib में bar chart बनाने के लिए plt.bar() का उपयोग किया जाता है।

import matplotlib.pyplot as plt

subjects = ["Hindi", "English", "Math", "Computer"]
marks = [75, 82, 90, 88]

plt.bar(subjects, marks)

plt.title("Subject Wise Marks")
plt.xlabel("Subjects")
plt.ylabel("Marks")

plt.show()

Horizontal Bar Chart

Horizontal bar chart बनाने के लिए plt.barh() का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

subjects = ["Hindi", "English", "Math", "Computer"]
marks = [75, 82, 90, 88]

plt.barh(subjects, marks)

plt.title("Subject Wise Marks")

plt.show()

Pie Chart क्या है?

Pie Chart किसी total के अलग-अलग हिस्सों या proportions को दिखाने के लिए उपयोगी होता है।

Pie chart बनाने के लिए plt.pie() का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

subjects = ["Hindi", "English", "Math", "Computer"]
marks = [75, 82, 90, 88]

plt.pie(marks, labels=subjects)

plt.title("Marks Distribution")

plt.show()

Pie Chart में Percentage

Pie chart में percentage दिखाने के लिए autopct parameter का उपयोग किया जा सकता है।

plt.pie(
    marks,
    labels=subjects,
    autopct="%1.1f%%"
)

plt.title("Marks Distribution")

plt.show()

Scatter Plot क्या है?

Scatter Plot दो numerical variables के बीच relationship या distribution को देखने के लिए उपयोगी होता है।

Matplotlib में इसे बनाने के लिए plt.scatter() का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

hours = [1, 2, 3, 4, 5, 6]
marks = [45, 50, 58, 65, 72, 80]

plt.scatter(hours, marks)

plt.title("Study Hours and Marks")
plt.xlabel("Study Hours")
plt.ylabel("Marks")

plt.show()

Multiple Lines एक Graph में

एक ही graph में एक से अधिक lines भी बनाई जा सकती हैं।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

python_marks = [60, 70, 75, 85, 90]
math_marks = [55, 65, 72, 80, 88]

plt.plot(x, python_marks, label="Python")
plt.plot(x, math_marks, label="Math")

plt.title("Subject Performance")
plt.xlabel("Test")
plt.ylabel("Marks")

plt.legend()

plt.show()

Legend क्या है?

Legend graph में अलग-अलग lines या data series की पहचान करने में मदद करता है।

plt.legend()

Chart को Image के रूप में Save करना

Chart को file के रूप में save करने के लिए savefig() method का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.title("Sample Graph")

plt.savefig("graph.png")

plt.show()

Figure Size बदलना

Graph का size बदलने के लिए figsize parameter का उपयोग किया जा सकता है।

import matplotlib.pyplot as plt

plt.figure(figsize=(8, 5))

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y)

plt.show()

Line Style और Marker

Line chart में line style और marker को customize किया जा सकता है।

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(
    x,
    y,
    linestyle="--",
    marker="o"
)

plt.show()

Matplotlib के Important Functions

Function उपयोग
plt.plot() Line chart बनाने के लिए
plt.bar() Vertical bar chart
plt.barh() Horizontal bar chart
plt.pie() Pie chart
plt.scatter() Scatter plot
plt.title() Chart title
plt.xlabel() X-axis label
plt.ylabel() Y-axis label
plt.legend() Legend दिखाने के लिए
plt.grid() Grid दिखाने के लिए
plt.show() Chart display करने के लिए
plt.savefig() Chart save करने के लिए

Line Chart और Bar Chart में अंतर

Line Chart Bar Chart
Trend दिखाने में उपयोगी Categories की comparison में उपयोगी
plt.plot() का उपयोग plt.bar() का उपयोग
Continuous या ordered data के लिए उपयोगी Category-wise values के लिए उपयोगी

Pie Chart और Scatter Plot

Chart मुख्य उपयोग
Pie Chart Total में parts/proportions दिखाने के लिए
Scatter Plot दो numerical variables के relationship/distribution को देखने के लिए

Practical Example — Student Marks Visualization

import matplotlib.pyplot as plt

subjects = ["Hindi", "English", "Math", "Python"]
marks = [78, 82, 91, 88]

plt.bar(subjects, marks)

plt.title("Student Marks")
plt.xlabel("Subjects")
plt.ylabel("Marks")

plt.grid(axis="y")

plt.show()

RBSE Class 12 Important Questions

  1. Matplotlib क्या है?
  2. Matplotlib का उपयोग क्यों किया जाता है?
  3. Matplotlib को Python में कैसे import करते हैं?
  4. Line Chart क्या है?
  5. Bar Chart क्या है?
  6. Pie Chart क्या है?
  7. Scatter Plot क्या है?
  8. plt.plot() का उपयोग क्या है?
  9. plt.bar() का उपयोग क्या है?
  10. plt.pie() का उपयोग क्या है?
  11. plt.scatter() का उपयोग क्या है?
  12. plt.title() का उपयोग क्या है?
  13. xlabel() और ylabel() में क्या अंतर है?
  14. plt.legend() क्या करता है?
  15. plt.grid() का उपयोग क्यों किया जाता है?
  16. plt.show() का उपयोग क्या है?
  17. savefig() method क्या करता है?
  18. Line Chart और Bar Chart में अंतर लिखिए।
  19. Pie Chart का उपयोग कब किया जाता है?
  20. Scatter Plot का उपयोग क्यों किया जाता है?

Frequently Asked Questions (FAQs)

Matplotlib क्या है?

Matplotlib Python की एक plotting और visualization library है जिसका उपयोग विभिन्न प्रकार के charts और graphs बनाने के लिए किया जाता है।

Matplotlib को import कैसे करते हैं?

सामान्यतः import matplotlib.pyplot as plt का उपयोग किया जाता है।

Line Chart बनाने के लिए कौन-सा function उपयोग होता है?

Line chart बनाने के लिए सामान्यतः plt.plot() का उपयोग किया जाता है।

Bar Chart बनाने के लिए कौन-सा function उपयोग होता है?

Vertical bar chart के लिए plt.bar() और horizontal bar chart के लिए plt.barh() का उपयोग किया जा सकता है।

Pie Chart बनाने के लिए कौन-सा function उपयोग होता है?

Pie chart बनाने के लिए plt.pie() का उपयोग किया जाता है।

Chart को image में कैसे save करते हैं?

Chart को image file में save करने के लिए plt.savefig() का उपयोग किया जा सकता है।

अब अगला Topic पढ़ें

RBSE Python Study Tip: Matplotlib में सबसे पहले plot(), bar(), pie() और scatter() को समझें। इसके बाद title, xlabel, ylabel, legend, grid और savefig() की practice करें। Exam में function का नाम और उसका purpose विशेष रूप से याद रखें।
Official Reference:

Matplotlib की विस्तृत जानकारी के लिए official documentation देखें।

Matplotlib Documentation