【问题标题】:How to increase the font size of the legend in my Seaborn FactorPlot/FacetGrid?如何在我的 Seaborn FactorPlot/FacetGrid 中增加图例的字体大小?
【发布时间】:2018-10-16 11:51:06
【问题描述】:

来自 question 的说明不适用于 Seaborn FacetPlots。是否有可能让方法做同样的事情?

【问题讨论】:

  • 我只想修改图例字体大小(标题/文本分别) - 就像上面链接的问题一样。

标签: python seaborn


【解决方案1】:

在链接的答案中,您可以使用setp 设置属性(在本例中为图例的字体大小)。

与链接问题的唯一区别是您需要为 FacetGrid 的每个轴执行此操作

g = FacetGrid( ... )

for ax in g.axes.flat:
    plt.setp(ax.get_legend().get_texts(), fontsize=22)  # for legend text
    plt.setp(ax.get_legend().get_title(), fontsize=32)  # for legend title

【讨论】:

  • 这会产生以下错误:AttributeError: 'FacetGrid' object has no attribute 'get_legend'。它也在对该链接答案的评论中提到。
  • 你不能从我在这里提出的代码中得到这个错误。
  • 哦,很抱歉没有注意到变化。
  • 还要添加到这个答案,如果使用sns.factorplot,你需要做ax[0].get_legend()....
  • 这可能已被弃用,不再有效。 @Hielke Walinga 的解决方案目前是准确的。
【解决方案2】:

如果您使用的是较新版本的 matplotlib,则可以更轻松地更改图例字体大小 -

plt.legend(fontsize='x-large', title_fontsize='40')

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

可能取决于您使用的 matplotlib 版本。我使用的是 2.2.3,它有 fontsize 参数,但没有 title_fontsize

【讨论】:

    【解决方案3】:

    facetgrid 图例不是坐标区的一部分,而是 facetgrid 对象的一部分。图例仍然是一个标准的 matplotlib 图例,可以这样操作。

    plt.setp(g._legend.get_title(), fontsize=20)
    

    其中 g 是您调用创建它的函数后返回的 facetgrid 对象。

    【讨论】:

      猜你喜欢
      • 2017-12-06
      • 2014-10-09
      • 1970-01-01
      • 2019-11-25
      • 2014-11-27
      • 2021-03-11
      • 2013-12-22
      相关资源
      最近更新 更多