【问题标题】:seaborn/matplotlib change number of columns in legend objectseaborn/matplotlib 更改图例对象中的列数
【发布时间】:2019-10-17 07:16:40
【问题描述】:

我见过Creating multi column legend in python seaborn plot,但我认为我的问题有点不同。简而言之,我有一个数据框,我在seabornlmplot 中绘制并得到一个FacetGrid。麻烦的是,hue 有很多值,所以我得到了一个超长的单列图例。下面的代码示例:

ers = sns.lmplot(
    data=emorb,
    x="Pb",
    y="Nd",
    row="Ridge Sys",
    hue="Seg Name",
    scatter=True,
    fit_reg=False,
    scatter_kws={"alpha":0.7, "edgecolor": "w"},
    palette=sns.color_palette("bright", 20),
    legend=True
)

ers.set(ylim=(0.5122,0.5134))

我可以访问通过调用ers._legend 创建的图例对象,这将返回一个类型为Legend 的对象(基本上是一个matplotlib 对象)。但是,我无法调用此图例对象来更改列数,例如:

l = ers._legend
l(ncols=9)

有什么建议,还是我遗漏了一些更明显的东西,例如重绘图例和指定任何参数的方法?

谢谢。

【问题讨论】:

    标签: python matplotlib plot seaborn legend


    【解决方案1】:

    哎呀,想通了:

    FacetGrid 对象有一个属性fig,即

    g = sns.lmplot()
    parent_mpl_figure = g.fig
    

    因此,如果我在sns.lmplot() 中设置legend=False,我就可以指定parent_mpl_figure.legend(labels=[], ncol=9, bbox_to_anchor=(1,1))

    写得很清楚:

    g = sns.lmplot(legend = False)
    parent_mpl_figure = g.fig
    parent_mpl_figure.legend(labels = [], ncol = 9, bbox_to_anchor = (1,1))
    

    希望这对其他人有指导意义/现在弄清楚如何让每个Facet 跨越整个调色板,以便每个Facet 组中的不同hue 组更容易区分...

    【讨论】:

    • 在时间限制允许的情况下接受您自己的答案
    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2020-08-30
    • 2016-01-25
    • 2020-09-16
    相关资源
    最近更新 更多