In [1]:
Copied!
import pandas as pd
from plotnine import *
df = pd.read_csv('countries.csv')
df.head(2)
import pandas as pd
from plotnine import *
df = pd.read_csv('countries.csv')
df.head(2)
Out[1]:
country | continent | gdp_per_capita | life_expectancy | population | |
---|---|---|---|---|---|
0 | Afghanistan | Asia | 663 | 54.863 | 22856302 |
1 | Albania | Europe | 4195 | 74.200 | 3071856 |
Changing fonts on plotnine graphics¶
To change fonts, you typically need to add in a theme. Depending on which fonts you want to change – header, axes, ticks, annotations, etc – you'll want to set different options for the theme.
Change the font of all of the text¶
To change every single text element on the graphic, you'll pass the theme text=element_text(family='Your Font Name')
, where family=
the name of the font.
In [2]:
Copied!
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(text=element_text(family='Frutiger Bold'))
)
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(text=element_text(family='Frutiger Bold'))
)
Out[2]:
<ggplot: (322121262)>
If you can't get it to work, check how to get a list of fonts – maybe plotnine knows it by a weird name! There's a link to the left on how to do this.
Changing the font of the title¶
In [19]:
Copied!
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(plot_title=element_text(family='Frutiger Bold'))
)
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(plot_title=element_text(family='Frutiger Bold'))
)
Out[19]:
<ggplot: (325084802)>
Changing the font of the labels¶
In [25]:
Copied!
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(axis_title=element_text(family='Rockwell'))
)
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy')
+ geom_point()
+ labs(title="Life Expectancy vs GDP (per capita)")
+ theme(axis_title=element_text(family='Rockwell'))
)
Out[25]:
<ggplot: (325203460)>
Changing the font of annotations¶
When you change the font of an annotation, you don't use theme
like you do for most everything else. Instead you'll use family=
inside of geom_text
or geom_label
.
In [77]:
Copied!
import matplotlib.patheffects as path_effects
labeled = df[df.country.isin(['New Zealand', 'Samoa'])]
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy', label='country')
+ geom_point(color='grey')
+ geom_point(labeled, color='red')
+ geom_text(labeled,
family='Rockwell',
ha='left',
color='white',
nudge_x=1000,
path_effects=[
path_effects.Stroke(linewidth=2, foreground='black'),
path_effects.Normal()
])
+ labs(title="Life Expectancy vs GDP (per capita)")
)
import matplotlib.patheffects as path_effects
labeled = df[df.country.isin(['New Zealand', 'Samoa'])]
(
ggplot(df)
+ aes(x='gdp_per_capita', y='life_expectancy', label='country')
+ geom_point(color='grey')
+ geom_point(labeled, color='red')
+ geom_text(labeled,
family='Rockwell',
ha='left',
color='white',
nudge_x=1000,
path_effects=[
path_effects.Stroke(linewidth=2, foreground='black'),
path_effects.Normal()
])
+ labs(title="Life Expectancy vs GDP (per capita)")
)
Out[77]:
<ggplot: (325957690)>
In [ ]:
Copied!