user12363817
user12363817

Reputation: 21

How to color a Seaborn boxplot using a different dataframe than the one plotted in Python?

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.

  1. Import libraries:
import numpy as np
import pandas as pd
import seaborn as sns
  1. Create dataset
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
  1. Make the boxplot
sns.boxplot(data=dataset)

Boxplot figure

  1. Here's the dataset that I want to use to color the boxplot. Basically, I want A and B to be red ('No') and C to be green ('Yes').
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

Answers (1)

Seb
Seb

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

Related Questions