0

I have a column that has time like 9:3:15 (no leading 0s) (3 mins and 15 sec past 9). I would like to plot this (no date, just time) on the x axis. so i tried the following code:

def data_vis(dayN):
    plt.subplots_adjust(bottom=0.2)
    plt.xticks( rotation=25 )
    ax=plt.gca()
    #ax.xaxis_date()
    xfmt = md.DateFormatter('%H:%MM:%S')
    ax.xaxis.set_major_formatter(xfmt)
    plt.plot_date(md.date2num(dayN["Time Stamp"]),dayN["Temperature(deg C)"])
    plt.show()

I got the following error:

DateFormatter found a value of x=0, which is an illegal date; this usually occurs because you have not informed the axis that it is plotting dates, e.g., with ax.xaxis_date()

with ax.xaxis_date() enabled, I got the below error.

'str' object has no attribute 'toordinal'

Since that columns was "str", I thought of using

pd.to_datetime(day2["Time Stamp"], format = '%H:%M:%S)

but it results in the below output:

1900-01-01 09:51:33

Now I would like to try datetime.datetime.strptime using map and/or lambda function for the above said column..

Any help regarding implementation of map function and plotting the data would be really helpful.Also wil the datetime.strptime help in resolving the problem?

thanks,

4
  • to clarify: day2["Time Stamp"] has strings representing time, no date. it only holds data within one day, i.e. the date does not change? Commented May 8, 2020 at 10:19
  • @MrFuppes yes that's right! Commented May 8, 2020 at 12:54
  • ok did you try to convert the column day2["Time Stamp"] to datetime (just don't care about the date) and then pass it to plt, without any further conversion? Commented May 8, 2020 at 13:07
  • I just tried it and it shows only the H:00:00 in the x-axis ! Commented May 8, 2020 at 13:20

1 Answer 1

0

not sure what's going wrong here; the following works fine for me:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.dates as md
# create a dummy df...
dayN = pd.DataFrame({"Temperature(deg C)": np.random.rand(10)+20,
                     "Time Stamp": ['9:3:15','9:3:16','9:3:17','9:3:18','9:3:19',
                                    '9:3:20','9:3:21','9:3:22','9:3:23','9:3:24']})
# format to datetime, don't care about the date:
dayN["Time Stamp"] = pd.to_datetime(dayN["Time Stamp"], format='%H:%M:%S')

plt.subplots_adjust(bottom=0.2)
plt.xticks(rotation=25)
ax = plt.gca()
xfmt = md.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot_date(md.date2num(dayN["Time Stamp"]), dayN["Temperature(deg C)"])
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I think that because of the no. of entries in my data, matplotlib only shows hourly time like 9:00:00 , 12:00:00 15:00:00. I read it from the comments to the ans here [link]here:stackoverflow.com/a/4091264/12129327 thanks for a solution really appreciate it!
@SomasundharamSampath: maybe you need to set custom ticks then, see e.g. here or matplotlib docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.