Themes
%%capture
from datar.datasets import mpg
from plotnine import *
from plotnine_prism import *
%run nb_helpers.py
base = ggplot(mpg, aes(x="displ", y="cty")) + geom_point(aes(colour="class"))
base
# apply default theme
p1 = (
base
+ theme_prism()
+ theme(
legend_position=(0.8, 0.7),
legend_key_height=10,
)
)
p2 = (
base
+ theme_prism()
+ theme(
legend_position=(0.8, 0.7),
legend_key_height=10,
legend_title=element_text(margin={'b': 8}),
)
)
print(p1, p2)
# redefine base plot without a legend for convenience
base = ggplot(mpg, aes(x="displ", y="cty")) + geom_point(
aes(colour="class"), show_legend=False
)
# adjust overall theme size
p1 = base + theme_prism(base_size=10)
p2 = base + theme_prism(base_size=16)
print(p1, p2)
# adjust overall theme size with specific line size
p1 = base + theme_prism(base_size = 12)
p2 = base + theme_prism(base_size = 12, base_line_size = 0.2)
print(p1, p2)
# change fontface or font family
p1 = base + theme_prism(base_fontface = "plain")
p2 = base + theme_prism(base_family = "monospace")
print(p1, p2)
# change x axis text angle
p1 = base + theme_prism(axis_text_angle = 45)
p2 = base + theme_prism(axis_text_angle = 90)
print(p1, p2)
# add a border and adjust its thickness
p1 = base + theme_prism(border=True) + coord_cartesian()
p2 = (
base
+ theme_prism(border=True, base_rect_size=4)
+ coord_cartesian() # adjust thickness
)
print(p1, p2)
# Theme palettes
display(str(list_themes()))
# try out some different theme palettes
p1 = base + theme_prism(palette = "purple_passion")
p2 = base + theme_prism(palette = "candy_bright")
print(p1, p2)
# compare two identical theme palettes
p1 = base + theme_prism(palette = "black_and_white")
p2 = base + theme_prism(palette = "plasma")
print(p1, p2)
# try out two more theme palettes and their corresponding colour palettes
p1 = base + theme_prism(palette="summer") + scale_colour_prism(palette="summer")
p2 = (
base
+ theme_prism(palette="stained_glass")
+ scale_colour_prism(palette="stained_glass")
)
print(p1, p2)
# Custom a new theme based on "theme_prism"
def theme_new(
base_size=10,
base_family="monospace",
base_fontface="bold",
base_line_size=None,
base_rect_size=None,
axis_text_angle=0,
border=False,
):
return theme_prism(
palette="stained_glass",
base_size=base_size,
base_family=base_family,
base_fontface=base_fontface,
base_line_size=base_line_size,
base_rect_size=base_rect_size,
axis_text_angle=axis_text_angle,
border=border,
) + theme(
panel_background=element_rect(fill="white", colour="None"),
plot_background=element_rect(fill="red", colour="None"),
axis_line=element_line(colour="black"),
axis_ticks=element_line(colour="black"),
)
# compare theme_prism() and our new theme function
p1 = base + theme_prism()
p2 = base + theme_new()
print(p1, p2)