import pandas as pd
from plotnine import (
ggplot,
aes,
geom_point,
geom_line,
geom_linerange,
element_blank,
element_rect,
theme,
theme_matplotlib,
theme_set,
)
# Set default theme
# matplotlib + the background of 538
theme_set(
theme_matplotlib()+ theme(
=element_rect(fill="#F0F0F0"),
plot_background=element_rect(fill="#F0F0F0"),
panel_background
) )
Upper & Lower Trends
The data
= pd.DataFrame({
df "x": range(0, 6),
"y": [3, 4, 3.5, 4.5, 4, 4]
})
Create the line ranges
"x", "y"))
(ggplot(df, aes(+ geom_linerange(aes(ymin="y-2", ymax="y"))
)
Add points at each end of the ranges.
"x", "y"))
(ggplot(df, aes(+ geom_linerange(aes(ymin="y-2", ymax="y"))
+ geom_point()
+ geom_point(aes(y="y-2"))
)
Add a trend-line along the top of the ranges and one along the bottom of the ranges. The line is added before(below) the points.
"x", "y"))
(ggplot(df, aes(+ geom_linerange(aes(ymin="y-2", ymax="y"))
+ geom_line()
+ geom_point()
+ geom_line(aes(y="y-2"))
+ geom_point(aes(y="y-2"))
)
Add some color.
# Gallery, lines
= "#9E2F68"
magenta = "#E5C8D6"
magenta_light
= "#2F9E65"
sea_green = "#C8E5D7"
sea_green_light
"x", "y"))
(ggplot(df, aes(+ geom_linerange(aes(ymin="y-2", ymax="y"))
+ geom_line(color=magenta, size=3)
+ geom_point(color=magenta, fill=magenta_light, size=3, stroke=0.5)
+ geom_line(aes(y="y-2"), color=sea_green, size=3)
+ geom_point(aes(y="y-2"), color=sea_green, fill=sea_green_light, size=3, stroke=0.5)
)