35

I'm using Pythons matplotlib and this is my code:

  plt.title('Temperature \n Humidity')

How can I just increase the font size of temperature instead of both the temperature & the humdity?

This does NOT work:

 plt.title('Temperature \n Humidity', fontsize=100)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

6 Answers6

53

fontsize can be assigned inside dictionary fontdict which provides additional parameters fontweight, verticalalignment , horizontalalignment

The below snippet should work

plt.title('Temperature \n Humidity', fontdict = {'fontsize' : 100})

Shyam A
  • 531
  • 4
  • 2
44
import matplotlib.pyplot as plt
plt.figtext(.5,.9,'Temperature', fontsize=100, ha='center')
plt.figtext(.5,.8,'Humidity',fontsize=30,ha='center')
plt.show()

Probably you want this. You can easily tweak the fontsize of both and adjust there placing by changing the first two figtext positional parameters. ha is for horizontal alignment

Alternatively,

import matplotlib.pyplot as plt

fig = plt.figure() # Creates a new figure
fig.suptitle('Temperature', fontsize=50) # Add the text/suptitle to figure

ax = fig.add_subplot(111) # add a subplot to the new figure, 111 means "1x1 grid, first subplot"
fig.subplots_adjust(top=0.80) # adjust the placing of subplot, adjust top, bottom, left and right spacing  
ax.set_title('Humidity',fontsize= 30) # title of plot

ax.set_xlabel('xlabel',fontsize = 20) #xlabel
ax.set_ylabel('ylabel', fontsize = 20)#ylabel

x = [0,1,2,5,6,7,4,4,7,8]
y = [2,4,6,4,6,7,5,4,5,7]

ax.plot(x,y,'-o') #plotting the data with marker '-o'
ax.axis([0, 10, 0, 10]) #specifying plot axes lengths
plt.show()

Output of alternative code:

enter image description here

PS: if this code give error like ImportError: libtk8.6.so: cannot open shared object file esp. in Arch like systems. In that case, install tk using sudo pacman -S tk or Follow this link

Tanmaya Meher
  • 1,438
  • 2
  • 16
  • 27
  • Hi Meher, what is the .5,.9 ? –  Jul 30 '14 at 13:27
  • 1
    I have specified there. These are positional parameter of the titles. You can say it as padding also. one for the x-axis and one for the y-axis. Just try manipulating them, you will understand; e.g., The text "Humidity" was just below "Temperature" so I place it just below by giving a value to the y-axis position as .8 where as x-axis position parameter is same as that of "Temperature", i.e., .5 . – Tanmaya Meher Jul 30 '14 at 13:32
  • Hi Meher, how can i get the figtext to display on top of the graph rather than on the figure –  Jul 31 '14 at 02:23
  • 1
    Sorry to say it may not be possible with `figtext`; except tweaking the font size and positional parameters accordingly. You should have posted some more code with some example data. Now I am posting a different code; but that would be problematic / annoying to you if you are on a project / already plotted the data. See the updated code. – Tanmaya Meher Jul 31 '14 at 07:02
  • hi Meher, i got an error that says ax is not defined –  Jul 31 '14 at 08:03
  • you may have left `fig.add_subplot(111) ` line assignment to `ax`, i.e., `ax = fig.add_subplot(111)`. code is perfectly fine. I have checked it again in python command line now. – Tanmaya Meher Jul 31 '14 at 08:05
  • ax is just a variable for the figure object. But anyway good to know it worked. – Tanmaya Meher Jul 31 '14 at 08:34
5

This has been mostly working for me across recent versions of Matplotlib (currently 2.0.2). It is helpful for generating presentation graphics:

def plt_resize_text(labelsize, titlesize):
    ax = plt.subplot()
    for ticklabel in (ax.get_xticklabels()):
        ticklabel.set_fontsize(labelsize)
    for ticklabel in (ax.get_yticklabels()):
        ticklabel.set_fontsize(labelsize)
    ax.xaxis.get_label().set_fontsize(labelsize)
    ax.yaxis.get_label().set_fontsize(labelsize)
    ax.title.set_fontsize(titlesize)

The odd for-loop construction seems to be necessary to adjust the size of each tic label. Also, the above function should be called just before the call to plt.show(block=True), otherwise for whatever reason the title size occasionally remains unchanged.

plasmo
  • 343
  • 3
  • 7
3

Assuming you are using matplotlib to render some plots.

You might want to checkout Text rendering With LaTeX — Matplotlib

Here are some lines of code for your case

plt.rc('text', usetex=True)
plt.title(r"\begin{center} {\Large Temperature} \par {\large Humidity} \end{center}")

plot

Hope that helps.

yipeipei
  • 76
  • 4
1

Simply do the following: ax.set_title('This is the title',fontsize=20)

Aymen Alsaadi
  • 1,329
  • 1
  • 11
  • 12
0

I don't want to go into this chart exactly Universal method: step one - make a distance between the main title and the chart:

from matplotlib import rcParams
rcParams['axes.titlepad'] = 20 

then insert subtitle by setting coordinates:

ax.text(0.3, -0.56, 'subtitle',fontsize=12)

enter image description here

Wojciech Moszczyński
  • 2,893
  • 21
  • 27