简体   繁体   English

识别 seaborn 或 matplotlib 使用的默认调色板的名称

[英]Identify name of default color palette being used by seaborn or matplotlib

在此处输入图像描述 This is the color palette, seaborn has used by default when I used a column with categorical variables to color the scatter points.这是调色板,seaborn 在我使用带有分类变量的列为散点着色时默认使用。
Is there a way to get the name or colors of color-palette being used?有没有办法获得正在使用的调色板的名称或 colors? I get this color scheme in the beginning but as soon as I use a diff scheme for a plotly, I can't get to this palette for the same chart.我一开始就得到了这个配色方案,但是一旦我对 plotly 使用了差异方案,我就无法使用同一个图表的这个调色板。 This is not the scheme which comes from sns.color_palette .这不是来自sns.color_palette的方案。 This can also be a matplotlib color scheme.这也可以是 matplotlib 配色方案。

Minimum reproducible example最小可重现示例

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px

# download data
df = pd.read_csv("https://www.statlearning.com/s/Auto.csv")
df.head()

# remove rows with "?"
df.drop(df.index[~df.horsepower.str.isnumeric()], axis=0, inplace=True)
df['horsepower'] = pd.to_numeric(df.horsepower, errors='coerce')

# plot 1 (gives the desired color-palette)
fig = sns.PairGrid(df, vars=df.columns[~df.columns.isin(['cylinders','origin','name'])].tolist(), hue='cylinders')
plt.gcf().set_size_inches(17,15)
fig.map_diag(sns.histplot)
fig.map_upper(sns.scatterplot)
fig.map_lower(sns.kdeplot)
fig.add_legend(ncol=5, loc=1, bbox_to_anchor=(0.5, 1.05), borderaxespad=0, frameon=False);

# plot 2
# Converting column cylinder to factor before using for 'color'
df.cylinders = df.cylinders.astype('category')

# Scatter plot - Cylinders as hue
pal = ['#fdc086','#386cb0','#beaed4','#33a02c','#f0027f']
col_map = dict(zip(sorted(df.cylinders.unique()), pal))
fig = px.scatter(df, y='mpg', x='year', color='cylinders', 
                 color_discrete_map=col_map, 
                 hover_data=['name','origin'])
fig.update_layout(width=800, height=400, plot_bgcolor='#fff')
fig.update_traces(marker=dict(size=8, line=dict(width=0.2,color='DarkSlateGrey')),
                  selector=dict(mode='markers'))
fig.show()

# plot 1 run again
fig = sns.PairGrid(df, vars=df.columns[~df.columns.isin(['cylinders','origin','name'])].tolist(), hue='cylinders')
plt.gcf().set_size_inches(17,15)
fig.map_diag(sns.histplot)
fig.map_upper(sns.scatterplot)
fig.map_lower(sns.kdeplot)
fig.add_legend(ncol=5, loc=1, bbox_to_anchor=(0.5, 1.05), borderaxespad=0, frameon=False);

In your first graph the cylinders is a continuous variable of type int64 and seaborn is using a single color, in this case purple, and shading it to indicate the scale of the value, so that 8 cylinders would be darker than 4. This is done on purpose so you can easily tell what is what by the shade of the color.在您的第一张图中, cylindersint64类型的连续变量,而 seaborn 使用单一颜色,在本例中为紫色,并对其进行着色以指示值的比例,因此 8 个圆柱体将比 4 暗。这已完成故意这样你就可以很容易地通过颜色的深浅来判断什么是什么。

Once you convert to categorical halfway through there is no longer such a relationship between the cylinder values, ie 8 cylinders is not twice as much a 4 cylinders anymore, they are essentially two totally different categories.一旦中途转换为分类,圆柱值之间不再存在这种关系,即 8 个圆柱不再是 4 个圆柱的两倍,它们本质上是两个完全不同的类别。 To avoid associating the shade of color with the scale of the variable (since the values are no longer continuous and the relationship doesn't exist) a categorical color palette will be used by default, such that every color is distinct from the other.为了避免将颜色的深浅与变量的比例相关联(因为值不再连续且关系不存在),默认情况下将使用分类调色板,这样每种颜色都不同于另一种颜色。

In order to solve your problem, you will need to cast cylinders back to int64 prior to running your final chart with为了解决您的问题,您需要在运行最终图表之前将cylinders转换回int64

df.cylinders = df.cylinders.astype('int64')

This will restore the variable to a continuous one and will allow seaborn to use gradients of the same color to represent the size of the values and your final plot will look just like the first one.这会将变量恢复为连续变量,并允许 seaborn 使用相同颜色的渐变来表示值的大小,最终的 plot 看起来就像第一个一样。

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import warnings
warnings.filterwarnings("ignore")

# download data
df = pd.read_csv("https://www.statlearning.com/s/Auto.csv")
df.head()

# remove rows with "?"
df.drop(df.index[~df.horsepower.str.isnumeric()], axis=0, inplace=True)
df['horsepower'] = pd.to_numeric(df.horsepower, errors='coerce')

# plot 1 (gives the desired color-palette)
fig = sns.PairGrid(df, vars=df.columns[~df.columns.isin(['cylinders','origin','name'])].tolist(), hue='cylinders')
plt.gcf().set_size_inches(17,15)
fig.map_diag(sns.histplot)
fig.map_upper(sns.scatterplot)
fig.map_lower(sns.kdeplot)
fig.add_legend(ncol=5, loc=1, bbox_to_anchor=(0.5, 1.05), borderaxespad=0, frameon=False);

# plot 2
# Converting column cylinder to factor before using for 'color'
df.cylinders = df.cylinders.astype('category')

# Scatter plot - Cylinders as hue
pal = ['#fdc086','#386cb0','#beaed4','#33a02c','#f0027f']
col_map = dict(zip(sorted(df.cylinders.unique()), pal))
fig = px.scatter(df, y='mpg', x='year', color='cylinders', 
                 color_discrete_map=col_map, 
                 hover_data=['name','origin'])
fig.update_layout(width=800, height=400, plot_bgcolor='#fff')
fig.update_traces(marker=dict(size=8, line=dict(width=0.2,color='DarkSlateGrey')),
                  selector=dict(mode='markers'))
fig.show()

# plot 1 run again
df.cylinders = df.cylinders.astype('int64')
fig = sns.PairGrid(df, vars=df.columns[~df.columns.isin(['cylinders','origin','name'])].tolist(), hue='cylinders')
plt.gcf().set_size_inches(17,15)
fig.map_diag(sns.histplot)
fig.map_upper(sns.scatterplot)
fig.map_lower(sns.kdeplot)
fig.add_legend(ncol=5, loc=1, bbox_to_anchor=(0.5, 1.05), borderaxespad=0, frameon=False);

Output Output

One way to get it back is with sns.set().恢复它的一种方法是使用sns.set(). But that doesn't tell us the name of the color scheme.但这并没有告诉我们配色方案的名称。

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

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