【问题标题】:Setting font type for x and y ticks in subplots matplotlib在子图中设置 x 和 y 刻度的字体类型 matplotlib
【发布时间】:2016-03-09 11:21:32
【问题描述】:

我在将 x 和 y 刻度字体类型设置为“Times New Roman”时遇到问题。下面是我的代码:

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)

但是,此命令不会将字体类型重置为我想要的字体。 x 和 y 轴和标签的标题将根据 **font 参数设置字体类型。有人可以指点我正确的命令来解决这个问题吗?感谢您的时间和帮助。

【问题讨论】:

    标签: python matplotlib fonts


    【解决方案1】:

    您需要为每个轴的xticklabelsyticklabels 设置fontname

    您可以使用get_xticklabelsget_yticklabels 获取刻度标签。然后你想设置生成的Text 对象的fontname 属性。

    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2012-05-13
      • 1970-01-01
      • 2018-11-23
      • 2012-03-14
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多