Advertisement
  1. Code
  2. Python
Code

Charting Using Plotly in Python

by
Difficulty:BeginnerLength:ShortLanguages:

Data visualization is a way to understand large chunks of data. Certain trends and patterns might go unnoticed in text format, so data visualization makes it easier to understand what the data is trying to say by visualizing it using different charts.

From the official documentation:

plotly.py is an interactive, browser-based graphing library for Python. Built on top of plotly.js, plotly.py is a high-level, declarative charting library. plotly.js ships with over 30 chart types, including scientific charts, 3D graphs, statistical charts, SVG maps, financial charts, and more.

In this tutorial, you'll be learning about the Plotly data visualization tool. You'll learn how to visualize data in Python using Plotly.

Getting Started

You'll be using a Python framework called Flask to create a Python web application. Once you have the application started, you'll see how to use the Plotly library to visualize data.

Setting Up the Flask Web App 

Flask is a micro-framework for creating web applications using Python. It is quite easy to set up Flask. Install Flask using PIP.

Create a directory called PythonPlot. Navigate to the directory and create a file called app.py.

Add the following code to the app.py file.

Start the web application server using the following code:

Point your browser to http://localhost:5000/ and you will have the web application running with the welcome message.

Now let's try to render an HTML page from your Flask web application. 

Create a folder called templates and, inside the templates folder, create a file called index.html. You'll be rendering the graphs created using plotly in the index.html file.

Add the following HTML code to templates/index.html.

Import render_template inside the app.py file.

Add a new route called showLineChart inside the app.py file. Here is how it looks:

Save the above changes and restart the server. Point your browser to http://localhost:5000/showLineChart, and you will have the page rendered in your browser.

Plotly Chart Display

Creating a Line Chart Using Plotly

Let's get started with creating a line chart using Plotly. Import the plotly-related libraries in the app.py file.

You'll be using NumPy to generate random data for displaying inside the line chart. Import numpy in the app.py file.

You'll be using the numpy.linspace method to create evenly spaced samples calculated over the interval.

The above code creates 500 evenly spaced samples between 0 and 100 for the x-axis scale.

You can use numpy.random.randn to create random samples for the y-axis scale.

Create a trace using the plotly.graph_objs.scatter method.

You need to convert the trace into JSON format. For that, you'll make use of the plotly JSON encoder plotly.utils.PlotlyJSONEncoder.

Once you have the JSON data, you'll pass it to the template file to be rendered.

Here is how the app.py file looks:

You need to handle the JSON data on the client side to render the chart data. In the templates/index.html file, add references to the following scripts:

As seen in the above code, you have referenced the plotly script, as well as jQuery and D3.js, which are also required for plotly to work.

Add the following script to parse the passed-in JSON and render the chart.

The safe filter explicitly marks the string as safe, hence disabling auto-escaping. Once the JSON is parsed into the graph variable, you have passed it to the plotly plot method along with the ID of the div in which to render the line chart.

Here is how the index.html file looks:

Save the above changes and restart the server. Point your browser to http://localhost:5000/showLineChart, and you will have the line chart rendered.

Line Chart Using Plotly

Creating a Multi-Line Chart Using Plotly

With some modifications to the above line chart, you can convert it into a multi-line chart. To create a multi-line chart, you need to add extra y-axis scales.

Let's start by creating a new route for displaying the multi-line chart.

Create an x-axis scale, as you did when creating the line chart, and add three y-axis scales.

Create traces using the above xScale and each of the y scales.

Convert the data into JSON using the plotly json encoder, as you did when creating a single line chart.

Here is what the /showMultiChart routing looks like:

Save the above changes and restart the server. Point your browser to http://localhost:5000/showMultiChart, and you will have the multi-line chart rendered.

Multi Line Chart Using Plotly

Wrapping It Up

In this tutorial, you learned how to create line and multi-line charts in Python using the Plotly library. You created a Python Flask web app and saw how to create a line chart using sample data generated with the NumPy library.

You can do a lot more using Plotly. For detailed information, I would recommend reading the official documentation.

Source code from this tutorial is available in the tutorial GitHub repo.

How was your experience learning to create charts using Plotly? Do let us know your thoughts and suggestions in the comments below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.