Joint plot#
import pandas as pd
from lets_plot.bistro import *
from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv")
print(df.shape)
df.head()
(150, 5)
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
1. Default Presentation of Joint Plot#
In the simplest case, assign x and y to create a scatterplot (using geom_point()) with marginal histograms (using geom_histogram()).
joint_plot(df, "petal_length", "petal_width")
2. Change Geom Types#
In addition to the points, there are a few other types of geoms: tile, density2d(f), pointdensity.
joint_plot(df, "petal_length", "petal_width", geom='tile')
joint_plot(df, "petal_length", "petal_width", color_by="species", geom='density2d')
joint_plot(df, "petal_length", "petal_width", geom='pointdensity')
3. Change Geom Parameters#
Use additional parameters for better customization: color, size, alpha, etc.
joint_plot(df, "petal_length", "petal_width", color="#756bb1", size=8, alpha=.5, se=False)
4. Marginal Layers Customization#
marginal parameter is a shortcut for the ggmarginal() layer.
joint_plot(df, "petal_length", "petal_width", color="black", marginal="box:lb:.03,hist:t:.4,hist:r") + \
ggmarginal("tr", layer=geom_area(stat='density', color="magenta", fill="magenta", alpha=.1)) + \
theme(axis_line_x='blank', axis_line_y='blank')
5. Grouping#
The color_by parameter sets the mapping to the fill and color aesthetics.
joint_plot(df, "petal_length", "petal_width", color_by="species", marginal="hist:tr")
6. Additional Layer#
Add any other layer that supports x and y aesthetics (e.g. points layer with the geom_point() function).
joint_plot(df, "petal_length", "petal_width", geom='density2df', \
color="#993404", alpha=1/3, reg_line=False) + \
geom_point(size=5, shape=21, color="#993404", fill="#ffffd4") + \
scale_fill_gradient(low="#d95f0e", high="#fff7bc")