Theory
Numbers table कोई नहीं पढ़ता
ResultDesk का describe() output statistically perfect है, और principal की आँखें चार सेकंड में glaze हो जाती हैं।
फिर आप एक अकेली नीचे की तरफ़ ढलती line दिखाते हैं: तीन internal tests में DBMS averages गिरते हुए, और कमरा आगे झुक जाता है। वही data। अलग container।
Insaan pictures को tables से सैकड़ों गुना तेज़ process करते हैं। Unit 5 उसी conversion के बारे में है, और इसका engine Python की standard plotting library है: matplotlib।
Theory
एक graph paper workbench
pyplot को एक graph-paper workbench समझिए:
plt.plot() current sheet पर एक line pencil करता है। इसे फिर call कीजिए: एक और line, वही sheet। title/xlabel/ylabel headings लिखते हैं। legend() colour key पिन करता है। और कुछ भी workbench से बाहर नहीं जाता जब तक plt.show() sheet को उठाकर नहीं दिखाता।
subplot() power move है: sheet को एक grid में rule कीजिए और हर cell में अलग chart खींचिए।
Theory
pyplot vocabulary
import matplotlib.pyplot as plt: universal alias (एक बार pip install matplotlib)।
- plt.plot(x, y): points से एक 2-D line खींचिए।
- plt.title / plt.xlabel / plt.ylabel: chart और दोनों axes को नाम दीजिए।
- plt.legend(): key: हर plot के
label=argument से बना। - plt.show(): figure render कीजिए: scripts इसके बिना कुछ भी नहीं दिखाते।
- plt.savefig('report.png'): show की जगह save कीजिए।
Helpers जो आप पहले से जानते हैं: range(n) x positions 0..n-1 बनाता है, len(df) rows गिनता है, df.columns बताता है क्या plottable है।
Practical
पहला plot, फिर एक 1x2 grid
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('marks.csv')
# one chart: DBMS scores across roll numbers
dbms = df[df['subject'] == 'DBMS']
plt.plot(dbms['roll'], dbms['score'], label='DBMS')
plt.plot(dbms['roll'], dbms['score'] * 0 + dbms['score'].mean(),
label='class mean') # a flat reference line
plt.title('DBMS scores by roll')
plt.xlabel('Roll number')
plt.ylabel('Score')
plt.legend() # uses the label= texts
plt.show() # nothing appears without this
# two charts on ONE figure: subplot(rows, cols, index), index from 1
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1) # left cell
plt.plot(range(len(dbms)), dbms['score'])
plt.title('By position (range/len)')
plt.subplot(1, 2, 2) # right cell
plt.plot(dbms['roll'], dbms['score'])
plt.title('By roll number')
plt.show()
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Blank-window mystery
एक student का script एक ख़ूबसूरत plot बनाता है: plot(), title(), labels, legend()... और इसे run करने पर कुछ नहीं print होता, कुछ नहीं दिखता, कोई error नहीं। tap करने से पहले: कौन सी एक line missing है, और वे पहले वाले सारे calls असल में क्या कर रहे थे?
Show the answer
plt.show() missing है। पहले का हर call एक in-memory figure पर draw कर रहा था: वह workbench sheet जो कभी उठाकर नहीं दिखाई गई। Script ख़त्म होता है, figure चुपचाप discard हो जाता है।
(Jupyter notebooks में figures अक्सर automatically दिखते हैं, जो बिल्कुल वही कारण है कि students plain scripts में show() भूल जाते हैं: exam का जवाब है: pyplot figures memory में बनाता है; show() उन्हें render करता है।)
Quiz
plt.subplot(2, 2, 1) call किया जाता है। अगला plot figure के किस cell में जाता है?
- Top-left: एक 2x2 grid, और index 1 row-wise 1 से गिनते हुए FIRST cell है
- Bottom-right: index उल्टा गिनता है
- Top-left, क्योंकि index 0 का मतलब है first
- यह error देता है: subplot को 0 से index चाहिए
Show the answer
Top-left: एक 2x2 grid, और index 1 row-wise 1 से गिनते हुए FIRST cell है
subplot(rows, cols, index) एक rows x cols grid rule करता है और cell number index select करता है, 1 से गिनते हुए, बाएँ से दाएँ, ऊपर से नीचे: (2,2,1) top-left है, (2,2,2) top-right, (2,2,3) bottom-left, (2,2,4) bottom-right। 1-से counting उस Python instinct को trip करती है जो zero-based lists पर trained है: options C और D बिल्कुल वही instinct हैं, और बिल्कुल वही lost mark।
Watch out
तीन chart-killers
कोई plt.show() नहीं: chart की जगह silence (scripts में, notebooks में नहीं)।
कोई label= arguments नहीं: plt.legend() एक empty box खींचता है या warn करता है: key में जो भी plot() चाहिए उसे label='...' चाहिए।
Unlabelled axes: examiners बिना title/xlabel/ylabel वाले chart पर marks काटते हैं, और वे सही हैं: एक unlabelled chart एक अफ़वाह है, report नहीं। Syllabus ax.set_title() भी नाम देता है: वही idea की object-style spelling, fig, ax = plt.subplots() के साथ इस्तेमाल होती है।
Theory
एक pattern, आगे चार charts
इस unit का बाक़ी हर lesson एक verb बदला हुआ वही SKELETON है: point clouds के लिए scatter(), trends के लिए plot(), distributions के लिए hist(), category comparisons के लिए bar()। Skeleton एक बार सीखिए (draw, title, axes label, legend, show) और अगले चार lessons हर एक एक नया verb हैं plus कब इस्तेमाल करना है इसकी judgement।
Summary
Key takeaways
- import matplotlib.pyplot as plt; pandas जैसा pip-installed।
- Skeleton: plot(x, y, label=...), title(), xlabel(), ylabel(), legend(), show()।
- Scripts में plt.show() के बिना कुछ भी render नहीं होता; savefig() एक file में लिखता है।
- बार-बार plot() calls एक figure पर lines layer करती हैं; legend() label= से उन्हें key करता है।
- subplot(rows, cols, index) charts tile करता है: index 1 से गिनता है, row-major।
- range() और len() x positions बनाते हैं; DataFrame columns सीधे plot होते हैं।
- Memory hook: sheet पर pencil, show() से उसे उठाकर दिखाइए।