2014-10-10 78 views
10

Seaborn允許定義包含多種顏色的調色板,適用於多行圖表。但是,當將調色板設置爲多種顏色時,只會使用前六種顏色,之後顏色將循環使用,從而難以區分線條。這可以通過顯式調用調色板來覆蓋,但這不方便。有沒有辦法強制Seaborn電流調色板不要回收顏色,當超過6個定義?Seaborn調色板 - 防止顏色回收

實施例:

from matplotlib import pyplot as plt 
import pandas as pd 
import seaborn as sb 

# Define a palette with 8 colors 
cmap = sb.blend_palette(["firebrick", "palegreen"], 8) 
sb.palplot(cmap) 

palette with 6 colors

# Set the current palette to this; only 6 colors are used 
sb.set_palette(cmap) 
sb.palplot(sb.color_palette()) 

palette with 6 colors

df = pd.DataFrame({x:[x*10, x*10+5, x*10+10] for x in range(8)}) 
fig, (ax1, ax2) = plt.subplots(2,1,figsize=(4,6)) 
# Using the current palette, colors repeat 
df.plot(ax=ax1) 
ax1.legend(bbox_to_anchor=(1.2, 1)) 
# using the palette that defined the current palette, colors don't repeat 
df.plot(ax=ax2, color=cmap) 
ax2.legend(bbox_to_anchor=(1.2, 1)) ; 

charts with 6 or 8 colors used

+0

聞起來像一個seaborn錯誤給我。 – tacaswell 2014-10-10 14:08:27

+0

我也是,但我不想做出假設 – iayork 2014-10-10 14:11:58

+1

實際上,我不這麼認爲:http://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.color_palette.html它看起來像它正在做它應該做的事情,它只是令人討厭。 – tacaswell 2014-10-10 14:13:37

回答

8

溶液(T漢克斯@tcaswell的指針):設置調色板顯式地使用所有的顏色:

# Setting the palette using defaults only finds 6 colors 
sb.set_palette(cmap) 
sb.palplot(sb.color_palette()) 
sb.palplot(sb.color_palette(n_colors=8)) 

# but setting the number of colors explicitly allows it to use them all 
sb.set_palette(cmap, n_colors=8) 
# Even though unless you explicitly request all the colors it only shows 6 
sb.palplot(sb.color_palette()) 
sb.palplot(sb.color_palette(n_colors=8)) 

enter image description here enter image description here enter image description here enter image description here

# In a chart, the palette now has access to all 8 
fig, ax1 = plt.subplots(1,1,figsize=(4,3)) 
df.plot(ax=ax1) 
ax1.legend(bbox_to_anchor=(1.2, 1)) ; 

enter image description here

+1

詳情請參閱https://github.com/mwaskom/seaborn/issues/314 – mwaskom 2014-10-10 14:50:03