Download notebook (.ipynb)

Residual plot#

import pandas as pd

from lets_plot import *
from lets_plot.bistro.residual 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

Default plot#

residual_plot(df, "petal_length", "petal_width")

Scatter plot (method='none')#

residual_plot(df, "petal_length", "petal_width", method='none', hline=False, marginal='none') + theme_classic()

Change geom type#

residual_plot(df, "petal_length", "petal_width", geom='tile', marginal="hist:tr") + \
    ggsize(600, 200)

Change method#

residual_plot(df, "petal_length", "petal_width", method='loess', span=.25, max_n=25, seed=1)

Geometries customization#

residual_plot(df, "petal_length", "petal_width", size=5, color="#feb24c", alpha=1/3)
residual_plot(df, "petal_length", "petal_width", geom='none', color="black", hline=False) + \
    geom_hline(yintercept=0, color="black") + \
    geom_point(size=5, shape=21, color="black", fill="#feb24c")
residual_plot(df, "petal_length", "petal_width", hline=False, marginal='none') + \
    geom_smooth(method='loess', se=True, level=.99, seed=42)

Marginal layers customization#

residual_plot(df, "petal_length", "petal_width", marginal="box:lb:.03,hist:t:.4,hist:r", color="black") + \
    ggmarginal("tr", layer=geom_area(stat='density', color="magenta", fill="magenta", alpha=.1)) + \
    theme_minimal()

Grouping#

residual_plot(df, "petal_length", "petal_width", color_by="species")

Interaction with other layers#

residual_plot(df, "petal_length", "petal_width", geom='tile', binwidth=[.4, .1], marginal='none') + \
    coord_fixed(ratio=.25, flip=True) + \
    theme(axis_ticks="blank", axis_text="blank", axis_line="blank")
residual_plot(df, "petal_length", "petal_width", color="white", binwidth=.1, marginal="hist:r") + \
    ylab("residual") + \
    theme_bw() + theme(text=element_text(family="monospace")) + \
    flavor_high_contrast_dark()