2017-07-26 117 views
0

在我seaborn tsplot的顏色不匹配的線畫出:seaborn tsplot:傳奇色彩變淡

Colors don't match the lines drawn.

for item in item_list: 
    sns.tsplot(get_data(), color=get_color(), legend=True) 

sns.plt.legend(labels=item_list) 
sns.plt.show() 

我讀了sns.tsplot和plt.legend文檔頁面,並可以不要爲什麼會這樣。

+0

你的'get_data'和'get_color'函數做了什麼? –

+1

如果您保留指定顏色祕密的方法,您如何期待任何人的幫助? – mwaskom

+0

@ArcturusB GET_DATA返回花車 列表get_color返回一個隨機十六進制顏色字符串 – skend

回答

1

tsplot在線上添加了一些低alpha的區域。即使它們不可見(因爲繪製了一條線),它們仍然可以進入圖例。

一種解決方法是得到直接的情節線:

h = plt.gca().get_lines() 
plt.legend(handles=h, labels=item_list) 

完整的示例:

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

item_list = list("ABXY") 

get_data = lambda : np.random.rand(10) 
get_color = lambda : "#" + "".join(np.random.choice(list("02468acef"), size=6)) 

for item in item_list: 
    sns.tsplot(get_data(), color=get_color()) 

h = plt.gca().get_lines() 
plt.legend(handles=h, labels=item_list) 

plt.show() 

enter image description here

我只想提一下,似乎沒有理由反正在這種情況下使用tsplot。一個簡單的線條圖(plt.plot)就足夠了,並且混淆的可能性較小。擊球碼產生與上述完全相同的輸出。

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

item_list = list("ABXY") 

get_data = lambda : np.random.rand(10) 
get_color = lambda : "#" + "".join(np.random.choice(list("02468acef"), size=6)) 

for item in item_list: 
    plt.plot(get_data(), color=get_color(), label=item) 

plt.legend() 
plt.show() 
+0

謝謝!這個工作完全 – skend

+0

剛剛更新使用'plt.plot',這可能是減少混亂的答案。 – ImportanceOfBeingErnest