2017-01-14 50 views
0

我創建散點圖在我的數據集一堆不同的屬性:熊貓散點圖有X標記和美觀的格式

import numpy as np 
for i, group in df.groupby('property_name'): 
    plt.figure() 
    group.plot(kind='scatter',x='Week', y='true_duration', title=str(i), grid=True) 
    plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2)) 
    plt.show() 

這適用於創建每個屬性的散點圖;然而,他們不擅長的散佈圖看到的這個形象:

scatterplot

我想修復它,所以它不會在x軸重合,我也想它沒有那些奇怪的白色任何一方的空間。

最後,他們是在每個圖下方添加一張表格的方式,並在圖表中對數據進行描述性統計。真的只是尋找平均值,中位數,最大值和最小值。

感謝您的幫助!

我的熊貓DF看起來是這樣的:

property_name Week  true_duration 
A     1   3 
A     3   5 
B     1   2.4 
C     3   5 
C     4   6 
C     5   4 
D     2   1 
+0

能否請您提供的代碼重新'df'? – pbaranay

回答

3

設置
這是我對你有什麼

np.random.seed([3,1415]) 
df = pd.DataFrame(dict(Week=np.random.randint(33, 53, 20), 
         true_duration=np.random.randint(9, 18, 20))) 
df.iloc[-1, :] = pd.Series(dict(Week=1, true_duration=7)) 

df.plot(kind='scatter',x='Week', y='true_duration', grid=True) 
plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2)); 

enter image description here


模擬

選項1
跳過plt.ticks

df.plot(kind='scatter',x='Week', y='true_duration', grid=True) 

enter image description here

選項2
加寬蜱

df.plot(kind='scatter',x='Week', y='true_duration', grid=True) 
plt.xticks(np.arange(min(df['Week']) - 5,max(df['Week']) + 5, 5)); 

enter image description here

選項3
旋轉蜱

df.plot(kind='scatter',x='Week', y='true_duration', grid=True, rot=90) 
plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2)); 

enter image description here

選項4
的一個的任意組合波夫

df.plot(kind='scatter',x='Week', y='true_duration', grid=True, rot=90) 
plt.xticks(np.arange(min(df['Week']) - 5,max(df['Week']) + 5, 5)); 

enter image description here

+0

我已經旋轉過了,這很有幫助,但我仍然在圖表兩側留下空白的空間。你知道怎麼改變這個機會嗎?另外,任何想法如何將一些彙總統計數據添加到每個圖表的底部?感謝幫助(我在我的問題中在我的df中添加了更多代碼)。 @piRSquared –

+0

@JoshDautel摘要Stats是另一個問題。在選項2中注意,我在最大值上加了5,從左邊減了5? – piRSquared

+0

好的,我會嘗試。是的,我不知道是否有一種方法可以使用matplotlib將彙總統計附加到每張表的底部。 –