python - Seaborn Striplot 数据可视化未将颜色应用于标记/条/条

标签 python matplotlib seaborn visualization

当使用seaborn创建带状图时,代码完美地创建了带状图。将颜色应用于图例和所有内容。除了颜色不适用于 strip 图中的各个 strip 之外。感谢这里的seaborn/matplotlib专家,因为我很茫然。代码如下。下面附上我的结果图片。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#set seaborn plotting aesthetics
sns.set_style("whitegrid")

data = [[2016.0, 0.4862, 0.4115, 0.3905, 0.3483, 0.1196],
 [2017.0, 0.4471, 0.4096, 0.3725, 0.2866, 0.1387],
 [2018.0, 0.4748, 0.4016, 0.3381, 0.2905, 0.2012],
 [2019.0, 0.4705, 0.4247, 0.3857, 0.3333, 0.2457],
 [2020.0, 0.4755, 0.4196, 0.3971, 0.3825, 0.2965]]

# cols = ['attribute_time', '100-81%', '80-61%', '60-41%', '40-21%', '20-0%']
cols = ['attribute_time', '100-81 percentile', '80-61 percentile', '60-41 percentile', '40-21 percentile', '20-0 percentile']

df = pd.DataFrame(data, columns=cols)

# Just to get rid of the decimals.
df['attribute_time'] = df['attribute_time'].astype('int') 
print(df)
df.columns = ['attribute_time', '100-81 percentile', '80-61 percentile', '60-41 percentile', '40-21 percentile', '20-0 percentile']
df = df.melt(id_vars = ['attribute_time'],
    value_name = 'pct_value',
    var_name = 'pct_range')
print(df.head(20))

print(df['pct_range'].unique())

# # Create a dictionary mapping subgroup values to colors
palette_colors = dict(zip(list(df['pct_range'].unique()), ['blue', 'orange', 'green', 'red', 'purple']))
print(palette_colors)

fig, ax = plt.subplots()

for year, value in zip(df['attribute_time'],df['pct_value']):
    ax.text(year - 2016, value, str(value), ha = 'center', va = 'bottom', fontsize = 'small',)

sns.stripplot(
    data = df,
    x = 'attribute_time',
    y = 'pct_value',
    hue = 'pct_range',
    palette=palette_colors,
    jitter = False,
    marker = '_',
    size = 25,
    linewidth = 2,
    ax = ax
).legend(fontsize=7)

plt.show()

stripplot without colors appliers to markers

最佳答案

Matplotlib 有两种类型的标记。大多数都是填充的(例如带边框的圆圈)。有些是未填充的(例如水平线)。

Seaborn 仅对内部使用色调颜色,并对边框(边缘)使用固定颜色(默认黑色)。如果我尝试运行您的代码,我会收到来自 matplotlib 的警告,提示为未填充的标记提供了面部颜色。您只需输入 edgecolor='face' 即可为边缘提供面部颜色。 (Matplotlib 仍然给出相同的警告,但确实通过色调颜色为标记着色。)

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# set seaborn plotting aesthetics
sns.set_style("whitegrid")

data = [[2016.0, 0.4862, 0.4115, 0.3905, 0.3483, 0.1196],
        [2017.0, 0.4471, 0.4096, 0.3725, 0.2866, 0.1387],
        [2018.0, 0.4748, 0.4016, 0.3381, 0.2905, 0.2012],
        [2019.0, 0.4705, 0.4247, 0.3857, 0.3333, 0.2457],
        [2020.0, 0.4755, 0.4196, 0.3971, 0.3825, 0.2965]]
cols = ['attribute_time', '100-81 percentile', '80-61 percentile', '60-41 percentile', '40-21 percentile', '20-0 percentile']

df = pd.DataFrame(data, columns=cols)
# Just to get rid of the decimals.
df['attribute_time'] = df['attribute_time'].astype('int')
df_long = df.melt(id_vars=['attribute_time'],
             value_name='pct_value',
             var_name='pct_range')
# Create a dictionary mapping subgroup values to colors
palette_colors = dict(zip(list(df_long['pct_range'].unique()), plt.cm.get_cmap('Set1').colors))

fig, ax = plt.subplots()

for year, value in zip(df_long['attribute_time'], df_long['pct_value']):
     ax.text(year - 2016, value, str(value), ha='center', va='bottom', fontsize='small', )

sns.stripplot(data=df_long,
              x='attribute_time',
              y='pct_value',
              hue='pct_range',
              palette=palette_colors,
              jitter=False,
              marker='_',
              size=25,
              linewidth=2,
              edgecolor='face',
              ax=ax)
ax.legend(fontsize=7)

plt.show()

sns.stripplot with edgecolor='face'

关于python - Seaborn Striplot 数据可视化未将颜色应用于标记/条/条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74672393/

相关文章:

python - 有没有更好的方法来查找字符串是否包含数字?

python - 在 python 图中添加额外信息?

python - 在 pandas 的 groupby 命令后与 seaborn 一起绘制

python - 如何从没有索引的列中获取数据

python - 如何在 matplotlib 中添加悬停注释

python - seaborn:lmplot() 得到一个意外的关键字参数 'figsize'

python - Python3中 map 的非惰性评估版本?

python - 将包含天数的日期字符串转换为浮点型日期时间对象

python - 合并两列,忽略空单元格并添加分隔符

python - 使用 mplfinance/matplotlib 时可能出现内存泄漏。如何解决?