Python with Arduino LESSON 11: Plotting and Graphing Live Data from Arduino with Matplotlib

We now have all the pieces put together to allow us to plot live data from the Arduino. If you have kept up with the earlier lessons, you will now have everything you need. If you have not done the earlier lessons, make sure you have python 2.7, vPython and pySerial installed from Python with Arduino LESSON 2.  Make sure you have installed matplotlib (Python with Arduino LESSON 7), and install drawnow (Python with Arduino LESSON 10). Also, you need to build the BMP180 circuit and get the arduino programmed up as explained in Python with Arduino LESSON 9. With this business taken care of, you are now ready to start plotting live data.

Pressure Data
This chart shows live pressure and temperature data being plotted in real time

We are using the Adafruit BMP180 pressure sensor.  We showed how to hook it up and program it in LESSON 9. As a reminder, we are using this code for the arduino. LESSON 9 explained in detail how the code works.

The video in this lesson above explains step-by-step how to develop the code on the Python side, and how matplotlib and drawnow work together to make live graphs and plots of data streaming from the arduino in real time. The code below is what we developed in the video. Do not simply cut and paste this code, but make sure that you understand it so you are able to create your own live graphing programs from scratch. If you are in my class, you will be required to be able to develop live graphing code like this from scratch, so don’t take a shortcut and copy and paste.

You should be seeing data like the graph on the top of this lesson. You will probably need to adjust your y-axis scale parameters in Python to ensure the scale is suitable for the data you are taking. If your chart is blank, likely your y-scales are not right for your data measurements.

