0

So I have this code to change the font of the xticks however it only changes the first value font as seen in the photo and highlighted by the blue circles. How can I get it to change the font for entire xticks?

Photo

def plot_function(ax):
     prop = fm.FontProperties(fname='C:\Program Files (x86)\Adobe\Acrobat Reader DC\Resource\Font\AdobeDevanagari-Regular.otf')
     ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
     ax.set_xticklabels(('0.0', '0.2', '0.4', '0.6', '0.8', '1.0'), fontproperties=prop)

fig, ax = plt.subplots(3)
fig.tight_layout(h_pad=3)
plot_function(ax[0])
plot_function(ax[1])
plot_function(ax[2])

ax[0].plot(h, g)
ax[0].grid()
ax[0].set_xlabel('Time /S', fontproperties=prop, fontsize = 10)
ax[0].set_ylabel('Amplitude /mV', fontproperties=prop, fontsize = 10)
ax[0].set_xlim(0,1)


ax[1].plot(h, amplitude_envelope)
ax[1].grid()
ax[1].set_xlabel('Time /S', fontproperties=prop, fontsize = 10)
ax[1].set_ylabel('Amplitude /mV', fontproperties=prop, fontsize = 10)
ax[1].set_xlim(0,1)

ax[2].plot(h, y)
ax[2].grid()
ax[2].set_xlabel('Time /S', fontproperties=prop, fontsize = 10)
ax[2].set_ylabel('Amplitude /mV', fontproperties=prop, fontsize = 10)
ax[2].set_xlim(0,1)


plt.show()
Ben Watson
  • 29
  • 5
  • you could check https://stackoverflow.com/questions/6390393/matplotlib-make-tick-labels-font-size-smaller – sammy Jun 29 '21 at 23:50

1 Answers1

0

There is an apparent bug with matplotlib so only the first label is correctly set. Following sammy’s suggestion (Matplotlib make tick labels font size smaller) and this question (Set Font Properties to Tick Labels with Matplot Lib), you could set it for every label:

def plot_function(ax):
    prop = fm.FontProperties(fname='C:\Program Files (x86)\Adobe\Acrobat Reader DC\Resource\Font\AdobeDevanagari-Regular.otf')
    ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
    ax.set_xticklabels(('0.0', '0.2', '0.4', '0.6', '0.8', '1.0'))
    for label in ax.get_xticklabels():
        label.set_fontproperties(prop)

My example:

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import numpy as np

fig, axs = plt.subplots(1,2,constrained_layout=True)
    
# creating random data
x = np.linspace(0,1,100)
y = np.random.rand(100)
    
# dummy font as example, run the following command to list available fonts
# in your system:
# matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
prop = fm.FontProperties(fname='C:\\Windows\\Fonts\\verdanab.ttf')

for ax in axs:
    ax.plot(x, y)
    ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])   
    ax.grid()
    ax.set_xlabel('Time /S', fontproperties=prop, fontsize=10)
    ax.set_ylabel('Amplitude /mV', fontproperties=prop, fontsize=10)
    ax.set_xlim(0,1)
   
# original try
axs[0].set_xticklabels(
    ('0.0', '0.2', '0.4', '0.6', '0.8', '1.0'),
    fontproperties=prop,
    )
axs[0].set_title('set_xticklabels(fontproperties=prop)', fontsize=9)
    
# setting the property for every label in xtick
for label in axs[1].get_xticklabels():
    label.set_fontproperties(prop)
axs[1].set_title('label.set_fontproperties(prop)', fontsize=9)
    
plt.show()

Graph with a plot using set_xlabels fontproperties value and set_fontproperties for every label

wagnifico
  • 632
  • 3
  • 13