Matplotlib — The Python Plotting Library Every Data Scientist Must Know
When I first started learning Data Visualization in Python, the very first library I opened was Matplotlib. And honestly — it looked complicated. Too many lines of code just to draw a simple line. But once I understood the logic behind it, everything clicked.
In this post I am going to share everything I learned about Matplotlib — from basic plots all the way to 3D visualizations. If you are just starting out, this is the right place to begin.
What is Matplotlib?
Matplotlib is the most fundamental data visualization library in Python. Almost every other plotting library — like Seaborn and Pandas .plot() — is built on top of it. Think of it as the foundation of Python plotting.
It gives you full control over every element of your chart — colors, sizes, labels, grids, fonts, everything. That is what makes it powerful, and also what makes it feel overwhelming at first.
💡 If you understand Matplotlib well, every other visualization library becomes much easier to learn — because they all follow the same logic underneath.
What is the basic structure of a Matplotlib chart?
Before jumping into chart types, it helps to understand two important terms:
fig — this is the whole canvas. Think of it as the blank paper you are drawing on.
ax — this is the actual plot inside the canvas. One fig can have multiple ax (subplots).
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
Once I understood this, everything else in Matplotlib made sense.
Basic plots
I started with the most common chart types. Here is what I learned and when to use each one.
Line Plot — Use this for trends over time. I used it to compare Rohit Sharma and Virat Kohli's IPL career runs across seasons. You can see the rise and fall clearly in a line chart.
Scatter Plot — Use this when looking at the relationship between two numerical variables. I plotted batting average vs strike rate for IPL batters — scatter plots are perfect for this kind of analysis.
Bar Chart — Use this to compare categories. I learned vertical bars, horizontal bars, multiple bars side by side, and stacked bars. Bar charts are the most used chart type in data analysis.
Histogram — Use this to see the distribution of a single variable. I plotted Virat Kohli's match run distribution — how many times did he score 0-20, 20-40, 40-60, and so on. Histograms answer that question perfectly.
Pie Chart — Use this to show proportions of a whole. I learned single and multiple pie charts. One important lesson — do not overuse pie charts. They get confusing when there are too many slices.
Customization — making charts look good
A plain chart is fine for exploring data. But a customized chart is what you show to others. Here is what I learned:
Colors using hex codes like
#FF5733and colormaps likeviridisandplasmaLine styles — solid, dashed, dotted — and controlling
linewidthMarker shapes and
markersizefor scatter and line plotsAdding titles, axis labels, and legends with proper placement
Controlling axis ranges using
plt.xlim()andplt.ylim()Adding grid lines using
plt.grid(True)to make charts easier to read
plt.plot(x, y, color='#1D9E75', linestyle='--', linewidth=2, marker='o', markersize=5)
plt.title('My Chart')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.grid(True)
plt.show()
Advanced features
After getting comfortable with basics, I moved into more powerful Matplotlib features.
Annotations — You can add text directly on a chart using plt.text(). This is useful when you want to highlight a specific data point — like the highest score in a match or a spike in stock price.
Reference Lines — plt.axhline() adds a horizontal reference line and plt.axvline() adds a vertical one. I used these to mark average values on charts — makes patterns much easier to spot.
Subplots — Instead of one chart at a time, you can create a grid of charts in a single figure using plt.subplots(). This is perfect for comparing multiple things side by side.
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].plot(x1, y1)
axes[1].bar(categories, values)
plt.tight_layout()
Figure Size — figsize controls how big your chart appears. Getting this right makes your plots look much more professional.
Colored Scatter Plots — Using cmap and alpha you can color scatter points by category and control their transparency. Very useful when points overlap.
Heatmaps and Contour Plots
One of the most interesting things I learned was building heatmaps using plt.imshow(). A heatmap shows intensity of values across a grid — I used it to analyze boundary deliveries per over in IPL ball-by-ball data. The patterns you discover are genuinely surprising.
Contour plots using ax.contour() and ax.contourf() visualize 3D data on a 2D surface — like a topographic map. They look complex but the code is simple once you understand the idea.
3D Visualizations
Matplotlib can also create 3D plots using projection='3d'. I learned three types:
3D Scatter Plot — Three variables plotted in 3D space. Great for spotting clusters in data.
3D Line Plot — A line traced through 3D space using ax.plot3D.
3D Surface Plot — ax.plot_surface with the viridis colormap. Honestly one of the most visually impressive things you can make with just a few lines of Python.
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()
Plotting directly from Pandas
One thing that surprised me — you do not always need to write plt.plot() manually. Pandas DataFrames have a built-in .plot() method that uses Matplotlib underneath. So you can go from a DataFrame to a chart in one line:
df['runs'].plot(kind='bar', title='Runs per Match')
plt.show()
This is incredibly useful during data exploration when you just want a quick look without writing a full chart from scratch.
My honest experience
Matplotlib has a learning curve. When I first saw fig, ax = plt.subplots() I had no idea what fig and ax meant. But once I understood that fig is the whole canvas and ax is the individual plot inside it — everything became much clearer.
My advice: do not try to memorize the syntax. Understand what each part does, and look up the rest when you need it. The Matplotlib documentation is actually very good once you know what you are searching for.
The real learning happened when I stopped copying code and started applying it to real datasets — IPL data, stock prices, and the Iris dataset. That is when Matplotlib went from confusing to genuinely fun.
Key Takeaways
Matplotlib is the foundation of Python plotting — every other library builds on it
fig is the canvas, ax is the plot — understand this and everything else makes sense
I learned line, scatter, bar, histogram, and pie charts for different data situations
Customization — colors, markers, grids, labels — makes charts professional
Subplots, annotations, and reference lines take your charts to the next level
Heatmaps and 3D plots are more powerful than they look
Pandas .plot() is a quick way to visualize data without full Matplotlib syntax
What is next in this series?
What is Data Visualization and why it matters
Matplotlib — The foundation of Python plotting — you are here
Seaborn — Beautiful charts with less code
Plotly — Interactive charts in Python
Matplotlib vs Seaborn vs Plotly — Which to use?
If this helped you, drop a reaction and follow along — more posts coming soon! 🚀
