When it comes to data visualization, sometimes a single plot just isn't enough to tell the whole story. That's where multi-panel figures come in handy. They allow you to combine multiple plots into one cohesive image, making it easier to compare different aspects of your data or showcase various relationships simultaneously.
In this blog post, we'll explore how to create complex multi-panel figures using Seaborn, a powerful data visualization library built on top of Matplotlib. We'll walk through the process step-by-step, from setting up your environment to fine-tuning the final details.
Before we dive in, make sure you have the necessary libraries installed. You'll need:
You can install these using pip:
pip install seaborn matplotlib pandas
Now, let's import the libraries and set up our environment:
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd # Set the style for our plots sns.set_style("whitegrid") # Load a sample dataset tips = sns.load_dataset("tips")
Let's start with a simple 2x2 grid of plots. We'll use Seaborn's FacetGrid
to create this layout:
g = sns.FacetGrid(tips, col="time", row="sex", height=4, aspect=1.2) g.map(sns.scatterplot, "total_bill", "tip") plt.show()
This code creates a grid with four panels, showing the relationship between the total bill and tip amount, split by time of day (dinner/lunch) and sex of the customer.
Now, let's make things more interesting by customizing each plot individually:
fig, axes = plt.subplots(2, 2, figsize=(12, 10)) # Scatter plot sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", ax=axes[0, 0]) axes[0, 0].set_title("Scatter Plot: Bill vs Tip") # Box plot sns.boxplot(data=tips, x="day", y="total_bill", ax=axes[0, 1]) axes[0, 1].set_title("Box Plot: Bill by Day") # Violin plot sns.violinplot(data=tips, x="day", y="tip", ax=axes[1, 0]) axes[1, 0].set_title("Violin Plot: Tip by Day") # Bar plot sns.barplot(data=tips, x="day", y="total_bill", hue="sex", ax=axes[1, 1]) axes[1, 1].set_title("Bar Plot: Average Bill by Day and Sex") plt.tight_layout() plt.show()
This code creates a 2x2 grid with four different types of plots, each showcasing a different aspect of the tips dataset.
To make our multi-panel figure more cohesive, we can add a shared legend:
fig, axes = plt.subplots(2, 2, figsize=(12, 10)) # Create plots (same as before) # ... # Remove individual legends for ax in axes.flat: ax.get_legend().remove() # Add a shared legend handles, labels = axes[1, 1].get_legend_handles_labels() fig.legend(handles, labels, title="Sex", loc="center right", bbox_to_anchor=(1.15, 0.5)) plt.tight_layout() plt.show()
To really polish our multi-panel figure, we can adjust the spacing and add a main title:
fig, axes = plt.subplots(2, 2, figsize=(14, 12)) # Create plots (same as before) # ... # Adjust spacing between subplots plt.subplots_adjust(hspace=0.3, wspace=0.3) # Add a main title fig.suptitle("Analysis of Restaurant Tips", fontsize=16, y=1.02) plt.tight_layout() plt.show()
For more complex layouts, you might want to combine plots of different sizes. Here's how you can do that using gridspec
:
import matplotlib.gridspec as gridspec fig = plt.figure(figsize=(16, 12)) gs = gridspec.GridSpec(3, 3) # Large plot on the left ax1 = fig.add_subplot(gs[:, 0]) sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", ax=ax1) ax1.set_title("Scatter Plot: Bill vs Tip") # Four smaller plots on the right ax2 = fig.add_subplot(gs[0, 1]) sns.boxplot(data=tips, x="day", y="total_bill", ax=ax2) ax3 = fig.add_subplot(gs[0, 2]) sns.violinplot(data=tips, x="day", y="tip", ax=ax3) ax4 = fig.add_subplot(gs[1, 1:]) sns.barplot(data=tips, x="day", y="total_bill", hue="sex", ax=ax4) ax5 = fig.add_subplot(gs[2, 1:]) sns.histplot(data=tips, x="tip", hue="time", multiple="stack", ax=ax5) plt.tight_layout() plt.show()
This code creates a more complex layout with one large plot on the left and four smaller plots on the right.
Creating complex multi-panel figures with Seaborn opens up a world of possibilities for data visualization. By combining different plot types, customizing layouts, and fine-tuning details, you can create powerful visual stories that effectively communicate your data insights.
Remember, the key to great multi-panel figures is balance. Make sure each plot adds value and that the overall layout is easy to understand at a glance. With practice, you'll be creating stunning visualizations that bring your data to life!
25/09/2024 | Python
06/10/2024 | Python
26/10/2024 | Python
08/12/2024 | Python
26/10/2024 | Python
25/09/2024 | Python
22/11/2024 | Python
21/09/2024 | Python
21/09/2024 | Python
15/11/2024 | Python
25/09/2024 | Python
26/10/2024 | Python