【问题标题】:Identify name of default color palette being used by seaborn or matplotlib识别 seaborn 或 matplotlib 使用的默认调色板的名称
【发布时间】:2021-05-14 17:34:24
【问题描述】:

这是当我使用带有分类变量的列为散点着色时,seaborn 默认使用的调色板。
有没有办法获取正在使用的调色板的名称或颜色?强> 我一开始就得到了这个配色方案,但是一旦我对一个情节使用了一个差异方案,我就无法为同一个图表使用这个调色板。 这不是来自sns.color_palette 的方案。这也可以是 matplotlib 配色方案。

最小可重现示例

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

【问题讨论】:

  • 你试过sns.color_pallete() 吗?根据文档,它将返回当前的调色板。
  • 是的,我试过 sns.color_palette。它给出了一些其他颜色非常不同的方案。
  • 如果您提供使用该调色板重现图表的最少代码会有所帮助
  • 来了......
  • 已添加代码。感谢您的观看。

标签: python matplotlib colors seaborn visualization


【解决方案1】:

在您的第一张图中,cylindersint64 类型的连续变量,而 seaborn 使用单一颜色,在本例中为紫色,并用阴影表示值的比例,因此 8 个圆柱体将是比 4 暗。这是故意这样做的,因此您可以通过颜色的深浅轻松分辨出什么是什么。

一旦中途转换为分类,圆柱值之间不再存在这种关系,即 8 个圆柱不再是 4 个圆柱的两倍,它们本质上是两个完全不同的类别。为了避免将颜色的深浅与变量的比例相关联(因为这些值不再连续并且关系不存在),默认情况下将使用分类调色板,这样每种颜色都不同于其他颜色。

为了解决您的问题,您需要在运行最终图表之前将cylinders 转换回int64

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

这会将变量恢复为连续变量,并允许 seaborn 使用相同颜色的渐变来表示值的大小,您的最终绘图将与第一个绘图一样。

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

输出

【讨论】:

    【解决方案2】:

    找回它的一种方法是使用sns.set(). 但这并没有告诉我们配色方案的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      相关资源
      最近更新 更多