Build time with matplotlib: TypeError: integer required

Grabbing everyone!

Can someone help me with the following problem? Thank you in advance!

I have a CSV file with time stamps (hours, minutes, seconds, milliseconds) and the brightness of the object in values ​​(float), for example:

16.59.55.51 13.8
17,00,17,27 13.7
17,00,39,01 13.6
17.01.01.06 13.4

And here is my python script:

import matplotlib.pyplot as plt
import csv
from datetime import time

x = []
y = []

with open('calibrated.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=' ')
    for row in plots:
        x.append(time(row[0]))
        y.append(float(row[1]))

plt.plot(x,y, marker='o', label='brightness')
plt.gca().invert_yaxis()
plt.xlabel('time [UT]')
plt.ylabel('brightness [mag, CR]')
plt.legend()
plt.grid()
plt.show()

When I run the script, I get this TypeError:

Traceback (most recent call last):
  File "lightcurve.py", line 11, in 
    x.append (time (row [0]))
TypeError: an integer is required

What am I doing wrong?

+1
source share
3 answers

- , datetime.time(),

row[0], "16,59,55,51". , row[0].split(","), . , int(), datetime.time.

:

x = []
y = []

with open('calibrated.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=' ')
    for row in plots:
        hours,minutes,seconds,milliseconds = [int(s) for s in row[0].split(",")]

        x.append(time(hours,minutes,seconds,milliseconds))
        y.append(float(row[1]))

plt.plot(x,y, marker='o', label='brightness')
plt.gca().invert_yaxis()
plt.xlabel('time [UT]')
plt.ylabel('brightness [mag, CR]')
plt.legend()
plt.grid()
plt.show()

:

enter image description here

+3

[0] , , . "16,59,55,51".

, , :

(hours, minutes, seconds, microseconds) = [int(v) for v in row[0].split(',')] x.append(time(hours, minutes, seconds, microseconds))

0

CSV [0]. , csv :

row = ["16,59,55,51", "13.8"]

To fix this, you need to convert these strings to the appropriate values.

 with open('calibrated.csv','r') as csvfile:
        plots = csv.reader(csvfile, delimiter=' ')
        for row in plots:
            t = [int(x) for x in row[0].split(',')]
            x.append(time(t[0],t[1],t[2],t[3]))
            y.append(float(row[1])

Another option is to use a date and time stamp as follows:

from datetime import datetime
x = []
y = []

with open('calibrated.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=' ')
    for row in plots:
        x.append(datetime.strptime(row[0], '%H,%M,%S,%f'))
        y.append(float(row[1]))

It will use milliseconds as microseconds, but for me it is not very important. However, it allows you to add dates later if you need to.

0
source

Source: https://habr.com/ru/post/1692252/


All Articles