绘图传感器布尔数据matplotlib
问题描述:
我有两个传感器的数据,我想要可视化。两个传感器只取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()
答
在提出问题之前,您应该已经解释了这个问题是否有意义。无论如何,下面是例子。
%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'})
如果您的数据设置正确,熊猫会自动绘制x轴与日期时间。试试'df.plot(x ='datetime',y ='data')'。 – lanery