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:
1 |
pip install plotly |
Should JupyterLab be unable to show you the Plotly graphs, you can install this additional package:
1 |
pip install "ipywidgets>=7.6" |
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:
1 2 3 4 5 6 7 |
import pandas as pd df = pd.DataFrame({ "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"], "Contestant": ["Alex", "Alex", "Alex", "Jordan", "Jordan", "Jordan"], "Number Eaten": [2, 1, 3, 1, 3, 2], }) |
Plotly Express uses a high-level abstraction on the graphs and we only need a few lines to plot the data frame:
1 2 3 4 5 6 7 8 |
import plotly.express as px fig = px.bar(df, x="Fruit", y="Number Eaten", color="Contestant", barmode="group") fig.show() |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
import plotly.graph_objects as go fig = go.Figure() for contestant, group in df.groupby("Contestant"): fig.add_trace(go.Bar(x=group["Fruit"], y=group["Number Eaten"], name=contestant, hovertemplate="Contestant=%s<br>Fruit=%%{x}<br>Number Eaten=%%{y}<extra></extra>"% contestant)) fig.update_layout(legend_title_text = "Contestant") fig.update_xaxes(title_text="Fruit") fig.update_yaxes(title_text="Number Eaten") fig.show() |
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.
1 2 3 4 5 6 7 |
fig = px.bar(df, x="Fruit", y="Number Eaten", color="Contestant", barmode="group") fig.update_xaxes(title_text="Selection of Fruits") fig.show() |
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:
1 2 3 |
import seaborn as sns diamonds = sns.load_dataset("diamonds") |
We can create a scatter plot with similar attributes as we did in the Seaborn example with this code:
1 2 3 4 5 |
fig = px.scatter(diamonds, x='carat', y='price', color='cut',) fig.show() |
There are different colours and we do not get the size attribute, but otherwise the plot shows us the same information:
The interactivity shows up when we hoover the mouse pointer over a point in our plot:
To zoom in, we first select the looking glass, then we select the part of our plot that we 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:
If we want to go back and see the whole plot, we can use the home icon:
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:
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”