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

I am trying to get sensor data from Ubidots and plot them in a graph using
Matplotlib. The graph can then be displayed on a web server via Flask. I am
getting the following error when I run the codes:

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

Matplotlib-Flask.py

    from flask import Flask, request, render_template
    from datetime import datetime
    from ubidots import ApiClient
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import matplotlib.animation as animation
    from matplotlib.backends.backend_agg import FigureCanvasAgg as
FigureCanvas
    from matplotlib.figure import Figure
    import io
    
    app = Flask(__name__)
    
    //get the latest value of the ldr sensor from Ubidots
    def get_records():
      api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXX')
      my_variable = api.get_variable('XXXXXXXXXXXXXXXXXXXX')
      ldr = my_variable.get_values(1)
      return ldr
    
    //get the latest timestamp from Ubidots
    def get_times():
      api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXXX')
      variable = api.get_variable('XXXXXXXXXXXXXXXXXXXXXX')
      value = variable.get_values(1)[0]
      timestamp = value.get('timestamp')
      global t
        t= mdates.epoch2num(timestamp)
        return t
    
    //render the graph with x and y values of time and sensor values
respectively and send to Flask
    @app.route('/', methods=['POST'])
    def my_form_post():
      ldr= get_records()
      time= get_time()
      templateData = {
        'ldr' : ldr,
        'time' : t
        }
      return render_template('main.html', **templateData)
    
    //Plotting the graph using Matplotlib
    @app.route('/')
    def plot_temp():
      ldr= get_records()
      ys = ldr
      fig = Figure()
      axis = fig.add_subplot(1,1,1)
      axis.set_title("Light Intensity")
      axis.set_xlabel("Time")
      axis.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
      axis.grid(True)
      time= get_times()
      xs = time
      axis.plot(xs,ys)
      canvas = FigureCanvas(fig)
      output = io.BytesIO()
      canvas.print_png(output)
      response = make_response(output.getvalue())
      response.mimetype = 'image/png'
      ani= animation.FuncAnimation(fig, animate, interval=3000)
      return response
    
    if __name__ == "__main__":
       app.run(host='0.0.0.0', port=80, debug=True)

I used the following reference links in case that helps:

Ubidots timestamp:
http://community.ubidots.com/t/convert-timestamp-on-json/473/2

Ubidots Python API: https://github.com/ubidots/ubidots-python

Flask: http://www.mattrichardson.com/Raspberry-Pi-Flask/index.html

Matplotlib graph using Flask:
http://www.instructables.com/id/From-Data-to-Graph-a-Web-Jorney-With-Flask-and-SQL/

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html

Hi,

Can you whittle this down to a self-contained small example?

Cheers, Jody

···

On Feb 18, 2018, at 12:43 PM, tia <teernabachoo at gmail.com> wrote:

I am trying to get sensor data from Ubidots and plot them in a graph using
Matplotlib. The graph can then be displayed on a web server via Flask. I am
getting the following error when I run the codes:

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

Matplotlib-Flask.py

   from flask import Flask, request, render_template
   from datetime import datetime
   from ubidots import ApiClient
   import matplotlib.pyplot as plt
   import matplotlib.dates as mdates
   import matplotlib.animation as animation
   from matplotlib.backends.backend_agg import FigureCanvasAgg as
FigureCanvas
   from matplotlib.figure import Figure
   import io

   app = Flask(__name__)

   //get the latest value of the ldr sensor from Ubidots
   def get_records():
     api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXX')
     my_variable = api.get_variable('XXXXXXXXXXXXXXXXXXXX')
     ldr = my_variable.get_values(1)
     return ldr

   //get the latest timestamp from Ubidots
   def get_times():
     api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXXX')
     variable = api.get_variable('XXXXXXXXXXXXXXXXXXXXXX')
     value = variable.get_values(1)[0]
     timestamp = value.get('timestamp')
     global t
       t= mdates.epoch2num(timestamp)
       return t

   //render the graph with x and y values of time and sensor values
