pandas - 带seaborn的条形图,按行着色

标签 pandas dataframe seaborn

我创建了一个组合条形图,无法按行着色。我已经尝试了色调、颜色、调色板、库的所有组合,但没有成功。我的代码是:

fig, axes=plt.subplots(1,5,figsize=(11,8))
my_new_color={'UA':'white','AA':'blueviolet','US':'#98F5FF',
         'F9':'#FF9912','B6':'#66CD00','OO':'green','AS':'#3D59AB','NK':'black',
         'WN':'red','DL':'yellow','EV':'#98F5FF','HA':'#FFF8DC','MQ':'#7FFF00',
         'VX':'pink'}

sns.boxplot(ax=axes[0],y='AIR_SYSTEM_DELAY', data=ARL_DA_reason3,linewidth=1, width=0.55)
sns.stripplot(ax=axes[0],y='AIR_SYSTEM_DELAY', data=ARL_DA_reason3, size=10, color='yellow')

sns.boxplot(ax=axes[1], data=ARL_DA_reason3, y='SECURITY_DELAY',linewidth=1, width=0.55)
sns.stripplot(ax=axes[1],y='SECURITY_DELAY', data=ARL_DA_reason3, color='yellow',size=10)

sns.boxplot(ax=axes[2], data=ARL_DA_reason3, y='AIRLINE_DELAY',linewidth=1, width=0.55)
sns.stripplot(ax=axes[2],y='AIRLINE_DELAY', data=ARL_DA_reason3, color='yellow',size=10)

sns.boxplot(ax=axes[3], data=ARL_DA_reason3, y='LATE_AIRCRAFT_DELAY',linewidth=1, width=0.55)
sns.stripplot(ax=axes[3],y='LATE_AIRCRAFT_DELAY', data=ARL_DA_reason3, color='yellow',size=10)

sns.boxplot(ax=axes[4], data=ARL_DA_reason3, y='WEATHER_DELAY',linewidth=1, width=0.55)
sns.stripplot(ax=axes[4],y='WEATHER_DELAY', data=ARL_DA_reason3, color='yellow',size=10)

enter image description here

enter image description here

最佳答案

Seaborn 的 stripplot() 似乎不支持单个点的颜色。但是,可以使用随机抖动的 x 位置通过 ax.scatter 来模拟 stripplot()。可以通过将给定字典映射到 ARL_CODE 列来创建颜色列表。

这里是一些示例代码(使用循环来避免代码复制):

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

sns.set()
my_new_color = {'UA': 'white', 'AA': 'blueviolet', 'US': '#98F5FF', 'F9': '#FF9912', 'B6': '#66CD00', 'OO': 'green',
                'AS': '#3D59AB', 'NK': 'black', 'WN': 'red', 'DL': 'yellow', 'EV': '#98F5FF', 'HA': '#FFF8DC',
                'MQ': '#7FFF00', 'VX': 'pink'}
# create some test data similar to the OP
df = pd.DataFrame({'ARL_CODE': np.random.choice(list(my_new_color.keys()), 50),
                   'AIR_SYSTEM_DELAY': np.random.uniform(100000, 1800000, 50),
                   'SECURITY_DELAY': np.random.uniform(1000, 17000, 50),
                   'AIRLINE_DELAY': np.random.uniform(100000, 4000000, 50)})
colors = df['ARL_CODE'].map(my_new_color)

y_columns = ['AIR_SYSTEM_DELAY', 'SECURITY_DELAY', 'AIRLINE_DELAY']
fig, axes = plt.subplots(ncols=len(y_columns), figsize=(11, 8))

for y_col, ax in zip(y_columns, axes):
    sns.boxplot(y=y_col, data=df, linewidth=1, width=0.55, ax=ax)
    ax.scatter(x=np.random.uniform(-0.2, 0.2, len(df)), y=df[y_col], s=100, color=colors)
    ax.set_ylabel('')
    ax.set_title(y_col)
plt.tight_layout()
plt.show()

combining sns.stripplot and sns.boxplot

关于pandas - 带seaborn的条形图,按行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69228458/

相关文章:

python - 通过重新索引将行插入数据帧

python - 使用每个组的不同列来计算组内的出现次数

python seaborn : alpha by hue

python - Matplotlib:我怎么知道正在使用哪个颜色图?

python - 如何将值从 1 列分配给另一列并在 Pandas 中发出警告

python - 根据 pandas 中的条件更改数据框的第一行

python - 如何提取数据框中不存在于另一个数据框中的行

r - .subset2(x, i, exact = exact) 错误 : subscript out of bounds in R

python - 在 clustermap 上为等于零的值设置特定颜色

python - 如何只修改数字变量python