14

While plotting using Matplotlib, I have found how to change the font size of the labels. But, how can I change the size of the numbers in the scale?

For clarity, suppose you plot x^2 from (x0,y0) = 0,0 to (x1,y1) = (20,20). The scale in the x-axis below maybe something like

0 1 2 ... 20.

I want to change the font size of such scale of the x-axis.

hectorpal
  • 711
  • 2
  • 6
  • 15

2 Answers2

23

Matplotlib refers to these as xtick labels. They can be changed in a large number of different ways, passed in as parameters, or iterated through and edited (as in the case of the solution posted here Matplotlib make tick labels font size smaller).

I went ahead and posted a more concise solution, as the former was very inefficient.

from matplotlib import pyplot
import math

def setLabelExample():
    fig = pyplot.figure() 
    x = [i for i in range(200)]
    y = [xi**2 for xi in x]

    ax = fig.add_subplot(1,1,1)
    ax.plot(x, y) 
    ax.tick_params(axis='x', labelsize=30)
    fig.suptitle('Matplotlib xticklabels Example')
    pyplot.show()

if __name__ == '__main__':
    setLabelExample()
Community
  • 1
  • 1
Cory Dolphin
  • 2,650
  • 1
  • 20
  • 30
  • 1
    Thank you! I should add that the method tick_params also work for the whole figure, not only for subplots. – hectorpal Jul 09 '12 at 11:18
0

simply put, you can use the following command to set the range of the ticks and change the size of the ticks

import matplotlib.pyplot as plt

set the range of ticks for x-axis and y-axis

plt.set_yticks(range(0,24,2))

plt.set_xticks(range(0,24,2))

change the size of ticks for x-axis and y-axis

plt.yticks(fontsize=12,)

plt.xticks(fontsize=12,)

eric R
  • 321
  • 2
  • 7