respectively and send to Flask
   @app.route('/', methods=['POST'])
   def my_form_post():
     ldr= get_records()
     time= get_time()
     templateData = {
       'ldr' : ldr,
       'time' : t
       }
     return render_template('main.html', **templateData)

   //Plotting the graph using Matplotlib
   @app.route('/')
   def plot_temp():
     ldr= get_records()
     ys = ldr
     fig = Figure()
     axis = fig.add_subplot(1,1,1)
     axis.set_title("Light Intensity")
     axis.set_xlabel("Time")
     axis.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
     axis.grid(True)
     time= get_times()
     xs = time
     axis.plot(xs,ys)
     canvas = FigureCanvas(fig)
     output = io.BytesIO()
     canvas.print_png(output)
     response = make_response(output.getvalue())
     response.mimetype = 'image/png'
     ani= animation.FuncAnimation(fig, animate, interval=3000)
     return response

   if __name__ == "__main__":
      app.run(host='0.0.0.0', port=80, debug=True)

I used the following reference links in case that helps:

Ubidots timestamp:
http://community.ubidots.com/t/convert-timestamp-on-json/473/2

Ubidots Python API: https://github.com/ubidots/ubidots-python

Flask: http://www.mattrichardson.com/Raspberry-Pi-Flask/index.html

Matplotlib graph using Flask:
http://www.instructables.com/id/From-Data-to-Graph-a-Web-Jorney-With-Flask-and-SQL/

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

it woulld also help to have the full traceback.

···

On Sun, Feb 18, 2018 at 6:50 PM, Jody Klymak <jklymak at uvic.ca> wrote:

Hi,

Can you whittle this down to a self-contained small example?

Cheers, Jody

> On Feb 18, 2018, at 12:43 PM, tia <teernabachoo at gmail.com> wrote:
>
> I am trying to get sensor data from Ubidots and plot them in a graph
using
> Matplotlib. The graph can then be displayed on a web server via Flask. I
am
> getting the following error when I run the codes:
>
> TypeError: float() argument must be a string or a number
>
> Matplotlib-Flask.py
>
> from flask import Flask, request, render_template
> from datetime import datetime
> from ubidots import ApiClient
> import matplotlib.pyplot as plt
> import matplotlib.dates as mdates
> import matplotlib.animation as animation
> from matplotlib.backends.backend_agg import FigureCanvasAgg as
> FigureCanvas
> from matplotlib.figure import Figure
> import io
>
> app = Flask(__name__)
>
> //get the latest value of the ldr sensor from Ubidots
> def get_records():
> api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXX')
> my_variable = api.get_variable('XXXXXXXXXXXXXXXXXXXX')
> ldr = my_variable.get_values(1)
> return ldr
>
> //get the latest timestamp from Ubidots
> def get_times():
> api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXXX')
> variable = api.get_variable('XXXXXXXXXXXXXXXXXXXXXX')
> value = variable.get_values(1)[0]
> timestamp = value.get('timestamp')
> global t
> t= mdates.epoch2num(timestamp)
> return t
>
> //render the graph with x and y values of time and sensor values
> respectively and send to Flask
> @app.route('/', methods=['POST'])
> def my_form_post():
> ldr= get_records()
> time= get_time()
> templateData = {
> 'ldr' : ldr,
> 'time' : t
> }
> return render_template('main.html', **templateData)
>
> //Plotting the graph using Matplotlib
> @app.route('/')
> def plot_temp():
> ldr= get_records()
> ys = ldr
> fig = Figure()
> axis = fig.add_subplot(1,1,1)
> axis.set_title("Light Intensity")
> axis.set_xlabel("Time")
> axis.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
> axis.grid(True)
> time= get_times()
> xs = time
> axis.plot(xs,ys)
> canvas = FigureCanvas(fig)
> output = io.BytesIO()
> canvas.print_png(output)
> response = make_response(output.getvalue())
> response.mimetype = 'image/png'
> ani= animation.FuncAnimation(fig, animate, interval=3000)
> return response
>
> if __name__ == "__main__":
> app.run(host='0.0.0.0', port=80, debug=True)
>
> I used the following reference links in case that helps:
>
> Ubidots timestamp:
> http://community.ubidots.com/t/convert-timestamp-on-json/473/2
>
> Ubidots Python API: https://github.com/ubidots/ubidots-python
>
> Flask: http://www.mattrichardson.com/Raspberry-Pi-Flask/index.html
>
> Matplotlib graph using Flask:
> http://www.instructables.com/id/From-Data-to-Graph-a-Web-
Jorney-With-Flask-and-SQL/
>
>
>
>
>
> --
> Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-
f3.html
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180218/8eaa0ecf/attachment-0001.html>

