python 如何将海运图例拆分为多个列?

9q78igpj  于 2022-12-21  发布在  Python
关注(0)|答案(5)|浏览(201)

我使用不同色调和风格的relplot,并希望显示各自的图例条目,而不是在对方下面。
所以现在我得到了一个这样的图例:

相反,我希望有一个单一的图例看起来像这样:

如何才能做到

我尝试设置以下内容,但没有效果:

plot._legend
leg._ncol = 2
leg.handleheight = 1  # restricting the height

解决此问题的最小工作示例:

import pandas as pd
import seaborn as sns

columns = ['category1', 'category2', 'category3', 'time', 'value']

data = [['content1', 'other1', 'critera1', 0, 0.1], ['content1', 'other1', 'critera1', 1, 0.4], ['content1', 'other1', 'critera1', 2, 0.7], ['content2', 'other1', 'critera1', 0, 0.2], ['content2', 'other1', 'critera1', 1, 0.6], ['content2', 'other1', 'critera1', 2, 0.8], ['content1', 'other2', 'critera1', 0, 0.0], ['content1', 'other2', 'critera1', 1, 0.2], ['content1', 'other2', 'critera1', 2, 0.8], ['content2', 'other2', 'critera1', 0, 0.3], ['content2', 'other2', 'critera1', 1, 0.6], ['content2', 'other2', 'critera1', 2, 0.5], [
    'content1', 'other1', 'critera2', 0, 0.1], ['content1', 'other1', 'critera2', 1, 0.4], ['content1', 'other1', 'critera2', 2, 0.7], ['content2', 'other1', 'critera2', 0, 0.2], ['content2', 'other1', 'critera2', 1, 0.6], ['content2', 'other1', 'critera2', 2, 0.8], ['content1', 'other2', 'critera2', 0, 0.0], ['content1', 'other2', 'critera2', 1, 0.2], ['content1', 'other2', 'critera2', 2, 0.8], ['content2', 'other2', 'critera2', 0, 0.3], ['content2', 'other2', 'critera2', 1, 0.6], ['content2', 'other2', 'critera2', 2, 0.5], ]

df = pd.DataFrame(data, columns=columns)

plot = sns.relplot(x='time', y='value', col='category3', hue='category1', style='category2', kind="line", col_wrap=2, data=df)

leg = plot._legend
leg.set_bbox_to_anchor((0.5, 1.3, 0, 0))
leg._loc = 9

n3h0vuf2

n3h0vuf21#

既然你似乎想把图例放在图的上方,我会指示seaborn不要使用legend_out=False为图例保留右边的空间。然后,只需要获取seaborn创建的句柄和标签,并使用ncol=2生成一个新的图例。注意,只有当你在两列中有相同数量的元素时,这才能很好地工作,否则事情会变得混乱。

plot = sns.relplot(x='time', y='value', col='category3', hue='category1', style='category2', kind="line", col_wrap=2, data=df, facet_kws=dict(legend_out=False))
h,l = plot.axes[0].get_legend_handles_labels()
plot.axes[0].legend_.remove()
plot.fig.legend(h,l, ncol=2) # you can specify any location parameter you want here
c9qzyr3d

c9qzyr3d2#

最终解决方案感谢@DizietAsahi

import pandas as pd
import seaborn as sns

columns = ['category1', 'category2', 'category3', 'time', 'value']

data = [['content1', 'other1', 'critera1', 0, 0.1], ['content1', 'other1', 'critera1', 1, 0.4], ['content1', 'other1', 'critera1', 2, 0.7], ['content2', 'other1', 'critera1', 0, 0.2], ['content2', 'other1', 'critera1', 1, 0.6], ['content2', 'other1', 'critera1', 2, 0.8], ['content1', 'other2', 'critera1', 0, 0.0], ['content1', 'other2', 'critera1', 1, 0.2], ['content1', 'other2', 'critera1', 2, 0.8], ['content2', 'other2', 'critera1', 0, 0.3], ['content2', 'other2', 'critera1', 1, 0.6], ['content2', 'other2', 'critera1', 2, 0.5], [
    'content1', 'other1', 'critera2', 0, 0.1], ['content1', 'other1', 'critera2', 1, 0.4], ['content1', 'other1', 'critera2', 2, 0.7], ['content2', 'other1', 'critera2', 0, 0.2], ['content2', 'other1', 'critera2', 1, 0.6], ['content2', 'other1', 'critera2', 2, 0.8], ['content1', 'other2', 'critera2', 0, 0.0], ['content1', 'other2', 'critera2', 1, 0.2], ['content1', 'other2', 'critera2', 2, 0.8], ['content2', 'other2', 'critera2', 0, 0.3], ['content2', 'other2', 'critera2', 1, 0.6], ['content2', 'other2', 'critera2', 2, 0.5], ]

df = pd.DataFrame(data, columns=columns)

plot = sns.relplot(x='time', y='value', col='category3', hue='category1', style='category2', kind="line",
                   col_wrap=2, data=df)

handles, labels = plot.axes[0].get_legend_handles_labels()
plot._legend.remove()
plot.fig.legend(handles, labels, ncol=2, loc='upper center', 
                bbox_to_anchor=(0.5, 1.15), frameon=False)

68bkxrlz

68bkxrlz3#

使用ncol

ax = sns.barplot(x="X", y="Y", data=data)    
ax.legend(loc='upper left',ncol=2, title="Title")
ryhaxcpt

ryhaxcpt4#

@DizietAsahi给出的答案对我的情况不起作用,然而,我能够用sns.move_legend()实用函数做到这一点,该函数可用于修改绘图的图例。

sns.move_legend(ax, "upper center", bbox_to_anchor=(0.5, 1.15), 
                ncol=2)
3duebb1j

3duebb1j5#

import seaborn as sns
import matplotlib.pyplot as plt

# legend and other functionalities will be controlled by matplotlib.pyplot
fig, ax = plt.subplots()

# your plot (it is generated by seaborn)
sns.relplot(...)

# position your legend
lgd = ax.legend(loc = 'upper center', bbox_to_anchor = (0.5, -0.13),
          fancybox=True, shadow=True, ncol=2)

# here bbox_extra_artists and bbox_inches='tight' will make sure the saved figure is not trimming the legend if it goes outside of the figure
plt.savefig("plot.jpg", bbox_extra_artists = (lgd,), bbox_inches = 'tight')

注意:如果图例与轴标签重叠,则可能需要调整bbox_to_anchor
参考文献:

相关问题