2016-08-24 47 views
0

我有兩個傳感器的數據,我想要可視化。兩個傳感器只取0/1值。如何更改xaxis標籤以顯示時間序列,y軸應該有2個標籤0和1,表示沿時間序列的傳感器值。繪圖傳感器布爾數據matplotlib

import pandas as pd 
import matplotlib.pyplot as plt 

def drawgraph(inputFile): 
    df=pd.read_csv(inputFile) 
    fig=plt.figure() 
    ax=fig.add_subplot(111) 
    y = df[['sensor1']] 
    x=df.index 
    plt.plot(x,y) 
    plt.show() 
+0

如果您的數據設置正確,熊貓會自動繪製x軸與日期時間。試試'df.plot(x ='datetime',y ='data')'。 – lanery

回答

1

在提出問題之前,您應該已經解釋了這個問題是否有意義。無論如何,下面是例子。

%matplotlib inline 
import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

trange = pd.date_range("11:00", "21:30", freq="30min") 
df = pd.DataFrame({'time':trange,'sensor1':np.round(np.random.rand(len(trange))),\ 
        'sensor2':np.round(np.random.rand(len(trange)))}) 
df = df.set_index('time') 
df.plot(yticks=[0,1],ylim=[-0.1,1.1],style={'sensor1':'ro','sensor2':'bx'}) 

enter image description here