2017-06-04 59 views
-2

這是我的pandas DataFrame。熊貓:用標記繪製數據框對象?

value action 
0  1  0 
1  2  1 
2  3  1 
3  4  1 
4  3  0 
5  2  1 
6  5  0 

我想要做什麼是馬克valueo如果action=0x如果action=1

因此,該地塊的標記應該是這樣的: o x x x o x o

,但不知道如何做到這一點?

需要你的幫助。謝謝。

回答

1

考慮以下的方法:

plt.plot(df.index, df.value, '-X', markevery=df.index[df.action==1].tolist()) 
plt.plot(df.index, df.value, '-o', markevery=df.index[df.action==0].tolist()) 

結果:

enter image description here

替代解決方案:

plt.plot(df.index, df.value, '-') 
plt.scatter(df.index[df.action==0], df.loc[df.action==0, 'value'], 
      marker='o', s=100, c='green') 
plt.scatter(df.index[df.action==1], df.loc[df.action==1, 'value'], 
      marker='x', s=100, c='red') 

結果:

enter image description here

1

您可以繪製過濾的數據幀。也就是說,您可以創建兩個數據框,一個用於動作0,一個用於動作1.然後分別繪製每個數據框。

import pandas as pd 

df = pd.DataFrame({"value":[1,2,3,4,3,2,5], "action":[0,1,1,1,0,1,0]}) 
df0 = df[df["action"] == 0] 
df1 = df[df["action"] == 1] 

ax = df.reset_index().plot(x = "index", y="value", legend=False, color="gray", zorder=0) 
df0.reset_index().plot(x = "index", y="value",kind="scatter", marker="o", ax=ax) 
df1.reset_index().plot(x = "index", y="value",kind="scatter", marker="x", ax=ax) 

enter image description here

+0

謝謝,但我想補充'上線圖marker'。我怎樣才能做到這一點? – user3595632

+0

一個線圖可以通過'.plot(kind =「line」,...)'來完成。 – ImportanceOfBeingErnest

相關問題