[英]Marker style of legend and the graph are not matching?
下面是我用于生成 plot 的代码,但问题是图中标记的样式与 plot 的样式不同
sns.set_style(rc={'boxplot.flierprops.markeredgecolor':'black' ,'boxplot.flierprops.markeredgewidth':1.25,'boxplot.flierprops.markerfacecolor':'white'})
fig, scatter = plt.subplots(figsize = (6,4), dpi = 100)
scatter = sns.lineplot(data=df_whole,x='shortest_distance',y='similarity',style ='Metric',hue='Metric'
,markers=True,lw=1,markeredgewidth=1.25,markeredgecolor='black',markersize=7,dashes= False,errorbar=None,markerfacecolor='white')
scatter.set(title='TF-IDF')
scatter.legend(title = "Similarity Methods",prop={'size': 12})
由于 seaborn 使用 matplotlib 元素的复杂组合来创建其情节,并试图使图例尽可能紧凑,因此图例通常是定制的。 因此,不幸的是,seaborn 并不总是考虑所有 matplotlib 级参数。
在这种情况下,可以通过将这些参数再次分配给图例句柄来解决该问题。 这是一个使用 seaborn 的测试数据集之一的示例:
import matplotlib.pyplot as plt
import seaborn as sns
flights = sns.load_dataset('flights')
markerprops = dict(markeredgewidth=1.25, markeredgecolor='black', markersize=7, markerfacecolor='none')
ax = sns.lineplot(data=flights, x='year', y='passengers', style='month', hue='month',
markers=True, lw=1, dashes=False, errorbar=None, **markerprops)
ax.set(title='TF-IDF')
handles, labels = ax.get_legend_handles_labels()
for h in handles:
h.set(**markerprops)
ax.legend(handles=handles, title="Months", prop={'size': 12}, ncol=3)
plt.tight_layout()
plt.show()
PS:Matplotlib 函数通常返回它们创建的图形元素(例如散点或线),而 seaborn(和 pandas)通常返回子图( ax
)或子图的网格。 因此,在将代码与其他 matplotlib 和 seaborn 示例进行比较时,将名称scatter
赋予sns.lineplot
的返回值可能会造成混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.