wychen
wychen

Reputation: 129

How to set x_ticks different fontsizes

Let's say I want some of the x_ticks to be bigger than the others, how can I achieve that?

Ideally I would pass a list of fontsizes to this function, where I plot those x_ticks, but it seems to be missing, I can only pass a number for a fontsize of all ticks.

plt.xticks(ticks=tick_positions, labels=label_names,fontsize=20)

So what I want is instead of 20, I would like to pass a list of fontsizes.

Upvotes: 0

Views: 332

Answers (1)

tmdavison
tmdavison

Reputation: 69106

You can iterate over the tick labels (which you can get from ax.get_xticklabels()) and set the font size of those using .set_fontsize(). If you have a list of font sizes you want to set, you could iterate over that at the same time as the ticks (using zip).

A minimal example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

tick_positions = [0, 1, 2, 3, 4, 5]
label_sizes = [10, 10, 20, 10, 10, 20]

ax.set_xticks(tick_positions)

for tick, size in zip(ax.get_xticklabels(), label_sizes):
    tick.set_fontsize(size)

plt.show()

enter image description here

Upvotes: 1

Related Questions