所以我有这段代码来改变xtick的字体,但是它只改变了照片中看到的第一个值字体,并且用蓝色的圆圈突出显示。我怎样才能让它改变整个xticks的字体?
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()发布于 2021-06-30 10:41:05
matplotlib有一个明显的bug,所以只正确设置了第一个标签。按照sammy的建议(Matplotlib make tick labels font size smaller)和这个问题(Set Font Properties to Tick Labels with Matplot Lib),您可以为每个标签设置它:
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)我的例子:
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()

https://stackoverflow.com/questions/68186413
复制相似问题