3

I have the following Pandas data frame and I'm trying to create a boxplot of the "dur" value for both client and server organized by qdepth (qdepth on x-axis, duration on y-axis, with two variables client and server). It seems like I need to get client and serveras columns. I haven't been able to figure this out trying combinations ofunstackandreset_index`.

example

1
  • It would help of if you could post a sample of your data in a format that we can import directly in pandas using pd.read_clipboard() (test it on your machine). Also, maybe a sketch of the boxplot you want to get would go a long way. Commented Nov 13, 2016 at 9:37

1 Answer 1

4

Here's some dummy data I recreated since you didn't post yours aside from an image:

qdepth,mode,runid,dur
1,client,0x1b7bd6ef955979b6e4c109b47690c862,7.0
1,client,0x45654ba030787e511a7f0f0be2db21d1,30.0
1,server,0xb760550f302d824630f930e3487b4444,19.0
1,server,0x7a044242aec034c44e01f1f339610916,95.0
2,client,0x51c88822b28dfa006bf38603d74f9911,15.0
2,client,0xd5a9028fddf9a400fd8513edbdc58de0,49.0
2,server,0x3943710e587e3932adda1cad8eaf2aeb,30.0
2,server,0xd67650fd984a48f2070de426e0a942b0,93.0

Load the data: df = pd.read_clipboard(sep=',', index_col=[0,1,2])

Option 1:

df.unstack(level=1).boxplot()

enter image description here

Option 2:

df.unstack(level=[0,1]).boxplot()

Option 2

Option 3:

Using seaborn:

import seaborn as sns
sns.boxplot(x="qdepth", hue="mode", y="dur", data=df.reset_index(),)

enter image description here

Update:

To answer your comment, here's a very approximate way (could be used as a starting point) to recreate the seaborn option using only pandas and matplotlib:

fig, ax = plt.subplots(nrows=1,ncols=1, figsize=(12,6))
#bp = df.unstack(level=[0,1])['dur'].boxplot(ax=ax, return_type='dict')

bp = df.reset_index().boxplot(column='dur',by=['qdepth','mode'], ax=ax, return_type='dict')['dur']

# Now fill the boxes with desired colors
boxColors = ['darkkhaki', 'royalblue']
numBoxes = len(bp['boxes'])
for i in range(numBoxes):
    box = bp['boxes'][i]
    boxX = []
    boxY = []
    for j in range(5):
        boxX.append(box.get_xdata()[j])
        boxY.append(box.get_ydata()[j])
    boxCoords = list(zip(boxX, boxY))
    # Alternate between Dark Khaki and Royal Blue
    k = i % 2
    boxPolygon = mpl.patches.Polygon(boxCoords, facecolor=boxColors[k])
    ax.add_patch(boxPolygon)

plt.show()

enter image description here

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

2 Comments

Thanks for the ansers. The Seaborn "hue" parameter seems to have been what I was looking for. It's not relevant for accepting this answer, but any way to achieve the groupings in vanilla Pandas without Seaborn?
Seaborn is litterally using vanilla matplotlib like pandas, so yes there is a way. You could look into Seaborn's source code (start here), and also this matplotlib example that shows how to color the boxes. (PS: an upvote would be nice in addition to accepting the answer)

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.