1

I try to plot data with a elapsed time hh:mm:ss on x axis. The hh should be a ongoing number (not only 24h). The imported x raw data has the format yyyy-mm-dd hh:mm:ss and the subtraction for x2 works.

import pandas as pd
import matplotlib.pyplot as plt

...

mydata = pd.read_excel(data_path + data_file, skiprows=2)

x = mydata.iloc[17:,0].values
y = mydata.iloc[17:,1].values

x2 = x - x[0]
plt.plot(x2,y)
plt.show()

But there is a datatype problem:

*TypeError: float() argument must be a string or a number, not 'datetime.timedelta'*

How can I solve this? Thanks

6
  • does the class method total_seconds() help? Commented Apr 21, 2022 at 5:21
  • x2.total_seconds() and then how to plot in the format hh:mm:ss with ongoing number of hours? Commented Apr 21, 2022 at 5:42
  • Does this answer your question? Plot datetime.timedelta using matplotlib and python Commented Apr 21, 2022 at 6:24
  • not really, please give me a help on my example code, thanks Commented Aug 5, 2022 at 10:17
  • Well then, can you provide code that actually produces the error. At the moment you are not even importing datetime in your example. Commented Aug 5, 2022 at 10:36

2 Answers 2

0

I reduced the whole script to the minimun, now I haven't this type error... I have to look in a second step why and when I get this error.

In this next code example there is no error. When I subtract all x values from the x[0] then the values on the x axis aren't in time format, see picture.

import pandas as pd
import matplotlib.pyplot as plt
import time
import datetime
from datetime import timedelta

data_path   = "C:/.../"
data_file = "..."
row_data_start = 0

mydata = pd.read_excel(data_path + data_file, skiprows=2)

x = mydata.iloc[row_data_start:,0].values
y1 = mydata.iloc[row_data_start:,1].values
y5 = mydata.iloc[row_data_start:,4].values
y4 = mydata.iloc[row_data_start:,3].values
 
x = x-x[0]

fig, ax = plt.subplots(ncols=1, nrows=2, dpi=300)

twin1 = ax[0].twinx()
twin1ax1 = ax[1].twinx()

p1, = ax[0].plot(x, y1, ".k-")
p4, = twin1.plot(x, y4, ".b-")
ax[1].plot(x, y5, "r-")

plt.show()

console print(x):

[           0  10000000000  20000000000  30000000000  40000000000
  50000000000  60000000000  70000000000  80000000000  90000000000
 100000000000 110000000000 120000000000 130000000000 140000000000
 150000000000 160000000000 170000000000 180000000000 190000000000
 200000000000]

Variable explorer of the input x values:

0   2022-08-04 08:56:22
1   2022-08-04 08:56:32
2   2022-08-04 08:56:42
.
.
.

enter image description here

How I can format the elapsed time from x[0] to hh:mm:ss?

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

Comments

0

You can always fiddle with your own formatters

import matplotlib.pyplot as plt
from matplotlib import ticker
import numpy as np

x = np.linspace( 0, 2.5 * 86400, 35 )
y = np.sin( x * 1e-5)

f = plt.figure()
bx = f.add_subplot( 1, 1, 1 )

def myformat( a, pos=None ):
    hh = a // ( 60 * 60 )
    rest = a % ( 60 * 60 )
    mm = rest // ( 60 )
    ss = int( rest % 60 )
    # ~return "{}:{:02d}:{:02d}".format( int( hh), int(mm), int( ss ) )
    return "{}:{:02d}".format( int( hh), int(mm))

bx.plot( x, y )
n = 5
td = max( x ) // n
base = 60 * 60 * ( td // ( 60 * 60 ) )
mbase = 60 * 20 * ( td // ( 60 * 20 ) )

bx.xaxis.set_major_locator( ticker.IndexLocator( base=base, offset=0 ) )
bx.xaxis.set_minor_locator( ticker.IndexLocator( base=mbase, offset=0 ) )
bx.xaxis.set_major_formatter( myformat )
bx.tick_params( axis='x', rotation=90 )
bx.minorticks_on()
bx.grid()
plt.tight_layout()
plt.show()

showing

enter image description here

Comments

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.