Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to plot a non-square Seaborn jointplot or JointGrid? (Matplotlib)
Seaborn's jointplot() creates square plots by default, but you can create non-square (rectangular) plots by adjusting the figure dimensions using set_figwidth() and set_figheight() methods.
Basic Non-Square Jointplot
Here's how to create a rectangular jointplot by modifying the figure dimensions ?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Generate sample data
np.random.seed(42)
x_data = np.random.randn(1000)
y_data = 0.2 * np.random.randn(1000) + 0.5
# Create DataFrame
df = pd.DataFrame({'x': x_data, 'y': y_data})
# Create jointplot
jp = sns.jointplot(x="x", y="y", data=df, height=4,
joint_kws={'color': 'blue'})
# Make it non-square by setting custom width and height
jp.fig.set_figwidth(8)
jp.fig.set_figheight(4)
plt.show()
Using JointGrid for More Control
For advanced customization, use JointGrid directly ?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Generate sample data
np.random.seed(42)
x_data = np.random.randn(500)
y_data = 0.3 * x_data + np.random.randn(500) * 0.5
df = pd.DataFrame({'x': x_data, 'y': y_data})
# Create JointGrid with custom aspect ratio
grid = sns.JointGrid(data=df, x="x", y="y", height=4)
# Set custom figure dimensions
grid.fig.set_figwidth(10)
grid.fig.set_figheight(5)
# Plot the data
grid.plot_joint(sns.scatterplot, alpha=0.6, color='green')
grid.plot_marginals(sns.histplot, kde=True, alpha=0.7)
plt.show()
Different Aspect Ratios
You can create various rectangular shapes by adjusting the width-to-height ratio ?
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Generate correlated data
np.random.seed(123)
x_data = np.random.normal(0, 1, 800)
y_data = 0.6 * x_data + np.random.normal(0, 0.8, 800)
df = pd.DataFrame({'x': x_data, 'y': y_data})
# Create wide horizontal plot
jp = sns.jointplot(x="x", y="y", data=df, kind="reg", height=3)
jp.fig.set_figwidth(12) # Wide
jp.fig.set_figheight(4) # Short
plt.suptitle("Wide Horizontal Jointplot", y=1.02)
plt.show()
Key Parameters
| Method | Purpose | Usage |
|---|---|---|
set_figwidth() |
Set plot width | jp.fig.set_figwidth(8) |
set_figheight() |
Set plot height | jp.fig.set_figheight(4) |
height parameter |
Initial plot size | sns.jointplot(height=4) |
Conclusion
Use set_figwidth() and set_figheight() on the jointplot's figure object to create non-square plots. JointGrid provides more control for complex customizations with rectangular aspect ratios.
Advertisements
