当使用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()
最佳答案
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()
关于python - Seaborn Striplot 数据可视化未将颜色应用于标记/条/条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74672393/