[英]Matplotlib / Seaborn legend changes style upon adding labels
我正在使用seaborn从pandas df绘制一些数据。 使用以下代码,几乎所有内容都能很好地绘制:
import pandas as pd
import seaborn as sns
sns.set(style='whitegrid', palette='muted')
legend = ["Hue 1", "Hue 2"]
order = ["A", "B"]
ax = sns.violinplot(x=df.xaxis, y=df.yaxis, hue=df.hue,
split=True, order=order)
ax.set_ylim(0, 100)
ax.set(xlabel='X - axis', ylabel='Y - axis')
ax.legend(title='Legend', loc='upper left', labels=legend)
ax.set_title('My little plot')
plt.show()
添加labels=
后,图例中显示的“线型”就会发生变化。 以下是屏幕截图。 不幸的是,我的数据集太大而无法发布,所以我希望足够。
提前致谢。 BBQuercus :)
左侧不带labels
,右侧带labels
(R,C是数据中的值)。
您可以尝试为图例绘制自定义补丁。 我尚未对此进行测试,但我认为它应该可以工作。
from matplotlib.patches import Patch
palette=sns.color_palette('muted')
bluepatch = Patch(
facecolor=palette[0],edgecolor='k',label='Hue 1'
)
orangepatch = Patch(
facecolor=palette[1],edgecolor='k',label='Hue 2'
)
ax.legend(
labels=['Hue 1','Hue 2'],
handles=[bluepatch, orangepatch],
title='Legend',
loc='upper left'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.