Matplotlib fill_between: Shading Between Two Curves

Matplotlib fill_between: three sine plots showing a basic fill, conditional shading with gaps, and the same with interpolate set to True

fill_between shades the area between two curves in Matplotlib. Give it the x values and one y series and it fills down to zero; give it two and it fills the gap between them. The two arguments that cause trouble are where=, for shading only part of the range, and interpolate=True, which you almost always … Read more >>

Use Matplotlib set_yticklabels for Custom Y-Axis Labels in Python

set_yticklabels

When I first started visualizing data with Matplotlib over a decade ago, customizing tick labels on the y-axis was always a bit tricky. In this article, I’ll share practical ways to use set_yticklabels in Matplotlib to customize your y-axis labels effectively. You’ll learn multiple methods to tweak labels, making your data visualization clearer and more … Read more >>

Matplotlib tight_layout(): Fix Overlapping Subplots and Labels

Matplotlib tight_layout guide: a 2x2 grid of subplots with titles and axis labels that no longer overlap

plt.tight_layout() automatically adjusts the spacing around and between subplots so that titles, axis labels and tick labels do not overlap or get cut off. Call it once after you have added all titles and labels and before plt.show() or savefig(). This Matplotlib tight_layout guide shows what it changes, its pad, w_pad, h_pad and rect parameters, … Read more >>

Matplotlib tick_params(): Customize Ticks and Tick Labels

Matplotlib tick_params example: major and minor ticks with different lengths and colours and red y tick labels

tick_params() changes how ticks, tick labels and gridlines look in Matplotlib: their direction, length, width, colour, label size, rotation and which sides of the plot show them. Call it on an axes, ax.tick_params(…), or use plt.tick_params(…) for the current axes. It changes existing and future ticks, so you do not need to recreate them. This … Read more >>

How to Add and Customize the X-Axis Label in Matplotlib

Matplotlib x-axis label example: a bar chart with a bold dark blue x-axis label set with set_xlabel

To add an x-axis label in Matplotlib, call plt.xlabel(“Label”), or ax.set_xlabel(“Label”) when you work with an Axes object (for example after plt.subplots()). Both accept text styling such as fontsize, color and fontweight, plus labelpad and loc for its position. This guide covers the matplotlib x-axis label from the basics to changing tick labels, showing every … Read more >>

How to Create Multiple Bar Charts in Matplotlib (Grouped, Side by Side)

Multiple bar chart in Matplotlib: grouped bars for four regions side by side for five products

To create a multiple bar chart in Matplotlib (also called a grouped, double or side-by-side bar chart), draw each series with ax.bar() at positions shifted by the bar width, for example x – width/2 and x + width/2, then put the group labels in the middle with ax.set_xticks(x, labels). With pandas, df.plot(kind=”bar”) draws a grouped … Read more >>

Add Legends in Matplotlib Scatter Plots

matplotlib scatter legend

I’ve worked extensively with data visualization libraries, and Matplotlib remains one of my go-to tools. Scatter plots are particularly useful when you want to visualize the relationship between two variables, especially in fields like marketing analytics or healthcare data analysis, here in the USA. One key element that often gets overlooked but is crucial for … Read more >>

How to Create a 3D Scatter Plot in Python Matplotlib

3D scatter plot in Python Matplotlib with points coloured by value, sized by a fourth variable and a colorbar

To make a 3D scatter plot in Python with Matplotlib, create a 3D axes with fig.add_subplot(projection=”3d”) and call ax.scatter(x, y, z). The same scatter() options you know from 2D plots (c, s, cmap, marker, label) work in 3D. This guide covers the basic Matplotlib 3D scatter plot, colour and size by value, groups with a … Read more >>

How to Create a Stacked Bar Chart in Matplotlib (Python)

Stacked bar chart in Matplotlib with value labels inside each segment and totals on top of every bar

To create a stacked bar chart in Matplotlib, draw the first series with plt.bar(), then draw each next series with the bottom argument set to the height of everything below it. For a horizontal stacked bar chart, use plt.barh() with left instead. This guide builds stacked bar charts in Python step by step: two series, … Read more >>

Matplotlib Two Y Axes in Python

matplotlib two y axes

I’ve often faced the challenge of visualizing data that involves two different scales or units on the same plot. This is especially common in fields like finance, economics, or marketing analytics, where you might want to compare stock prices with trading volume, or GDP growth with unemployment rates, all in one clear, concise graph. Matplotlib, … Read more >>