2017-06-23 72 views
0

我有一個數據幀,看起來像這樣:情節矩形波蟒蛇

DF:

Start  End Value 
0  0 98999  0 
1 99000 101999  1 
2 102000 155999  0 
3 156000 161999  1 
4 162000 179999  0 

我想繪製一個矩形波,從「開始」到「結束」,並有去「價值」中的值 您能幫我嗎?謝謝!

我嘗試:

for j in range(0,df.shape[0]): 
     plt.figure() 
     plt.plot(range(df['Start'].iloc[j],df['End'].iloc[j]), np.ones(df['End'].iloc[j]-df['Start'].iloc[j])*df['Value'].iloc[j])  

,但它在不同的圖繪製...

+1

它ploting在不同的數字,因爲你有plt.figure()在循環 – Tbaki

回答

0

有點慢......

import matplotlib.pyplot as plt 
l = df.apply(lambda x: ([x.Value for i in range(x.Start,x.End)]),axis=1).sum() 
plt.plot(l) 
plt.show() 

enter image description here

1

也許你可以使用plt.step繪製一個逐步的功能:

import pandas as pd 
from matplotlib import pyplot as plt 

df = pd.DataFrame({'End': [98999, 101999, 155999, 161999, 179999], 
'Start': [0, 99000, 102000, 156000, 162000], 
'Value': [0, 1, 0, 1, 0]}) 

plt.step(df.Start, df.Value, where='post') 

這將每Value其相應Start值繪製到下一個Start

step plot

0

我相信matplotlib.pyplotstep功能是你在找什麼。

你可以嘗試這樣的代碼:

import pandas as pd 
import matplotlib.pyplot as plt 
df = pd.DataFrame([[0, 98999, 0],[99000, 101999, 1],[102000, 155999, 0],[156000, 161999, 1],[162000, 179999, 0]],columns=["Start","End","Value"]) 
plt.step(df["Start"],df["Value"],where="post") 
plt.show() 

這最終會顯示如下圖: enter image description here