Themes, Palettes, and Scales¶
The styling helpers provide ggpubr-, ggsci-, and ggthemes-inspired themes, palettes, and scales as ordinary plotnine components.
[1]:
import numpy as np
import pandas as pd
from plotnine_extra import *
from plotnine_extra.data import ToothGrowth, flights, iris, penguins
tooth = ToothGrowth.assign(dose=ToothGrowth["dose"].astype(str))
penguin_data = penguins.dropna(subset=[
"bill_length_mm",
"bill_depth_mm",
"body_mass_g",
"species",
]).copy()
ggthemes-style palettes¶
ggthemes_palette() returns deterministic hex colors. The named scales use the same palette definitions for color and fill aesthetics.
[2]:
{
"colorblind": ggthemes_palette("colorblind", n=4),
"tableau": ggthemes_palette("tableau", n=4),
"wsj": ggthemes_palette("wsj", n=4),
}
[2]:
{'colorblind': ['#000000', '#E69F00', '#56B4E9', '#009E73'],
'tableau': ['#4E79A7', '#F28E2B', '#E15759', '#76B7B2'],
'wsj': ['#0073CF', '#FFB100', '#D65F00', '#850000']}
[3]:
(
ggplot(penguin_data, aes("bill_length_mm", "bill_depth_mm", color="species"))
+ geom_point(alpha=0.75)
+ scale_color_colorblind(k=3)
+ theme_few()
+ labs(x="Bill length (mm)", y="Bill depth (mm)", color="Species")
)
[3]:
ggthemes-inspired themes¶
Theme helpers are plain plotnine themes. They can be combined with labels, scales, and guides.
[4]:
theme_data = flights[flights["year"].isin([1956, 1957, 1958])].copy()
theme_data["month_index"] = theme_data.groupby("year").cumcount() + 1
(
ggplot(theme_data, aes("month_index", "passengers", color="factor(year)"))
+ geom_line(size=1)
+ scale_color_wsj(k=3)
+ theme_wsj()
+ labs(x="Month", y="Passengers", color="Year", title="Air passengers")
)
[4]:
[5]:
(
ggplot(theme_data, aes("month_index", "passengers", color="factor(year)"))
+ geom_line(size=1)
+ scale_color_economist(k=3)
+ theme_economist()
+ labs(x="Month", y="Passengers", color="Year", title="Economist-style line plot")
)
[5]:
Publication themes and styling helpers¶
The ggpubr-style helpers adjust labels, grids, font settings, rotations, and axis scales without requiring a new theme class.
[6]:
(
ggplot(tooth, aes("dose", "len", fill="supp"))
+ geom_boxplot()
+ scale_fill_tableau(k=2)
+ theme_pubr()
+ labs(title="Tooth growth", x="Dose", y="Length")
+ labs_pubr()
+ rotate_x_text(0)
+ grids(axis="y")
)
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
findfont: Font family '' not found.
[6]:
Palette utilities¶
Palette utilities are useful outside a plot when preparing style guides or manual scales.
[7]:
{
"palette_names": all_palette_names()[:8],
"npg_colors": get_palette("npg", 3),
"point_shapes": type(show_point_shapes()).__name__,
"line_types": type(show_line_types()).__name__,
}
[7]:
{'palette_names': ('npg',
'aaas',
'nejm',
'lancet',
'jama',
'jco',
'ucscgb',
'uchicago'),
'npg_colors': ['#E64B35', '#4DBBD5', '#00A087'],
'point_shapes': 'ggplot',
'line_types': 'ggplot'}
Scale utilities¶
extended_range_breaks(), smart_digits(), and bank_slopes() support axis labels and aspect-ratio choices.
[8]:
breaks = extended_range_breaks(n=5)((0.2, 9.7))
{
"breaks": breaks,
"labels": smart_digits(breaks),
"bank": bank_slopes([0, 1, 2], [0, 2, 4]),
}
[8]:
{'breaks': [0.0, 2.0, 4.0, 6.0, 8.0, 10.0],
'labels': ['0', '2', '4', '6', '8', '10'],
'bank': 0.5}