Giving graphs a subtitle in matplotlib
I want to give my graph a title in big 18pt font, then a subtitle below it in smaller 10pt font. How can I do this in matplotlib? It appears the title()
function only takes one single string with a single fontsize
attribute. There has to be a way to do this, but how?
What I do is use the title()
function for the subtitle and the suptitle()
for the main title (they can take different font size arguments). Hope that helps!
Although this doesn't give you the flexibility associated with multiple font sizes, adding a newline character to your pyplot.title() string can be a simple solution;
plt.title('Really Important Plot\nThis is why it is important')
This is a pandas code example that implements Floris van Vugt's answer (Dec 20, 2010). He said:
>What I do is use the title() function for the subtitle and the suptitle() for the >main title (they can take different fontsize arguments). Hope that helps!
import pandas as pd
import matplotlib.pyplot as plt
d = {'series a' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'series b' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
title_string = "This is the title"
subtitle_string = "This is the subtitle"
plt.figure()
df.plot(kind='bar')
plt.suptitle(title_string, y=1.05, fontsize=18)
plt.title(subtitle_string, fontsize=10)
Note: I could not comment on that answer because I'm new to stackoverflow.
I don't think there is anything built-in, but you can do it by leaving more space above your axes and using figtext
:
axes([.1,.1,.8,.7])
figtext(.5,.9,'Foo Bar', fontsize=18, ha='center')
figtext(.5,.85,'Lorem ipsum dolor sit amet, consectetur adipiscing elit',fontsize=10,ha='center')
ha
is short for horizontalalignment
.