【发布时间】:2018-06-03 10:37:18
【问题描述】:
我正在尝试使用 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 轴,它应该是我在电子表格中设置的 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
当我做 print(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:S M/D/Y 时间应该足够简单。我一直在寻找几个小时试图将 x 轴绘制为定义的时间,但无济于事。
【问题讨论】:
-
df.Time = df.Time.astype(int)怎么样? -
我添加了我的图表图片
-
尝试将其转换为字符串,然后进行绘图。
df.Time = df.Time.astype(str) -
我认为这可能成功了!我只需要将 unix 时间转换为日期时间并调整刻度线标签的角度就可以了!谢谢!
标签: python pandas numpy matplotlib plot