sns.relplot()
A flexible function that combines scatter and line plots with enhanced features.
The sns.relplot()
function is a versatile tool for creating relational plots in Seaborn. It provides a high-level interface for creating scatter plots and line plots with additional features like faceting and subplots.
Syntax
sns.relplot(
data=None,
*,
x=None,
y=None,
kind='scatter', or 'line'
hue=None,
size=None,
style=None,
col=None,
row=None,
col_wrap=None,
row_order=None,
col_order=None,
palette=None,
size_order=None,
sizes=None,
alpha=None,
markers=None,
dashes=True,
legend='auto',
height=5,
aspect=1,
facet_kws=None,
**kwargs,
)
Key Parameters
Core Parameters
data
: Dataset (Pandas DataFrame) to visualize.x
,y
: Variables to be plotted on the x- and y-axes.kind
: Type of relational plot ("scatter"
or"line"
).
Aesthetic Mappings
hue
: Differentiates points or lines by color (categorical or numerical variable).size
: Differentiates points by size (numerical or categorical variable).style
: Differentiates points or lines by marker style or line dashes (categorical variable).palette
: Specifies color palette forhue
.size_order
: Specifies order forsize
categories.sizes
: Specifies range of marker sizes (e.g.,(min_size, max_size)
).
Faceting
col
,row
: Create subplots based on categorical variables.col_wrap
: Number of columns in the subplot grid.row_order
,col_order
: Specify order of categorical variables for faceting.
Figure Settings
height
: Height (in inches) of each subplot.aspect
: Aspect ratio (width/height) of each subplot.
Examples with Inbuilt Datasets
Dataset Setup
We will use the inbuilt datasets provided by Seaborn, such as tips
, fmri
, and penguins
.
import seaborn as sns
import matplotlib.pyplot as plt
# Load datasets
tips = sns.load_dataset('tips')
fmri = sns.load_dataset('fmri')
penguins = sns.load_dataset('penguins')
penguins = sns.load_dataset('penguins')
penguins.head()
species | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex |
0 | Adelie | Torgersen | 39.1 | 18.7 | 181.0 | 3750.0 |
1 | Adelie | Torgersen | 39.5 | 17.4 | 186.0 | 3800.0 |
2 | Adelie | Torgersen | 40.3 | 18.0 | 195.0 | 3250.0 |
3 | Adelie | Torgersen | NaN | NaN | NaN | NaN |
4 | Adelie | Torgersen | 36.7 | 19.3 | 193.0 | 3450.0 |
tips = sns.load_dataset('tips')
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fmri = sns.load_dataset('fmri')
fmri.head()
subject | timepoint | event | region | signal | |
0 | s13 | 18 | stim | parietal | -0.017552 |
1 | s5 | 14 | stim | parietal | -0.080883 |
2 | s12 | 18 | stim | parietal | -0.081033 |
3 | s11 | 18 | stim | parietal | -0.046134 |
4 | s10 | 18 | stim | parietal | -0.037970 |
1. Basic Scatter Plot
sns.relplot(data=tips,
x='total_bill',
y='tip',
kind='scatter')
plt.title("Basic Scatter Plot")
plt.show()
Use Case: Visualize the relationship between total bill and tip.

2. Basic Line Plot
sns.relplot(data=fmri,
x='timepoint',
y='signal',
kind='line')
plt.title("Basic Line Plot")
plt.show()
Use Case: Visualize how the signal
changes over timepoint
in the fmri dataset.

The shaded area around the plot represents a 95% confidence interval, which can be adjusted to show standard deviation by modifying the code:
sns.relplot(data=fmri, x='timepoint', y='signal', kind='line', errorbar='sd')
plt.title("Basic Line Plot")
plt.show()

You can also remove the confidence interval line entirely by setting the errorbar
parameter to none like this
sns.relplot(data=fmri, x='timepoint', y='signal', kind='line', errorbar=None)
plt.title("Basic Line Plot")
plt.show()

