16

Seaborn では、複数の色を含むカラー パレットを定義できるため、線の多いチャートに役立ちます。ただし、パレットを複数の色を持つパレットに設定すると、最初の 6 つだけが使用され、その後は色が循環するため、線の区別が難しくなります。これは、パレットを明示的に呼び出すことでオーバーライドできますが、これは便利ではありません。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)

6色のパレット

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

6色のパレット

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))  ;

6 色または 8 色を使用したチャート

4

1 に答える 1

11

解決策(ポインタの@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) )

ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力

# 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)) ;

ここに画像の説明を入力

于 2014-10-10T14:44:36.143 に答える