1

I am trying to plot the graph as I want time in x axis and integer data in y axis. I am getting the following error

TypeError: float() argument must be a string or a number

plt.plot(time,data) on the above command it is showing the error time contains the data points from after every one minute of interval 15:46:00 to 16:45:00 i have also checked the data type also it is showing datetime for time .

import matplotlib as plt
import matplotlib
import datetime as db
import matplotlib.pyplot as plt


time=["16:45:00","16:46:00","16:47:00","16:48:00","16:49:00","16:50:00"]
data=[1,2,1,3,4,6]
time1=[]
for tr in time:  
  t=db.datetime.strptime(tr,"%H:%M:%S").time()
  time1.append(t)
plt.plot(time1,data)
plt.show()
2
  • there is nothing much in code one array is there containing the time and other is containing the data point in it . Commented Nov 9, 2016 at 11:35
  • File "C:\Python27\lib\site-packages\matplotlib\axes_base.py", line 1725, in upda path = line.get_path() File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 938, in get_path self.recache() File "C:\Python27\lib\site-packages\matplotlib\lines.py", line 625, in recache x = np.asarray(xconv, np.float) File "C:\Python27\lib\site-packages\numpy\core\numeric.py", line 482, in asarray return array(a, dtype, copy=False, order=order) TypeError: float() argument must be a string or a number Commented Nov 9, 2016 at 11:51

1 Answer 1

2

You need to remove the .time() from your datetime.strptime

time = ["16:45:00", "16:46:00", "16:47:00", "16:48:00", "16:49:00", "16:50:00"]
data = [1, 2, 1, 3, 4, 6]
time1 = []
for tr in time:
    t = db.datetime.strptime(tr, "%H:%M:%S")
    time1.append(t)

fig,ax = plt.subplots()
ax.plot(time1, data)
plt.gcf().autofmt_xdate()  formats the x-axis to get rotate the tick labels 
plt.show()

This produces the figure:

enter image description here

However, this gives you some extra zeros at the end of the datetimes. In order to remove this then the datetimes need to be formatted. This can be done by using matplotlibs DateFormatter.

from matplotlib.dates import DateFormatter

time = ["16:45:00", "16:46:00", "16:47:00", "16:48:00", "16:49:00", "16:50:00"]
data = [1, 2, 1, 3, 4, 6]
time1 = []
for tr in time:
    t = db.datetime.strptime(tr, "%H:%M:%S")
    time1.append(t)

#format the plotting of the datetime to avoid times like 12:45:00.00000000
date_formatter = DateFormatter('%H.%M.%S')

fig,ax = plt.subplots()
ax.plot(time1, data)
ax.xaxis.set_major_formatter(date_formatter)
plt.gcf().autofmt_xdate()
plt.show()

This produces a nice looking figure:

enter image description here

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

3 Comments

Thnx a lot it worked for me after removing the .time()
If this answered your question then please mark as accepted by pressing the tick button next to the answer
These will be better by default in 2.0 matplotlib.org/devdocs/users/…

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.