2016-11-13 80 views
2

我有以下的熊貓數據幀,我試圖創造以qdepth舉辦的「杜爾」爲客戶端和服務器值的箱線圖(X軸qdepth,在y軸上的持續時間,具有兩個變量clientserver)。好像我需要得到client和服務器as columns. I haven't been able to figure this out trying combinations of拆散and reset_index`。創建從數據幀數據幀箱線行和列多指標

example

+0

如果你可以用我們可以使用pd.read_clipboard()直接導入熊貓的格式發佈你的數據樣本,這將有助於你的數據(在你的測試中機)。 此外,也許你想得到的boxplot草圖將會走很長的路。 –

回答

1

這是我重新因爲你沒有從圖像上傳你留出一些虛擬數據:

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 

加載數據:df = pd.read_clipboard(sep=',', index_col=[0,1,2])

選項1:

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

enter image description here

選項2:

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

Option 2

選項3:

使用seaborn:

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

enter image description here

更新:

爲了回答您的評論,這裏有一個非常近似的方式(可以作爲一個起點)只用熊貓和matplotlib重新創建seaborn選項:

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

+0

謝謝你的指導。 Seaborn的「色調」參數似乎是我一直在尋找的。接受這個答案並不相關,但是在沒有Seaborn的情況下以任何方式實現香草熊貓分組? –

+0

Seaborn使用像熊貓這樣的香草matplotlib進行腸道照明,所以是的,有一種方法。你可以看看Seaborn的源代碼(啓動[這裏](https://github.com/mwaskom/seaborn/blob/27f195df8bf8964d187fb14d0f90fb1aeba1b214/seaborn/categorical.py#L427)),並且也是這個matplotlib [示例](HTTP:/ /matplotlib.org/examples/pylab_examples/boxplot_demo2.html),它顯示瞭如何爲框着色。 (PS:一個[給予好評](http://meta.stackexchange.com/questions/13390/when-you-accept-an-answer-should-you-also-vote-it-up)。將除了很好接受答案) –