如何使用 Matplotlib 来更改坐标轴的刻度的字体大小?


若要更改 Matplotlib 中坐标轴对象的刻度字体大小,我们可以采取以下步骤:

  • 使用 Numpy 创建 x 和 y 数据点。

  • 使用 subplots() 方法创建一个图形和一组子图 (fig and ax)

  • 使用 plot() 方法绘制 x 和 y 数据点,颜色为红色且线宽为 5。

  • 使用 x 数据点设置 xticks 

  • 使用 get_major_ticks() 方法获取主要刻度列表。

  • 迭代主要刻度(从步骤 5 开始),并设置字体大小,将其旋转 45 度。

  • 若要显示该图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-2, 2, 10)
y = np.sin(x)

fig, ax = plt.subplots()

ax.plot(x, y, c='red', lw=5)
ax.set_xticks(x)
for tick in ax.xaxis.get_major_ticks():
   tick.label.set_fontsize(14)
   tick.label.set_rotation('45')

plt.tight_layout()

plt.show()

输出

更新日期:08-May-2021

527 次浏览

开启您的 职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.