Jointplots¶
A jointplot combines a bivariate plot and a marginal plot to visualize the relationship between two continuous variables, providing a comprehensive view of how they interact and behave together.
By displaying the bivariate plot in the middle, surrounded by marginal plots showing the distribution of each variable separately, jointplots shed light on correlation, clustering, and outliers in the data, as well as patterns and relationships that might not be immediately apparent from looking at just one variable alone.
Show code cell source
import plotly.io as pio
pio.renderers.default = "sphinx_gallery"
import plotly.express as px
import statsplotly
By default, a scatter plot is used for depicting the joint distribution :
df = px.data.tips()
fig = statsplotly.jointplot(
data=df,
x="total_bill",
y="tip",
hist=True,
kde=True,
fit="linear",
bins_x=20,
color_palette="Set2",
slicer="sex",
marginal_plot="all",
)
fig.layout.height = 800
fig.show()
2D density plot¶
The underlying distribution can be plotted by using the kde
keyword argument.
As it is not possible to visualize density maps as overlays, a dropdown menu is created to switch between data slices :
df = px.data.tips()
fig = statsplotly.jointplot(
data=df,
x="total_bill",
y="tip",
step=False,
rug=True,
kde=True,
hist=False,
fit="linear",
plot_type="scatter+kde",
equal_bins_x=False,
bins_x=20,
color_palette="Set2",
kde_color_palette="greens",
slicer="sex",
shared_coloraxis=True,
marginal_plot="all",
)
fig.layout.height = 600
fig.show()
2D histograms¶
Multiple different combination of bivariate and univariate histograms and color palettes can be combined :
df = px.data.tips()
fig = statsplotly.jointplot(
data=df,
x="total_bill",
y="tip",
step=False,
rug=True,
kde=False,
hist=True,
plot_type="histogram",
equal_bins_x=True,
bins_x=20,
color_palette="bone",
kde_color_palette="reds",
slicer="sex",
marginal_plot="x",
)
fig.layout.height = 600
fig.show()
“Histmaps”¶
The <dimension>_histmap
parameter draws an histogram of one variable along unique values of the dimension.
Here, setting plot_type="x_histmap"
display histogram of stock value for each month :
import pandas as pd
df = px.data.stocks().set_index("date")
fig = statsplotly.jointplot(
data=df.melt(ignore_index=False, var_name="company", value_name="stock_value"),
y="stock_value",
x="date",
plot_type="x_histmap",
barmode="stack",
slicer="company",
marginal_plot="y",
)
fig.layout.height = 600
fig.show()
Full details of the API : jointplot()
.