Contents
Subplots and Multi-Chart Layouts
Sometimes one chart is not enough. Placing several charts side by side lets you compare views and tell a fuller story. This topic shows how to build grids of charts in a neat and controlled way.
a. Creating Subplots in Matplotlib
The plt.subplots() function creates a figure with a grid of axes. You pass the number of rows and columns, and it returns the figure and an array of axes.
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
axes[0].hist(df["age"], bins=15)
axes[0].set_title("Age")
axes[1].boxplot(df["salary"])
axes[1].set_title("Salary")
plt.tight_layout()
plt.show()Each axes object can be customized separately. The tight_layout() call adjusts spacing so that titles and labels do not overlap.
Use subplots to show different views of the same data, or to compare the same chart across groups.
b. Sharing Axes Across Charts
When you compare charts, they should use the same scale. Otherwise, the viewer may think two values are similar when they are not. The sharex and sharey options keep the axes aligned.
fig, axes = plt.subplots(1, 3, figsize=(15, 4), sharey=True)Shared axes also save space, since labels appear only once. This makes the grid cleaner and easier to read.
Use shared axes when charts show the same measure. Use separate axes only when the scales are truly different, and make that clear with labels.
c. Using GridSpec for Custom Layouts
Sometimes you want charts of different sizes, such as one large chart with two small ones beside it. GridSpec gives you control over how the figure is divided.
from matplotlib.gridspec import GridSpec
fig = plt.figure(figsize=(12, 6))
gs = GridSpec(2, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[1, 1:])In this example, the first chart spans the whole top row. The two charts below take one and two columns each. This lets you design a layout that fits your story.
Plan your layout on paper first. A clear arrangement guides the eye from the main chart to the supporting details.
d. Building Faceted Charts with Seaborn
Faceting splits your data into groups and draws one small chart for each group, all with the same scale. It is excellent for comparing patterns across categories.
sns.relplot(data=tips, x="total_bill", y="tip", col="day", kind="scatter")
sns.displot(data=tips, x="total_bill", col="time", kde=True)The col option creates a separate chart for each value in a column, and row does the same by rows. You can use both to create a full grid.
Faceting often works better than putting many colored groups on one crowded chart. Each small chart stays simple, and the shared scale makes comparison fair.
Data Visualization Tutorial
Wesline Rinisha








