Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python从dataframe绘制多条曲线_Python_Pandas_Matplotlib_Plot - Fatal编程技术网

python从dataframe绘制多条曲线

python从dataframe绘制多条曲线,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我试图在一个图上画出几个电器的温度 数据来自下面的dataframe df,我首先创建date列作为索引 df=df.set_index('Date') Date Appliance Value (degrees) 2016-07-05 03:00:00 Thermometer 22 2016-08-06 16:00:00 Thermometer . 19 2016-12-07 21:00:00 . Thermometer . 25

我试图在一个图上画出几个电器的温度

数据来自下面的dataframe df,我首先创建date列作为索引

df=df.set_index('Date')

Date                  Appliance      Value (degrees)
2016-07-05 03:00:00   Thermometer    22
2016-08-06 16:00:00   Thermometer .  19
2016-12-07 21:00:00 . Thermometer .  25
2016-19-08 23:00:00 . Thermostat .   21
2016-25-09 06:00:00 . Thermostat .   20
2016-12-10 21:00:00 . Thermometer .  18
2016-10-11 21:00:00 . Thermostat .   21
2016-10-12 04:00:00 . Thermometer .  20
2017-01-01 07:00:00 . Thermostat .   19
2017-01-02 07:00:00 . Thermometer .  23
我们希望能够显示两条曲线:一条是温度计的温度曲线,另一条是恒温器的温度曲线,随时间变化有两种不同的颜色

plt.plot(df.index, [df.value for i in range(len(appliance)]
ax = df.plot()
ax.set_xlim(pd.Timestamp('2016-07-05'), pd.Timestamp('2015-11-30'))
这样更好吗


我无法做到这一点

当然有几种方法来绘制数据。 假设我们有这样一个数据帧

import pandas as pd

dates = ["2016-07-05 03:00:00", "2016-08-06 16:00:00", "2016-12-07 21:00:00", 
         "2016-19-08 23:00:00", "2016-25-09 06:00:00", "2016-12-10 21:00:00", 
         "2016-10-11 21:00:00", "2016-10-12 04:00:00", "2017-01-01 07:00:00", 
         "2017-01-02 07:00:00"]       
app = ["Thermometer","Thermometer","Thermometer","Thermostat","Thermostat","Thermometer",
       "Thermostat","Thermometer","Thermostat","Thermometer"]     
values = [22,19,25,21,20,18,21,20,19,23]  
df = pd.DataFrame({"Date" : dates, "Appliance" : app, "Value":values})
df.Date = pd.to_datetime(df['Date'], format='%Y-%d-%m %H:%M:%S')  
df=df.set_index('Date')
使用matplotlib pyplot.plot 使用DataFrame.plot
import matplotlib.pyplot as plt

df1 = df[df["Appliance"] == "Thermostat"]
df2 = df[df["Appliance"] == "Thermometer"]

plt.plot(df1.index, df1["Value"].values, marker="o", label="Thermostat")
plt.plot(df2.index, df2["Value"].values, marker="o", label="Thermmeter")
plt.gcf().autofmt_xdate()
plt.legend()
df1 = df[df["Appliance"] == "Thermostat"]
df2 = df[df["Appliance"] == "Thermometer"]

ax = df1.plot(y="Value", label="Thermostat")
df2.plot(y="Value", ax=ax, label="Thermometer")
ax.legend()