Constellation
Constellation

Reputation: 35

stripplot with seaborn, colored by rows

I have created a combo stripplot and cannot color by row. I have tried all combinationf of hue, color, palette, library and did not work. My code is:

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

Upvotes: 2

Views: 1183

Answers (1)

JohanC
JohanC

Reputation: 80509

Seaborn's stripplot() doesn't seem to support colors for individual points. However, a stripplot() can be simulated via ax.scatter using a randomly jittered x-position. A list of colors can be created by mapping the given dictionary to the ARL_CODE column.

Here is some example code (using a loop to avoid code copying):

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

Upvotes: 4

Related Questions