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

Python डेटा विज़ुअलाइज़ेशन

Data visualization numbers को bars, dots और slices जैसी pictures में बदल देता है, ताकि patterns आसानी से दिखें। यह किसी लंबी list पढ़ने की बजाय एक picture बनाने जैसा है।
Syntax
python
import matplotlib.pyplot as plt

plt.bar(categories, values)
plt.xlabel("label")
plt.ylabel("label")
plt.show()

Bar Charts बनाना

plt.bar() vertical bars खींचता है (या horizontal के लिए plt.barh()) जिनकी height हर category की value के अनुपात में होती है, जिससे मुट्ठी भर discrete categories की तुलना करते समय bar charts स्वाभाविक पसंद बन जाते हैं।

उदाहरण: Drawing Bar Charts

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

plt.bar(["A", "B", "C"], [10, 20, 15])  # bar height proportional to each value
plt.savefig("bar.png")

Scatter Plots बनाना

plt.scatter() हर data point को उसकी x और y values के हिसाब से एक dot के रूप में अलग-अलग plot करता है, बिना उन्हें किसी line से जोड़े। इससे scatter plots दो numeric variables के आपस में correlated दिखने या न दिखने की visually जाँच करने का standard तरीका बन जाते हैं।

उदाहरण: Drawing Scatter Plots

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

plt.scatter([1, 2, 3], [4, 1, 5])  # each point plotted individually, no connecting line
plt.savefig("scatter.png")

Histograms बनाना

plt.hist() continuous numeric data को bins कहे जाने वाले ranges के एक set में बाँटता है और हर bin की count के लिए एक bar खींचता है, जिससे distribution का overall आकार -- चाहे वो लगभग symmetric हो, skewed हो, या कई peaks वाला हो -- ऐसे तरीके से सामने आता है जो numbers की raw list कभी नहीं दिखा सकती।

उदाहरण: Drawing Histograms

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

plt.hist([1, 2, 2, 3, 3, 3, 4])  # sorts values into bins and draws a bar per bin
plt.savefig("hist.png")

Pie Charts बनाना

plt.pie() हर category को total में उसके हिस्से के अनुसार आकार वाले circle के एक proportional wedge के रूप में खींचता है।

Pie charts तब सबसे अच्छे काम करते हैं जब categories कम हों और साफ़ तौर पर किसी meaningful whole में जुड़ती हों; ज़्यादा slices chart को गड्डमड्ड और सटीक तुलना करने में मुश्किल बना देते हैं।

उदाहरण: Drawing Pie Charts

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

plt.pie([30, 20, 50], labels=["A", "B", "C"])  # each wedge sized by its share of the total
plt.savefig("pie.png")

Subplots बनाना

plt.subplots(rows, cols) एक ही figure के भीतर independent axes की एक grid बनाता है, जिससे आप कई related charts को सीधी तुलना के लिए साथ-साथ दिखा सकते हैं, अलग-अलग images बनाने की बजाय जिन्हें viewer को मानसिक रूप से एक-सीध में लाना पड़ता।

उदाहरण: Creating Subplots

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

fig, axes = plt.subplots(1, 2)  # 1 row, 2 columns of independent charts
axes[0].plot([1, 2, 3])
axes[1].bar(["A", "B"], [5, 10])
plt.savefig("subplots.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.