Explain Codes LogoExplain Codes Logo

How to change tick label font size

python
matplotlib
customization
plotting
Anton ShumikhinbyAnton Shumikhin·Nov 7, 2024
TLDR

The fastest way to change the tick label font size in matplotlib:

import matplotlib.pyplot as plt # Plotting some hot data plt.plot([1, 2, 3, 4], [10, 20, 25, 30]) # Set tick label size to 14, you know, because 14 is cool! plt.gca().tick_params(labelsize=14) plt.show()

Use the labelsize parameter in tick_params to adjust the font size for both x and y axis ticks.

Do more with your x and y axis!

Ever felt like you are not doing enough with your ticks? Don't worry! We have got you covered with the Matplotlib arsenal of customization options.

Major ticks vs Minor ticks

There's a major difference between these two; one's major, one's minor! Jokes apart, you can specify label size for 'major' and 'minor' ticks separately.

ax.tick_params(axis='both', which='major', labelsize=16) ax.tick_params(axis='both', which='minor', labelsize=12)

Tick labels on a diet

Ever seen tick labels doing yoga? No? Let's rotate them for better posture and readability.

plt.gca().tick_params(axis='x', rotation=45)

Individual attention

Some tick labels might need a bit of "individual tutoring" for them to shine.

for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(14) tick.label.set_rotation(45)

Only talking to the x-axis

If you're the type who only rotates x-axis labels because they're your favorite, this one's for you.

ax.set_xticklabels(['A', 'B', 'C'], fontsize='large')

Default is the new style

For globally cool plots, tweak the default label size with rcParams. This will apply to all your plots, talk about uniform inconsistencies.

import matplotlib as mpl mpl.rcParams['xtick.labelsize'] = 12 mpl.rcParams['ytick.labelsize'] = 12

The power of custom functions

Them: Spend minutes adjusting every single label. You, an intellectual: Create my own function to do all that work.

def set_ticklabel_style(axes, rotation, size): axes.tick_params(axis='both', rotation=rotation, labelsize=size)

Advanced customization

Let's dig into some complex label customization that lets you tinker until perfection.

Minimalist tick labels

Nobody said complexity can't be beautiful! If the situation calls for it, hide your minor ticks or simplify your labels. This directly translates to a less cluttered plot.

ax.tick_params(axis='y', which='minor', labelleft=False)

Spin that label right round

Speaking of spinning things around, here's how you can rotate tick labels.

ax.tick_params(axis='x', rotation=30)

Make sure to not overdo it, or you might end up with tick labels that need a chiropractor!

Exploring font traits

Font properties serve as the building blocks of readability and visual appeal in a plot. Here's how you can mess around with them.

from matplotlib.font_manager import FontProperties font = FontProperties(weight='bold', style='italic', size=12) ax.tick_params(axis='x', labelsize=font)

Embrace the change in defaults

With the default settings, you can manipulate various aspects of your plots—font size, family, tick sizes, and more.

mpl.rcParams.update({'font.size': 12, 'font.family': 'serif', 'xtick.major.size': 7})