2014-10-10 15 views
10

Seaborn cho phép xác định bảng màu có chứa nhiều màu, hữu ích cho các biểu đồ có nhiều dòng. Tuy nhiên, khi thiết lập bảng màu thành một với nhiều màu, chỉ có sáu màu đầu tiên được sử dụng, sau đó màu sắc tái chế, làm cho khó phân biệt các dòng. Điều này có thể được ghi đè bằng cách gọi một cách rõ ràng bảng màu, nhưng điều đó không thuận tiện. Có cách nào để buộc bảng màu hiện tại của Seaborn không tái chế màu sắc, khi hơn 6 được xác định?Bảng màu Seaborn - ngăn ngừa tái chế màu

Ví dụ:

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

mùi giống như một lỗi sanh ở biển đối với tôi. – tacaswell

+0

Tôi cũng vậy, nhưng tôi không muốn đưa ra giả định – iayork

+1

Thực ra, tôi không nghĩ vậy: http://web.stanford.edu/~mwaskom/software/seaborn/generated/seaborn.color_palette.html Có vẻ như nó đang làm những gì nó được cho là, nó chỉ là gây phiền nhiễu. – tacaswell

Trả lời

8

Solution (t nùi để @tcaswell cho con trỏ): Thiết lập các bảng sử dụng một cách rõ ràng tất cả các màu:

# 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

Xem tại đây để biết thêm thông tin: https://github.com/mwaskom/seaborn/issues/314 – mwaskom

Các vấn đề liên quan