def get_UbiTimestamp():
  api = ApiClient(token='A1E-6l4DwwC86SDR6QRTJWXGDwshTGPHFl')
  variable = api.get_variable('5a5f80eec03f971388983b8e')
  value = variable.get_values(1)[0]
  timestamp = value.get('timestamp')
  t= mdate.epoch2num(raw)
  
  return t

def plot_temp():
  ldr= get_records()
  ys = ldr
  fig = Figure()
  axis = fig.add_subplot(1,1,1)
  axis.set_title("Light Intensity")
  axis.set_xlabel("Time")
  axis.xaxis.set_major_formatter(mdate.DateFormatter('%H:%M:%S'))
  axis.grid(True)
  time= get_UbiTimestamp()
  xs = time
  axis.plot(xs,ys)
  canvas = FigureCanvas(fig)
  output = io.BytesIO()
  canvas.print_png(output)
  response = make_response(output.getvalue())
  response.mimetype = 'image/png'
  ani= animation.FuncAnimation(fig, animate, interval=3000)
  return response

Apparently the timestamp format is giving the following errors:

Complete traceback:

    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1997, in
__call__
    return self.wsgi_app(environ, start_response)
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1985, in
wsgi_app
    response = self.handle_exception(e)
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1540, in
handle_exception
    reraise(exc_type, exc_value, tb)
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1982, in
wsgi_app
    response = self.full_dispatch_request()
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1614, in
full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1517, in
handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1612, in
full_dispatch_request
    rv = self.dispatch_request()
    File "/usr/lib/python2.7/dist-packages/flask/app.py", line 1598, in
dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "/home/pi/MatPlotLib-Flask.py", line 51, in plot_temp
    axis.plot(xs,ys)
    File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line
1894, in inner
    return func(ax, *args, **kwargs)
    File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line
1407, in plot
    self.add_line(line)
    File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line
1787, in add_line
    self._update_line_limits(line)
    File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line
1809, in _update_line_limits
    path = line.get_path()
    File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 989,
in get_path
    self.recache()
    File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 685,
in recache
    y = np.asarray(yconv, np.float_)
    File "/usr/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

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html

I posted the full traceback in a reply to the above answer. Do check it
please.

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html

The error I believe is in the way the timestamp has been formatted.

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html

The error I believe is in the way the timestamp has been formatted.

···

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-f3.html

This is incomplete:

def get_UbiTimestamp():
        api = ApiClient(token='A1E-6l4DwwC86SDR6QRTJWXGDwshTGPHFl')
        variable = api.get_variable('5a5f80eec03f971388983b8e')
        value = variable.get_values(1)[0]
        timestamp = value.get('timestamp')
        t= mdate.epoch2num(raw)

        return t

`raw` is undefined. Also, nothing in that traceback indicates a problem
with datetime formatting, so what makes you think that is the problem?

···

On Mon, Feb 19, 2018 at 2:07 AM, tia <teernabachoo at gmail.com> wrote:

The error I believe is in the way the timestamp has been formatted.

--
Sent from: http://matplotlib.1069221.n5.nabble.com/matplotlib-users-
f3.html
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20180219/759314f9/attachment.html>