90 thoughts on “Python with Arduino LESSON 11: Plotting and Graphing Live Data from Arduino with Matplotlib”

  1. Hi Paul,
    Thanks a lot for your effort, you are really opening a new world with your course. One of the best I have seen on Internet.
    I followed your course step by step but I stumble on a problem with this lesson 11.
    When I run your python script, I get a plot but I have the following message in the shell:

    Warning (from warnings module):
    File “C:\Python27\Lib\site-packages\matplotlib\”, line 2380
    warnings.warn(str, mplDeprecation)
    MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented

    I checked the whole Matplotlib (I am using Windows 7) installation and dependencies with PIP but everything seems ok. If I don’t plot anything, and print the temperature, I can see the increase if I touch the BMP180. But as soon as I plot the graph, values seems to remain constant and it is slow. I spent sometimes in the web but could not find anything related with this problem. Any idea of what could be the problem. (Baud rate and COM are correct as well). Thanks again for our help. Gilles

    1. I have exactly the same problem except the graph is drawing correctly, after making sure the Y scales are correct.

  2. Paul,
    This is a great course and a great credit to you, thank you very much for all your work. I’d like to learn how to make this live graph available on a website. Can you suggest any tutorials that might guide me on how to get Python graphs on to a website?

    Many thanks Geoff Melbourne Australia

    1. Geoff,
      I have not investigated running Python in a browser, so am not sure how to go about that. Sorry I can not help on that one, and good luck in your project.

  3. Hi Paul,
    Great tutorial. I am learning a lot.
    I am plotting acceleration x, y, z data from Adafruit_LSM9DS0 sensor.
    It works fine for awhile until the accelY data ran out of data and pop failed. See error below.
    IndexError: pop from empty list
    Any idea why this is happening?

  4. Big thanks for your tutorials! I previously tried to avoid learning Python considering it very time consuming – finally I am plotting my sensor data in less than 4 hours. Still no perfect knowledge of Python, by I followed your tutorials and got the point quite quickly :o)
    Keep on!

  5. Thank you so much for sharing this code and I’m really appreciate it. I do find this code is very helpful to me to implement in my project. However, may I know how to delay the graph output in case when I press the sensor the result at the GUI will delay about 2 or 3 seconds.

    Big thanks!! 🙂

  6. Thank you for the video,

    i had one question i ran the exact same code that you had (copy and paste) but noticed that my graph was plotted much slower than what you demonstrated in the video is there a reason why?

    1. It depends on the speed of your graphics card, speed of the computer, and how fast you are transferring the data over the serial port. Try playing with delays in arduino and rate in vpython to get something that runs smooth and as fast as possible on your setup. Sometimes trying to run things too fast makes things worse. You have to play around with the parameters.

  7. Paul, thank you so much for preparing these great video lessons and excellent webpage. They make learning Python a true pleasure!!

    I’d like to point to some potential improvement of the lesson: After I followed the life data plotting exercise above, I discovered a similar solution, which has only 27 lines of code and does not require to install the “drawnow” library. I test it only under Linux and with Python 2.7.6 however. The Link is: Bennis Welt: Live plotting data from the serial port using Python, Matplotlib and an Arduino

  8. Hello
    I want to draw a curve of x and y by 2 rheostat at the entrance to A0 A1
    Is it possible

  9. Hello
    I want to plot a curve of x and y by 2 rheostat at the entrance to A0 A1
    Is it possible

  10. Hi Paul

    thanks for the tutorials they have been invaluable. In this lesson i’m struggling with an error message telling me my serial port is busy :
    OSError: [Errno 16] Resource busy: ‘/dev/tty.usbmodem1411’
    any suggestions for a solution?

    1. You can not have python running and the arduino serial monitor running at the same time. Kill python before you try and download the program to arduino, and make sure the arduino serial monitor is closed before you try and run python. Only one thing can have the serial port at a time.

  11. plt.ylim(93450,93525) #Set limits of second y axis- adjust to readings you are getting

    Please explain: It is Ok or mistake? Perhaps there should be plt2?

  12. Hi, Paul.
    This lesson 11 helped me a lot in my project!
    But I have a question about the 250msec delay in your arduino algorithm. Is there a reason why you’ve chosen this value for delay?

    Actually I need at least 100 points per sec in my graph, but when put 10msec delay, the plotting becomes much more slower or even does not plot the graph.
    It shows an Value Error telling that the python program could not convert the string (from serial) to float.

    Can you help me?
    Thank you.

    1. I am going to bet that the graph can not keep up with that much data. matplotlib was really not designed for live graphing, so we are sort of coaxing it into doing it. It is really pretty slow.

  13. Hi my teacher,
    I try to draw using drawnow in Python but this sentence appeared everytime:
    from drawnow import *
    ImportError: No module named drawnow
    Please can you help me? Thanks in advance my teacher

    1. You need to watch the earlier lessons and make sure you have all the needed libraries installed on your computer.

  14. Yes, I watched it and fixed the problem. Thank you so much my best teacher for your advice, I appreciate your help so much.

    1. Hello Sara,
      can you please tell me how you fixed the
      Importerror: no module named drawnow…?
      cause i did install successfully:
      pip install drawnow on my Win7 PC.
      I have Vpython and Python 2.7.9 with anaconda2–maybe ‘drawnow’ went there or so?
      Help much appreciated 🙂

  15. Hi Paul, your lessons are fantastic! Thanks so much for taking the time to put these together. I have never played with Python before (I’d hardly even heard of it), but with your help it is really easy. I already have a little sensor node running giving a graph of temp, humidity and dewpoint. I have a wireless mesh network running with about 6 sensor nodes (using the NRF24L01) and eventually hope to plot the outputs from all 6 nodes on the same graph…. OK, more digging required!

  16. Hi Paul,

    First of all, thanks a million for all the great tutorials, it really helped me out combining Arduino with Python!
    I do have a question for You about lesson 11: all my code is the same as You have put it in the above example but I keep getting an error with the text:

    P = float( dataArray[1]) #Convert second element to floating number and put in P
    IndexError: list index out of range.

    I have checked the output of the arduino but looks the same as the one in the tutorial.
    what do You think this can be?

    Many thanks, and don’t stop making tutorials!



    1. It sounds like you are not getting your dataArray for some reason, and then it fails because the array is not formed. Try printing dataArray itself before this step, and also print whatever the raw string was that was used to create dataArray. You will probably find some sort of problem there.

  17. Hello Paul,
    I have been following your tutorials to learn Python and adapting them to use with a PIC instead of Arduino. I can plot data at realtime but I want to place realtime read from the PC at the X axis, I can read time with x = np.array(
    But I can’t place it on x axis. Do you know how to do?
    Thanks for all, you learnt me a lot.

  18. Another great lesson – thanks, Paul. I have a question, and wondering if anyone else has seen this:

    In this example, Python is using 100% of the CPU resources (on a late model Macbook Pro). This is according to “Activity Monitor”, an OS X utility similar to “Task Manager” in Windows. I wondered if anyone else is seeing excessive amounts of CPU utilization, and if so, how do we control that? Would an interrupt-style interface be better than the polling-style interface?

    1. The answer to the CPU usage issue is apparently in Python’s “signal” module. I’ll try this when I have time & report back here.

  19. Hi sir. What do you think is the problem, i just want to print the data from arduino from my serial port as simple as that however, when i will click the run module, the python shell will appear but there’s no printed text on it nor displayed problem or errors for it in the phyton shell. Please help.

  20. Hi sir. Criss again, i noticed that whenever i pull out the serial cable from the serial port the text “hello” (which is my expected output) appears and the errors are displayed as well. What should i do?

  21. what does this mean sir? I am encountering this error
    ” ValueError: could not convert string to float: ÿ0″
    what should i do sir? I am trying to plot live my serial data coming from arduino..

  22. I totally like your lessons . Very interesting videos.
    I tried it with another sensor and it works. Than I tried it for a program where I write some data with the coman “Serial.parseFloat()” and it doesn’t work. Do soeone have any solution of the problem ?

  23. Hi Paul, Thanks very much for your tutorials – they are excellent and I’ve made great progress and I have a greater understanding of the process of writing programs.
    I’ve encounted a problem I hope you can help with. When I ran the python code:
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/drawnow/”, line 61, in drawnow
    else: plt.draw_all()
    AttributeError: ‘module’ object has no attribute ‘draw_all’

    Do you have any suggestions how I might resolve this issue?
    Many thanks,

  24. HI Paul,
    24 hours later – I have solved the issue. I installed an older version of drawnow (0.60) instead of the most upto date one (0.71). It works OK with your tutorial

    All the best


    1. Thankyou Jed, I was getting the same error. Definitely should have checked the version numbers as programming was going great until that point.

  25. Hi, I a rookie
    I’m unable to run drawnow with version 0.71.
    File “C:\Python27\Lib\site-packages\drawnow\”, line 61, in drawnow
    else: plt.draw_all()
    AttributeError: ‘module’ object has no attribute ‘draw_all’
    I tryed to install drawnow 0.60 but python doesn’t find drawnow.
    Any suggestion?

    Really useful tutorials. Thanks

    1. I got the same error message:
      AttributeError: ‘module’ object has no attribute ‘draw_all’

      The only way I could get past it was to open the file
      in Notepad++, comment out lines 60 and 61, then Save.

      I have no idea why this worked and no I have no idea when this workaround will haunt me in the future…

    2. If you have pip installed, see one of the previous lesson, you can specify the version so, “sudo pip install drawnow==0.60”
      Remember to uninstall the previous version first!

  26. Very cool and totally agree with comments about excellent content and clarity of teaching.
    When using an arduino with USB, the following line is necessary:
    arduinoData = serial.Serial(“/dev/cu.usbmodem1421”,baudrate=115200,timeout=10)
    Specifying baud rate as above seems to make a difference too.
    Stack exchange has a fuller explanation and this code helps find all the ports available on your machine.
    ports = list(
    for p in ports:
    print p

    Thanks again.

  27. hi Paul,

    Great lesson, I really enjoyed it.
    One thing that bugs me about the final result though, I can see that the x-axis ticks refer to the elements of the temperature and pressure arrays, and thus it stops increasing once the counter reaches 50.
    Alternatively, I was wondering how you would you go about making Time (seconds) as the x-axis, and having the time axis pan along after the 50th data point is reached?

    Anyways, thanks for the lesson!

  28. Guys, if you got the “AttributeError: ‘module’ object has no attribute ‘draw_all’”, just write:


    That should fix this issue 🙂

    1. Thank you so much for you suggestion and it worked…
      By the way can you tell me what does this syntax does???

  29. Hello Paul,

    Your course is really interesting and useful to me. I have copied your example in an editor en tried it. But every time I get this message :
    nvalid! cannot cast
    Traceback (most recent call last):
    File “”, line 36, in
    File “/usr/local/lib/python2.7/dist-packages/drawnow/”, line 61, in drawnow
    else: plt.draw_all()
    AttributeError: ‘module’ object has no attribute ‘draw_all’

    It seems there is missing something in the library. Can you help me to fix this?

    Thanks in advance.


    1. If you are not using the same versions of all the software, there could be an issue. I can not try all versions on all computers. Look through the comments and see if anyone has had similar problems.

  30. I’m having some serious trouble with the call to drawnow.

    This is the error message I’m getting:
    Traceback (most recent call last):
    File “/Users/johannes/Desktop/pythonScripts/”, line 39, in
    drawnow(makeFig) #Call drawnow to update our live graph
    File “build/bdist.macosx-10.11-intel/egg/drawnow/”, line 61, in drawnow
    AttributeError: ‘module’ object has no attribute ‘draw_all’

    Anyone see this?
    Also, my IDE doesn’t like the drawnow either, says it is not callable.

    Otherwise great tutorial!

  31. I am also getting the ‘draw_all’ error message mentioned by Mr Holm.
    Please help as this looks really good and I’m keen to use it

  32. Sir please help me i got this error dont know what to do please

    Traceback (most recent call last):
    File “Untitled”, line 22
    File “C:\Python27\Lib\site-packages\drawnow\”, line 61, in drawnow
    else: plt.draw_all()
    AttributeError: ‘module’ object has no attribute ‘draw_all’

      1. RE: DIYMicha
        FEBRUARY 8, 2016 AT 8:48 PM

        Guys, if you got the “AttributeError: ‘module’ object has no attribute ‘draw_all’”, just write:


  33. Thank you for this tutorial.
    I’ve been looking for this kind of thing for a while and am grateful that I found this. Everything worked perfectly.

    I thought I would add my suggestion. This allows the y axes to autoscale. Temperature readings can be fairly constant, so I just added 0.1 F to either side:

    def makeFig(): #Create a function that makes our desired plot

    t_low = numpy.min(tempF) – 0.1
    t_high = numpy.max(tempF) + 0.1
    p_low = numpy.min(pressure) – (numpy.max(pressure) – numpy.min(pressure))*0.1
    p_high = numpy.max(pressure) + (numpy.max(pressure) – numpy.min(pressure))*0.1

    plt.ylim(t_low,t_high) #Set y min and max values

    plt2=plt.twinx() #Create a second y axis
    plt.ylim(p_low, p_high) #Set limits of second y axis

  34. thanks for this great tutorial! i am encounting this error as i follow along what you are doing in lesson 11.
    the error is as follows:
    AttributeError: ‘module’ object has no attribute ‘ion’

    what should i do?

  35. hye. i’m using Python with beaglebone black. i want Plotting and Graphing Live Data from beaglebone black with Matplotlib/pygame/

    do you have link or tutorial to do that?

    thanks in advance

    1. I did a series showing how to plot live data from raspberr pi. You might look at those videos and see if they would help.

  36. Dear paul,
    First of all, thank you for your nice videos and “step by step” explantion.
    Is it possible to plott data in an individual graph for each parameter? For example, here tow separate graph for temp an pressure.
    Thank you for your kind attention

    1. There is a way to do it, but you would need to go to the matplotlib site and see what the command is. I have done it before, but dont remember the command.

  37. Hi Paul,

    This is was very helpful. I was wondering if you also can suggest how to go about pausing the live streaming., more like freezing the graph.


  38. Hi Mr Paul,
    Is there any posibility in plotting temperature and altitude in one graph, and iclination on a diferent one, and run both at once?
    And , is ther any posibility of saving the plotted data and using older samples to compare them with new data(the one I measure in the present )?

  39. I have a code that returns me: RuntimeError: module compiled against API version a but this version of numpy is 9

    import serial #Import Serial Library
    import numpy as np
    import matplotlib.pyplot as plt
    from drawnow import *

    arduinoSerialData = serial.Serial(‘com4’,115200) #Create Serial port object called arduinoSerialData
    plt.ion() #Tell matplotlib you want interactive mode to plot live data


    while True:
    if (arduinoSerialData.inWaiting()>0):
    arduino_data = arduinoSerialData.readline()
    print GSR

    Some ideas on how to solve this?

  40. plt.ylable(‘Temp F’)
    once i write this into the code and run it get a erorr sign

    Traceback (most recent call last):
    File “C:\Python27\Lib\site-packages\visual\examples\test 11 f”, line 30
    drawnow(makeFig,show_once=True) #drawnow(makeFig,show_once=True)changing back to drawnow(makeFig)
    File “C:\Python27\Lib\site-packages\drawnow\”, line 58, in drawnow
    draw_fig(*argv, **kwargs)
    File “C:\Python27\Lib\site-packages\visual\examples\test 11 f”, line 17, in makeFig
    plt.ylable(‘Temp F’) #this line does not work / plt.ylable(‘Temp F’)
    AttributeError: ‘module’ object has no attribute ‘ylable’
    can someone please help

  41. plt2.plot(pressure, ‘b^-‘, label=’Pressure (Pa)’)

    this line is not ploting pressure any ideas on getting pressure to read on the graph thanks

  42. hi..
    i am umesh from india following your all videos i got problem in python lesson 10 showing drawnow library function draw_all () not in module.

    1. First uninstall the previous version.
      Download the version 0.60 version of draw_all and then install it.
      Still it might not work, then follow the below steps:
      1. open the (Drawnow python file) in notepad++
      2. put the comment line for line – 60, 61, 62
      3. and then save it.

      Trust me , after struggling so much I got this.
      And, it worked for me.
      Hope it helps.

  43. Hi Paul,
    Brilliant lesson.
    I wanted to know how to similarly plot for a third variable.
    I am working on Plotting data from an accelero-meter.
    It would be great if you could help me out.

  44. Hello Paul..
    This video helped me a lot in my project.
    Thank You so much.

    I have a question: How to save the live streaming data from serial monitor into a text file using Python?
    Hope you get a chance to answer me.
    or anyone whoever aware of this.

    Since I am new to Python and Arduino.
    I appreciate all help. 🙂

  45. Hi Paul, Thanks very much for your tutorials – they are excellent and I’ve made great progress and I have a greater understanding of the process of writing programs.
    I’ve encounted a problem I hope you can help with. When I ran the python code:
    File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/drawnow/”, line 61, in drawnow
    else: plt.draw_all()
    AttributeError: ‘module’ object has no attribute ‘draw_all’

    Do you have any suggestions how I might resolve this issue?
    Many thanks,

  46. Hi,Thanks very much for your tutorials.
    I typed :”plt.plot(tempF, ‘ro-‘)” in the while True
    and it works .why we should make function and then use “drawnow(makeFig) “?

  47. Hey Paul , Thanks for your amazing tutorials.
    I have a problem with this project :
    my arduino send data in bytes but in python I want to replace them with Strings.
    the data is like : b’700.00 , 648.00 \r\n’
    I used this function :
    ArduinoString = ArduinoString.decode(‘UTF-8’)
    then the project works for a while but it broke down again and give me this error :
    “UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xfc in position 4: invalid start byte”
    please help me through this problem.


  48. This script worked well on a Linux computer, but when the same Python code ran on a Raspberry Pi (reading the same sensor data from the same Arduino via serial port), the Pi was too slow to handle all of the data, so the plot was not picking up dynamic changes in the data that it should have.

    It was fixed by clearing the cache with this last line of code in the while loop:


    I’m using Python 2.7 but if you are using Python 3, you would use reset_input_buffer() instead of flushInput().

  49. Hi Sir,

    First of all, many thanks for your very good an clear videos.
    I am doing a project, where I want to plot real time data measured by multiple sensors (connected to the arduino Uno). Thanks to your code, I was able to plot the data of a DHT11 temperature and humidity sensor!! But when I tried to code for more figures , with each plotting own real time data, I didn’t succeed.

    I have asked this question on many forums, but didn’t get a proper reply. Could you maybe help me with my little problem?

    Thanks in advance.


  50. Paul
    These are great videos, you’re the science teacher I wish I had. I’ve gone through a lot of your videos, I just completed lesson 11 on Arduinos with Python. I see that someone else asked the question about having the time on the X axis but I didn’t see an answer. I want to plot temp and pressure over a couple of days. I would want the current time at the far right of X axis, have the grid divide in 4 hour segments with the past time on those. How would I do that?
    Thanks again

  51. Traceback (most recent call last):
    File “C:\Users\msi\AppData\Local\Programs\Python\Python36\”, line 37, in
    dataArray = arduinoString.split (‘,’)
    TypeError: a bytes-like object is required, not ‘str’

    I am using python3.6, and I need to add this to make it work :
    arduinoString = str(arduinoString,encoding =”utf-8″)
    #converting bytes to string

    I spent a whole morning for about 3 to 4 hours to make the whole programme work, and there are some errors when using python 3.6, “plt.set_ylabel” does not work. and the last lines, “tempF.pop(0)” doesn’t clear out the data.

    And now, my live streaming sensor is working, thanks Paul, Bravo from China!!!

  52. Hello Paul! I just wanted to thank you for your extremely well-planned and executed tutorial! I could use essentially the same code for using my AM2315 Humidity and Temperature sensor. Thank you again for your guidance- amazing resource!

Comments are closed.