Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 使用pandas在matplotlib中打印_Python_Pandas_Numpy_Matplotlib_Plot - Fatal编程技术网

Python 使用pandas在matplotlib中打印

Python 使用pandas在matplotlib中打印,python,pandas,numpy,matplotlib,plot,Python,Pandas,Numpy,Matplotlib,Plot,我试图用2个y轴和一个共享的x轴进行绘图。我已经到了这一点,但它不是我想要的x轴 这是我的密码: import pandas as pd import matplotlib.pyplot as plt df = pd.read_excel("Live_data_test.xlsx","Sheet1") timedat = df.loc[:, 'Time'] temperaturedat = df.loc[:, 'Temperature'] humiditydat = df.loc[:,

我试图用2个y轴和一个共享的x轴进行绘图。我已经到了这一点,但它不是我想要的x轴

这是我的密码:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel("Live_data_test.xlsx","Sheet1")



timedat = df.loc[:, 'Time']
temperaturedat = df.loc[:, 'Temperature']
humiditydat = df.loc[:, 'Humidity']

fig, ax1 = plt.subplots()

ax2 = ax1.twinx()
ax1.plot(timedat, temperaturedat, 'g-')
ax2.plot(timedat, humiditydat, 'b-')

ax1.set_xlabel('Time')
ax1.set_ylabel('Temperature', color='g')
ax2.set_ylabel('Humidity', color='b')

plt.show()
x轴打印为0,1,2,。。。不管我有多少观点。它不是绘制定义的x轴,该x轴应该是我在电子表格中设置的unix时间

电子表格:

Time    Temperature Humidity
1513753200  54  45
1513753201  55  48
1513753202  55  50
1513753203  56  52
1513753204  56  54
1513753205  57  54
1513753206  56  54
1513753207  56  55
1513753208  56  56
1513753209  55  56
1513753210  54  52
1513753211  53  50
1513753212  52  45
1513753213  51  45
当我打印(timedat)时,我得到:

0     1.513753e+09
1     1.513753e+09
2     1.513753e+09
3     1.513753e+09
4     1.513753e+09
5     1.513753e+09
6     1.513753e+09
7     1.513753e+09
8     1.513753e+09
9     1.513753e+09
10    1.513753e+09
11    1.513753e+09
12    1.513753e+09
13    1.513753e+09
Name: Time, dtype: float64
我相信将unix时间转换为H:M:SM/D/Y时间应该足够简单。我已经搜索了数小时,试图将x轴绘制为定义的时间,但没有结果


将历元时间戳转换为日期时间,并使用内置的轴标签格式。尝试替换此:

timedat = df.loc[:, 'Time']
为此:

timedat = pd.to_datetime(df['Time'], unit='s')

要将
Time
列从unix格式转换为字符串datetime,请使用
pd.To_datetime
+
dt.strftime
-

df.Time = pd.to_datetime(df.Time, unit='s').dt.strftime('%H:%I:%S %m/%d/%y')
df

                 Time  Temperature  Humidity
0   07:07:00 12/20/17           54        45
1   07:07:01 12/20/17           55        48
2   07:07:02 12/20/17           55        50
3   07:07:03 12/20/17           56        52
4   07:07:04 12/20/17           56        54
5   07:07:05 12/20/17           57        54
6   07:07:06 12/20/17           56        54
7   07:07:07 12/20/17           56        55
8   07:07:08 12/20/17           56        56
9   07:07:09 12/20/17           55        56
10  07:07:10 12/20/17           54        52
11  07:07:11 12/20/17           53        50
12  07:07:12 12/20/17           52        45
13  07:07:13 12/20/17           51        45

如何执行
df.Time=df.Time.astype(int)
?我添加了一张图形图片,将其转换为字符串,然后绘图
df.Time=df.Time.astype(str)
!我只需要将unix时间转换为datetime,并调整刻度线标签的角度,这将很好!谢谢我尝试了这段代码,它的工作方式正是我希望它在Windows10上运行Python3.6的方式,但现在我正在转换到我的raspberry pi 3(希望最终在触摸屏上使用它),我得到了一个错误:“Series”对象没有属性“dt”。如果我删除那行代码中“.dt”之后的所有内容,它的图形就不正确(它最初在图片中的表现)。但是,当我打印timedat时,它的打印格式是:“YYYY-MM-DD H:MM:SS.ms”,错误可能是因为我在python 3.4.2上运行代码,而python 3.4.2是Pi'的默认版本吗?@splassed_u?您仍然在运行相同的代码吗?
pd.to_datetime(df.Time,unit='s')
的输出是什么?@COLDSPEED我正在运行相同的代码。我已经在pi上安装了熊猫的最新版本。代码仅以
df.Time=pd.to_datetime(df.Time,unit='s').dt.strftime('%H:%I:%s%m/%d/%y')
pd.to_datetime(df.Time,unit='s')的输出为:0 2017-12-20 07:00:00 1 2017-12-20 07:00:01 2 2017-12-20 07:00:02.005000。。。但它不会在图表上显示这一点。它的图表如作品所示。嗯,非常令人费解。如果你说代码在你的桌面上工作,但在rpi上不工作,那么你可以用raspberry pi标签打开一个新问题并解释你的问题?我对环境不熟悉,所以我想不出任何东西可以解释这一点。