1

I am trying to plot with 2 y axis and a shared x axis. I have gotten to this point, but its not the x axis I want.

Here is my code:

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()

The x axis is plotting as 0,1,2,... for however many points I have. It is not plotting the defined x axis which should be unix time that I have set in the spreadsheet.

Spreadsheet:

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

When I do print(timedat) I get this:

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

I believe converting unix time to H:M:S M/D/Y time should be simple enough. I have been searching for hours trying to plot the x axis as the defined time, but to no avail.

enter image description here

4
  • What about doing df.Time = df.Time.astype(int)? Commented Dec 20, 2017 at 20:04
  • I have added a picture of my graph Commented Dec 20, 2017 at 20:10
  • Try converting it to string, and then plotting. df.Time = df.Time.astype(str) Commented Dec 20, 2017 at 20:14
  • I think that may have done the trick! I just need to convert unix time to datetime and angle the tick marks labels and it will be good! Thanks! Commented Dec 20, 2017 at 20:18

2 Answers 2

2

Convert your epoch timestamps to datetime and use pandas built-in axis label formatting. Try replacing this:

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

with this:

timedat = pd.to_datetime(df['Time'], unit='s')
Sign up to request clarification or add additional context in comments.

Comments

1

To convert your Time column from unix format to string datetime, use 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

8 Comments

I tried this code and it worked just how i wanted it running Python 3.6 on windows 10, but now I'm in the process transferring over to my raspberry pi 3 (hoping to use this on a touchscreen eventually) and im getting an error: 'Series' object has no attribute 'dt'. If i remove everything after ".dt" in that line of code, it graphed incorrectly (how it did initially in the picture). but when I print timedat, it prints in the format: "YYYY-MM-DD H:MM:SS.ms" Could the error be because i am running the code on python 3.4.2 which is default on Pi'?
@smashed_ Are you still running the same code? What is the output of pd.to_datetime(df.Time, unit='s')?
@COLDSPEED I am running the same code. I have installed the latest version of pandas onto the pi. the code only breaks with df.Time = pd.to_datetime(df.Time, unit='s').dt.strftime('%H:%I:%S %m/%d/%y') the output of pd.to_datetime(df.Time, unit='s') is: 0 2017-12-20 07:00:00 1 2017-12-20 07:00:01 2 2017-12-20 07:00:02.005000 ... but it will not plot that on the graph. It graphs as shown in the OP.
@smashed_ Hmm, very puzzling. If you say that the code works on your desktop, but not on a rpi, then you could open a new question with the raspberry-pi tag and explain your problem?
I am not familiar with the environment, so there's really nothing I can think of that explains this.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.