代码之家  ›  专栏  ›  技术社区  ›  Andrew

标题是粗体,但标签不是。该怎么办?

  •  0
  • Andrew  · 技术社区  · 2 年前

    我制作了一个可复制的小程序,在那里你会看到 title 以粗体显示,但 label 使用普通字体:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    observed = np.array([28, 15, 21, 23, 17, 22, 19, 19, 24, 27, 20, 21, 25, 25, 16, 20, 23])
    
    fig, ax = plt.subplots(figsize=(4, 2))
    
    sns.histplot(ax = ax, x = list(observed), label = "observed data") 
    ax.set_title("Observed data", fontweight='bold')
    
    ax.legend()
    plt.show()
    

    enter image description here

    我试着设置 标签 通过放入相同的代码以粗体显示 fontweight='bold' (在之后 label = 学期 在…内 这个 sns.histplot 指令,但这会导致错误:

    sns.histplot(ax = ax, x = list(observed), label = "observed data", fontweight='bold') 
    

    好吧,我承认这不是一个非常明智的尝试,但我不知道如何以简单的方式将标签加粗。。。 有人能帮我这么做吗?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Anonymous    2 年前

    调整图例的字体属性:

    ax.legend(prop={"weight": "bold"})
    

    https://matplotlib.org/3.5.0/api/_as_gen/matplotlib.pyplot.legend.html