pandas - Creating dataframe boxplot from dataframe with row and column multiindex -
i have following pandas data frame , i'm trying create boxplot of "dur" value both client , server organized qdepth (qdepth on x-axis, duration on y-axis, 2 variables client
, server
). seems need client
, serveras columns. haven't been able figure out trying combinations of
unstackand
reset_index`.
here's dummy data recreated since didn't post yours aside 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 data: df = pd.read_clipboard(sep=',', index_col=[0,1,2])
option 1:
df.unstack(level=1).boxplot()
option 2:
df.unstack(level=[0,1]).boxplot()
option 3:
using seaborn:
import seaborn sns sns.boxplot(x="qdepth", hue="mode", y="dur", data=df.reset_index(),)
update:
to answer comment, here's approximate way (could used starting point) recreate seaborn option using pandas , 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'] # fill boxes desired colors boxcolors = ['darkkhaki', 'royalblue'] numboxes = len(bp['boxes']) in range(numboxes): box = bp['boxes'][i] boxx = [] boxy = [] 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 , royal blue k = % 2 boxpolygon = mpl.patches.polygon(boxcoords, facecolor=boxcolors[k]) ax.add_patch(boxpolygon) plt.show()
Comments
Post a Comment