Python Friday #190: Interactive Plots With Plotly

We came a long way with data visualisation over the last few months. But so far, all plots we created were static. Now it is time to change this and look at interactive plotting libraries.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.

 

Installation

Plotly allows us to create interactive charts and maps for Python, R, Julia, JavaScript, ggplot2, F#, and Dash. For the time being, we focus on the Python library that we can install with this command:

Should JupyterLab be unable to show you the Plotly graphs, you can install this additional package:

 

Plotly Express or Plotly Graph Objects?

Plotly offers us two different ways to create graphs: Plotly Express (px) and Plotly Graph Objects (go). The official documentation offers us this nice comparison between the two options to visualise this Pandas data frame:

Plotly Express uses a high-level abstraction on the graphs and we only need a few lines to plot the data frame:

We get a bar chart for the different fruits that the two contestants have eaten.

With Plotly Graph Objects we need a lot more code, but we can influence all aspects of the graph to show exactly what we want:

The much larger code created the exact same plot with a bar chart for the different fruits that the two contestants have eaten.

Both options allow us to create the same graphs. Even better, Plotly Express creates the same graph objects behind the scenes as we could create with the more verbose Plotly Graph Objects syntax. Therefore, we best start with the express way and only go down to the graph objects when we need to customise them.

The plot looks the same as before, but now the x axis title is 'Selection of Fruits'

 

Interactivity

In the previous blog posts, we had outliers in our data sets, but we had no easy way to figure out the exact values of them. With Plotly and its interactive plots we can address exactly this point and get the information we need.

We reuse the diamonds data set from the Seaborn examples and load that into a data frame:

We can create a scatter plot with similar attributes as we did in the Seaborn example with this code:

There are different colours and we do not get the size attribute, but otherwise the plot shows us the same information:

We get the known scatter plot of the diamonds data set for carats and price.

The interactivity shows up when we hoover the mouse pointer over a point in our plot:

We can see the price, the carats, and the cut of the point.

To zoom in, we first select the looking glass, then we select the part of our plot that we want to zoom in:

Select the part of the graph you want to zoom in

When we release the mouse, Plotly will zoom in the selected area and shows us the details we are interested in:

We can now see the part of the graph much better.

If we want to go back and see the whole plot, we can use the home icon:

The home icon resets the axes.

When we have a section of the plot that we want to save, we can use the camera icon and Plotly saves the plot as a PNG image:

The camera icon allows us to save a plot.

 

Next

With these basic steps we could get a first impression of Plotly and interacted with the data of our plots. We continue this exploration next week with a closer look on the different diagrams that we can use with Plotly.

4 thoughts on “Python Friday #190: Interactive Plots With Plotly”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.