Title font in subplots with axes.title.set_text

Question:

I have intention in making multiple subplot to present my results. I used subplots from matplotlib. I have a problem with text sizes. As you can see in the trivial code here. In the plt.title documentation it says title(label, fontdict=None, loc='center', pad=None, **kwargs)

import random
from matplotlib.pyplot import figure, plot, xlabel, ylabel, legend, close, subplots, title, savefig, get_current_fig_manager, show, pause, clf

x = []
for i in range(10):
    x.append(random.random()*i)

y_1 = []

for i in range(10):
    y_1.append(random.random()*i)
y_2 = []

for i in range(10):
    y_2.append(random.random()*i)


fig, ax = subplots(1, 2, squeeze = False, figsize = (10,10))
ax[0,1].title.set_text('y_1', fontdict = {'font.size':22})
ax[0,1].plot(x,y_1)
ax[0,1].set_xlabel('x')
ax[0,1].set_ylabel('y_1')

ax[0,0].title.set_text('y_2', fontdict = {'font.size':22})
ax[0,0].plot(x,y_2)
ax[0,0].set_xlabel('x')
ax[0,0].set_ylabel('y_2')

but if I run this code I get an error TypeError: set_text() got an unexpected keyword argument 'fontdict'

am I using the wrong command.

Asked By: Noob Programmer

||

Answers:

This is really just a minor issue:

To set the title of a specific axes you should use the set_title method of the axes. Using plt.title sets the title of the current axes instance.

Basically replace your ax[0,0].title.set_text with ax[0,0].set_title and you are good to go!

Answered By: jojo

You can also simply use fontsize=22 directly , as in
ax[0,1].set_title('y_1', fontsize=22)

Answered By: Geofredo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.