简体   繁体   English

多个 seaborn 图未在图例上显示正确的颜色

[英]Multiple seaborn plots not showing the right color on the legend

I have three point plot i'm trying to chart and show a legend.我有一个三点图,我正在尝试绘制图表并显示一个图例。 The colors do not match the colors called out in the plots.颜色与图中标出的颜色不匹配。 I tried using the solution from Legend not showing when plotting multiple seaborn plots but that did not work.我尝试使用Legend 中的解决方案在绘制多个 seaborn 图时未显示,但这不起作用。

Here is the code I'm using:这是我正在使用的代码:

fig, ax = plt.subplots()
a = sns.pointplot(x=l[1:], y = np.exp(model_m.params[1:]), label = 'factor',
              ax = ax, color = 'green')

b = sns.pointplot(x=l[1:], y = np.exp(model_m.conf_int()[1:][:,1]), 
              ax = ax, label = 'conf_int+', color = 'red')

c = sns.pointplot(x=l[1:], y = np.exp(model_m.conf_int()[1:][:,0]), 
              ax = ax, label = 'conf_int-', color = 'blue')
plt.title('Model M Discrete')
ax.legend(labels = ['factor', 'conf_inf+', 'conf_inf-'],
           title = 'legend')

Here is what it produces:这是它产生的:

在此处输入图片说明

The easiest solution would be to use sns.lineplot instead of sns.pointplot :最简单的解决方案是使用sns.lineplot而不是sns.pointplot

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

fig, ax = plt.subplots()
x = np.arange(10)
sns.lineplot(x=x, y=1 + np.random.rand(10).cumsum(),
             ax=ax, label='factor', color='green', marker='o')
sns.lineplot(x=x, y=2 + np.random.rand(10).cumsum(),
             ax=ax, label='conf_int+', color='red', marker='o')
sns.lineplot(x=x, y=3 + np.random.rand(10).cumsum(),
             ax=ax, label='conf_int-', color='blue', marker='o')
ax.set_title('Model M Discrete')
ax.legend(title='legend')
plt.tight_layout()
plt.show()

带有图例的 sns.lineplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM