Graphing data using Plotly
Overview
- In this lesson we are going to learn how to graph data collected from our environmental sensors.
- Data will be graphed using a python package called plotly.
- Plotly can produce graphs in HTML format so that they can be easily displayed on a web page.
- Graphing sensor data is very important because it shows trends and changes in data more clearly.
Learning Objectives
- Learn how to create graphs using plotly
- Learn how to import data from files using pandas
- Learn how to add more formatting options in plotly
Creating graphs using plotly
- In this example we are going to install the plotly python package and then write a program to create some simple graphs.
Installation of the plotly python package
- From the Raspberry Pi top bar menu click on the Terminal icon.
- Open the Terminal and enter the command pip3 install plotly
- pip is short for Pip Installs Python.
- pip3 installs packages for python3
- The installation process will take approximately one minute.
- When plotly has been installed you should see an output like the one below.
- The version of plotly that was installed is plotly-4.14.3
- Other python packages may also be installed to support plotly.
- In this instance, another package named retrying-1.3.3 was also installed.
First plotly program in Python
- Open Python3 (IDLE3).
- From the File dropdown menu select New File.
- Enter the python code shown below.
- Save this file as temp_plot_gen.py in your project’s directory (the file name is short for temperature plot generator)
- Save and Run the program.
- After approximately 10-20 seconds the web browser Chromium will open and display a graph of your data.
Adding title descriptions to your graph
- Update the python code with to add layout details in the graph.
- The completed code is as follows.
- Save and Run the program. A new tab in the web browser will open and display this graph.
Importing and plotting data from text files
Plotly is good at plotting graphs, but it cannot important data from text files. To prepare data for plotly we need another python package named pandas. In the following lesson we will install pandas and demonstrate how to import data from a text file. Before we can start using pandas we also need to clean and label the data in our text file to make sure it is ready to import.
Cleaning and adding labels to sensor data
To import data from our data.txt file need to clean the data and give labels to the data. Click on the File Manager. Navigate to your project’s directory and open the data.txt file. By default, it should open in the Mousepad text editor.







