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

When looking for a solution to my previous question (which was clearly resolved), I had another problem that has not yet been resolved. I would be very grateful to everyone who could help me solve this problem!

There was a thought that I used the outdated version of matplotlib 1.5.1, but now I upgraded it to 2.1.1, as well as from numpy to 1.14.0, this did not help me, alas.

So again. I have a CSV file as input:

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 I ran this python script on it:

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

And I get this TypeError on it (while the person who solved my previous question doesn't have it!):

Traceback (most recent call last):
  File "lightcurve.py", line 16, in <module>
    plt.plot(x,y, marker='o', label='brightness')
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3154, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
    self.add_line(line)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
    self._update_line_limits(line)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
    path = line.get_path()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
    self.recache()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
    x = np.asarray(xconv, np.float_)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number

I'm stuck there.

+4
source share
2

matplotlib 2.1 numpy 1.13, .

, , , matplotlib pandas datetime.time. pandas , , , .

, . , matplotlib, datetime ( ).

- .

import io

u = u"""16,59,55,51 13.8
17,00,17,27 13.7
17,00,39,01 13.6
17,01,01,06 13.4"""

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv
from datetime import datetime

x = []
y = []

plots = csv.reader(io.StringIO(u), delimiter=' ')
for row in plots:
    hours,minutes,seconds,milliseconds = [int(s) for s in row[0].split(",")]

    x.append(datetime(2018,1,1,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()

hours = mdates.SecondLocator(bysecond=[0,30])
t_fmt = mdates.DateFormatter('%H:%M:%S')
plt.gca().xaxis.set_major_locator(hours)
plt.gca().xaxis.set_major_formatter(t_fmt)

plt.show()

enter image description here

+2

matplotlib plot_date, . , ( , , , ).

:

import matplotlib.pyplot as plt
import csv
import datetime

x = []
y = []

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

        x.append(datetime.datetime(year,month,day,hours,minutes,seconds,milliseconds))
        y.append(float(row[1]))


plt.plot_date(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

coldspeed, , , pandas.

, , , .

@ImportanceOfBeingErnest .

0

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


All Articles