Jason
Jason

Reputation: 181

Setting font type for x and y ticks in subplots matplotlib

I am having issues setting up the x and y ticks font type to "Times New Roman". Here is my code below:

font = {'fontname':'Times New Roman'}

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(20,12))
axes[0,0].get_xticklabels(**font)
axes[0,0].get_yticklabels(**font)

However, this command is not resetting the font type to the font I would like it to be. The titles for the x and y axis and labels will set the font type based off the **font argument. Can someone point me to the right command to fix this issue please? Thank you for your time and assistance.

Upvotes: 2

Views: 2932

Answers (1)

Suever
Suever

Reputation: 65430

You need to set the fontname for each of the xticklabels and yticklabels for each axes.

You can get the tick labels using get_xticklabels and get_yticklabels. Then you want to set the fontname property of the resulting Text objects.

import matplotlib.pyplot as plt

fontname = 'Times New Roman'

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(10,6))

for ax in axes.flatten():
    labels = ax.get_xticklabels() + ax.get_yticklabels()
    [label.set_fontname(fontname) for label in labels]

enter image description here

Upvotes: 3

Related Questions