1

I am trying to make a Box and Whisker plot on my dataset that looks something like this -

Dataset

& the chart I'm trying to make

Intended Chart

My current lines of code are below -

import seaborn as sns
import matplotlib.pyplot as plt
d = df3.boxplot(column = ['Northern California','New York','Kansas','Texas'], by = 'Banner')
d

Thank you

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • For future reference, rather than post a picture of some data, it's much better to consider supplying a [reproducible Pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) so that people can just copy it and then plot it or work with it in other ways. – David Buck Mar 02 '20 at 14:06
  • Sure, I'll take care of it going further. Thanks – Chirag Khandelwal Mar 03 '20 at 01:01

1 Answers1

1

I've recreated a dummy version of your dataset:

import numpy as np
import pandas as pd
dictionary = {'Banner':['Type1']*10+['Type2']*10,
              'Northen_californina':np.random.rand(20),
              'Texas':np.random.rand(20)}
df = pd.DataFrame(dictionary)

What you need is to melt your dataframe (unpivot) in orther to have the information of geographical zone stored in a column and not as column name. You can use pandas.melt method and specify all the columns you want to put in your boxplot in the value_vars argument.

With my dummy dataset you can do this:

df = pd.melt(df,id_vars=['Banner'],value_vars=['Northen_californina','Texas'],
             var_name='zone', value_name='amount')

Now you can apply a boxplot using the hue argument:

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(9,9)) #for a bigger image
sns.boxplot(x="Banner", y="amount", hue="zone", data=df, palette="Set1")

output image

TeArge
  • 108
  • 5