Reputation: 21
I'm trying to create a boxplot using Seaborn that has data from one dataset but is colored based on a different dataset. Below is a smaller sized dataset as an example. The dataset I'm actually working with is much larger.
import numpy as np
import pandas as pd
import seaborn as sns
data = ([[0.038095,0.259664,-0.016144],
[0.070850,0.533989,0.221025],
[0.010452,0.108146,0.007267],
[0.033338,0.006664,0.160160],
[0.005897,0.060313,-0.001070],
[0.089018,0.002074,0.409608],
[-0.010612,0.006957,0.331146],
[-0.002889,0.005181,0.928332]])
dataset = pd.DataFrame(data,columns=['A','B','C'])
dataset
sns.boxplot(data=dataset)
type = ([
['A', 'No'],
['B','No'],
['C', 'Yes']])
type_dataset = pd.DataFrame(type); type_dataset
So, how can I get the boxplots to be colored according to the type dataset? Do I need to merge the datasets? Any/all help is appreciated! Thank you.
Upvotes: 2
Views: 418
Reputation: 4576
Use the palette
keyword argument to boxplot
:
sns.boxplot(data=dataset, palette=['r', 'r', 'g'])
or, taking the metadata from your type_dataset
object:
cmap = {'Yes': 'green', 'No': 'red'}
sns.boxplot(data=dataset, palette=[cmap[v] for v in type_dataset[1]])
Upvotes: 1