Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Title font in subplots with axes.title.set_text

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.

like image 522
Noob Programmer Avatar asked Aug 30 '19 06:08

Noob Programmer


People also ask

How do you change the font size of a title in a Matplotlib figure?

pyplot. title() method to assign a title to a plot, so in order to change the font size, we are going to use the font size argument of the pyplot. title() method in the matplotlib module.

Which method is used to add title to the subplots using Matplotlib?

We can also add title to subplots in Matplotlib using title. set_text() method, in similar way to set_title() method.


1 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!

like image 79
jojo Avatar answered Sep 19 '22 21:09

jojo