3. Scatter Plot with hue
sns.relplot(data=tips,
x='total_bill',
y='tip',
hue='sex',
kind='scatter')
plt.title("Scatter Plot with Hue")
plt.show()
Key Parameter: hue
differentiates data points by gender.

4. Line Plot with hue
sns.relplot(data=fmri,
x='timepoint',
y='signal',
hue='region',
kind='line')
plt.title("Line Plot with Hue")
plt.show()
Key Parameter: hue
colors the lines based on region
.

5. Scatter Plot with size
sns.relplot(data=tips,
x='total_bill',
y='tip',
size='size',
sizes=(20, 200),
kind='scatter')
plt.title("Scatter Plot with Variable Sizes")
plt.show()
Key Parameter: size
adjusts the marker sizes based on the party size.

6. Scatter Plot with style
sns.relplot(data=tips,
x='total_bill',
y='tip',
style='sex',
kind='scatter')
plt.title("Scatter Plot with Style")
plt.show()
Key Parameter: style
differentiates points by marker based on gender.

7. Subplots in columns
sns.relplot(data = tips,
x='total_bill',
y='tip',
col='sex',
kind='scatter')
plt.suptitle("Faceted Scatter Plot", y=1.05)
plt.show()
Key Parameter: col
creates subplots for each sex

The plt.suptitle("Faceted Scatter Plot", y=1.05)
function is used to add a super title to the entire figure when using Seaborn's relplot
(or other FacetGrid-based plots). This super title is placed above all the subplots (facets) created by sns.relplot
.
"Faceted Scatter Plot"
: The text of the super title.y=1.05
: The vertical position of the super title. The default value is1.0
, which places the title at the top of the figure. By settingy=1.05
, the title is moved slightly above the top of the figure to avoid overlapping with the subplots.
8. Subplots in rows
sns.relplot(data = tips,
x='total_bill',
y='tip',
row ='sex',
kind='scatter')
plt.suptitle("Faceted Scatter Plot", y=1.05)
plt.show()

9. Wrapping columns
sns.relplot(data=tips,
x="total_bill",
y="tip",
kind="scatter",
col="day",
col_wrap=2)
plt.show()

10. Ordering columns
sns.relplot(data=tips,
x="total_bill",
y="tip",
kind="scatter",
col="day",
col_wrap=2,
col_order=["Thur", "Fri", "Sat", "Sun"])
plt.show()

8. Faceted Line Plot
sns.relplot(data=fmri,
x='timepoint',
y='signal',
hue='event',
kind='line',
col='region')
plt.suptitle("Faceted Line Plot", y=1.05)
plt.show()
Key Parameter: col
creates subplots for each brain region.

9. Line Plot with Custom dashes
sns.relplot(data=fmri,
x='timepoint',
y='signal',
hue='event',
style='event',
kind='line',
dashes=False)
plt.title("Line Plot with Custom Dashes")
plt.show()
Key Parameter: dashes=False
ensures solid lines.

10. Scatter Plot with Transparency
sns.relplot(data=tips,
x='total_bill',
y='tip',
alpha=0.6,
kind='scatter')
plt.title("Scatter Plot with Transparency")
plt.show()
Key Parameter: alpha
adjusts point transparency.

11. Faceted Scatter Plot with Row and Column
sns.relplot(data=penguins,
x='bill_length_mm',
y='bill_depth_mm',
row='sex',
col='species',
kind='scatter')
plt.suptitle("Faceted Scatter Plot with Row and Column", y=1.05)
plt.show()
Key Parameters: row
and col
create a grid layout based on gender and species.

12. Scatter Plot with Custom Marker Sizes
sns.relplot(data=tips,
x='total_bill',
y='tip',
size='size',
sizes=(50, 300),
kind='scatter')
plt.title("Scatter Plot with Custom Marker Sizes")
plt.show()
Key Parameter: sizes=(50, 300)
scales marker sizes.
