[英]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.