3

So I have a data frame like

exp_name, index, items, clicks
"foo",0, "apple",200
"foo",0, "banana", 300
"foo",0,"melon",220
"foo",1, "apple", 10
"foo",1,"peach", 20
"bar",0, "apple",400
"bar",0,'banana', 500
"bar",0, "melon",240
"bar",1,"apple",500

and so on

I want to plot... for each experiment name... bar plots of number of clicks for each item in each index but colored by index. So basically.. plot 1.. for experiment "foo", a bar plots.. where index == 0.. all the barplots for index 0 in one color.. index 1 in another color.

if the item is missing (for example peach is in "foo", 1 but not in any other place) replace "peach" to be zero in other places.

1 Answer 1

4

I copy/paste your data into a txt file called 'test.txt' and rename "index" as "status" to avoid confusion with the DataFrame index. Then I use the Seaborn library to make barplots with the contingencies you mention (and as I understand them). I use subplots rather than use color to set apart "status" cause I personally think it looks cleaner, but I use colors below since that's what you asked about.

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

df = pd.read_csv('test.txt')  
fig, ax = sns.plt.subplots(1, 1, figsize=(7,5))
sns.factorplot(x="items", y="clicks", hue="exp_name", col="status", data=df, kind="bar")
plt.show()

Gives the following:enter image description here

If you really want to distinguish "index" (what I call "status") by color, you might define a new variable which combines "exp_name" with "status"

df['exp'] = df.exp_name + df.status.astype(str)
sns.factorplot(x="items", y="clicks", hue="exp", data=df, kind="bar")

Gives something like this

enter image description here

Check out the docs for seaborn if you have more questions. It's a really great library for categorical data. Changing the legend labels and other settings follows matplotlib conventions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.