[Matplotlib-users] TypeError: float() argument must be a string or a number

tia teernabachoo at gmail.com
Sun Feb 18 14:43:04 EST 2018

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


    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
    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
    def plot_temp():
    	ldr= get_records()
    	ys = ldr
    	fig = Figure()
    	axis = fig.add_subplot(1,1,1)
    	axis.set_title("Light Intensity")
    	time= get_times()
    	xs = time
    	canvas = FigureCanvas(fig)
    	output = io.BytesIO()
    	response = make_response(output.getvalue())
    	response.mimetype = 'image/png'
    	ani= animation.FuncAnimation(fig, animate, interval=3000)
    	return response
    if __name__ == "__main__":
       app.run(host='', port=80, debug=True)

I used the following reference links in case that helps:

Ubidots timestamp:

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

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

Matplotlib graph using Flask:

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

More information about the Matplotlib-users mailing list