Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

3.3 Advanced Matplotlib

Lesson 22 of 32 in the free Data Visualisation and Analytics notes on Siksha Sarovar, written by Rohit Jangra.

Advanced Matplotlib: Styling, Subplots & Layouts

1. Subplots: One Figure, Multiple Graphs

Often you want to compare different views side-by-side. We use plt.subplots().

ParameterMeaningExample
nrowsNumber of rows in gridnrows=2
ncolsNumber of columns in gridncols=2
figsizeTotal size of the figurefigsize=(12, 8)

Code Example:

# Create a 2x1 grid (2 rows, 1 column)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 10))

# Top Plot (Axes 1)
ax1.plot(x, y, color='blue')
ax1.set_title("Linear Trend")

# Bottom Plot (Axes 2)
ax2.bar(x, y, color='orange')
ax2.set_title("Bar View")

# Adjust spacing so titles don't overlap
plt.tight_layout()
plt.show()

2. Deep Styling Reference

Matplotlib allows granular control over aesthetics.

PropertyDescriptionExamples
color (c)Line/Marker color'r' (Red), '#FF5733' (Hex), 'navy' (Named)
linestyle (ls)Style of the line'-' (Solid), '--' (Dashed), '-.' (Dash-Dot), ':' (Dotted)
markerPoint shape'o' (Circle), 's' (Square), '^' (Triangle), 'x' (Cross)
linewidth (lw)Thickness of linelw=2.5
alphaTransparency (0 to 1)alpha=0.5 (Semi-transparent)
fontsizeText sizefontsize=14

3. Additional Plot Types

PlotFunctionWhen to Use
Histogramplt.hist(data, bins=20)Distribution of continuous data
Heatmapplt.imshow(matrix)Correlation matrices, confusion matrices
Area Chartplt.fill_between(x, y)Cumulative values over time
Horizontal Barplt.barh(y, width)When category labels are long

4. Annotations & Text

Annotating specific data points makes charts more informative:

plt.annotate('Peak Value', xy=(x_peak, y_peak),
             xytext=(x_peak+1, y_peak+10),
             arrowprops=dict(arrowstyle='->', color='red'),
             fontsize=12, color='red')

5. Legends

A legend maps colors/lines to meanings.

  1. Label: Add label="My Label" inside the plot function.
  2. Display: Call plt.legend() at the end.
  • Positioning: plt.legend(loc='upper right') or loc='best'.

6. Saving Figures

plt.savefig('my_chart.png', dpi=300, bbox_inches='tight')
# dpi=300: High resolution for publications
# bbox_inches='tight': Removes extra whitespace