import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Import your data
df = pd.read_csv("../country-gdp-2014.csv")
df.head()
Country | Continent | GDP_per_capita | life_expectancy | Population | |
---|---|---|---|---|---|
0 | Afghanistan | Asia | 663 | 54.863 | 22856302 |
1 | Albania | Europe | 4195 | 74.200 | 3071856 |
2 | Algeria | Africa | 5098 | 68.963 | 30533827 |
3 | Angola | Africa | 2446 | 45.234 | 13926373 |
4 | Antigua and Barbuda | N. America | 12738 | 73.544 | 77656 |
Change the font just for the title or axis labels
The default font is BitstreamVeraSans Roman, but we want to try out
something else. You can pass fontname
to .set_xlabel
, .set_ylabel
,
.set_title
, or .annotate
to specify a particular font. This does not
change the font for the numbers on the axes.
# Plot the median life expectancy by continent
ax = df.groupby('Continent')['Population'].median().sort_values().plot(kind='barh')
ax.set_xlabel("")
# Change the y axis label to Arial
ax.set_ylabel("Median Population", fontname="Arial", fontsize=12)
# Set the title to Comic Sans
ax.set_title("Oceania has small countries", fontname='Comic Sans MS', fontsize=18)
Text(0.5,1,'Oceania has small countries')
Change the font for the tick marks/numbers on the axes
Changing the fonts for the labels on each axis (the numbers) is a little bit more complicated, but you can use it in combination with the content above to specify fonts for every part of your graph.
# Plot the median life expectancy by continent
ax = df.groupby('Continent')['Population'].median().sort_values().plot(kind='barh')
# You can also ax.set_xlabel("") but the spacing gets weird
ax.xaxis.label.set_visible("False")
# Change the y axis label to Arial
ax.set_ylabel("Median Population", fontname="Arial", fontsize=12)
# Set the title to Comic Sans
ax.set_title("Oceania has small countries", fontname='Comic Sans MS', fontsize=18)
# Set the font name for axis tick labels to be Comic Sans
for tick in ax.get_xticklabels():
tick.set_fontname("Comic Sans MS")
for tick in ax.get_yticklabels():
tick.set_fontname("Comic Sans MS")
Specify a default font for everything on your graphs
You can also specify a default font for everything in matplotlib
. This will
affect every single plot you make.
Note: Although you can do this, unless you’re practicing to make a house style I recommend specifying single-use fonts (the above section) instead of defaults.
I don’t know why, but you can only set it once. If you change your mind about what you want your default font to be you’ll have to restart your kernel.
It also knows whether your font is serif or sans-serif. If you try it with
serif
and it doesn’t work, change both of them to sans-serif
instead. For
example, if I changed my font to Georgia down below I’d need it to be
serif
instead, since it’s a font with serifs.
# Say, "the default sans-serif font is COMIC SANS"
# Then, "ALWAYS use sans-serif fonts"
plt.rcParams.update({
'font.sans-serif': 'Comic Sans MS',
'font.family': 'sans-serif'
})
ax = df.groupby('Continent')['Population'].median().sort_values().plot(kind='barh')
ax.set_xlabel("")
ax.set_ylabel("Median Population")
ax.set_title("Oceania has small countries", fontsize=20)
Text(0.5,1,'Oceania has small countries')
Listing all of the fonts matplotlib knows about
If you’d like to know what fonts are available for use, check out list all fonts available in matplotlib plus samples.