The Plotly Layout (from Express to Graph Objects)

What is Plotly?

Plotly is a library that one can utilize to create, adjust and display interactive figures. Plotly is available in many different programming languages, which wrap the JavaScript library D3.js, though we will be using Python throughout the course.

Another visualization library

So what does Plotly offer over other data visualization libraries (e.g., matplotlib)? Plotly has amazing interactivity where users can hover over points, zoom, and narrow the viewing window with ease when compared to matplotlib. It also has interactive elements such as drop-downs and sliders, which we will cover later! Plotly also has a wider range of visualizations (especially statistical and machine learning related visuals) than many other libraries.

Plotly Express vs. graph objects

Plotly Express lies within the Plotly ecosystem and is used for generating stunning plots in very few lines of code. The plotting methods and arguments are intuitive, and because of this, we can get quickly explore our datasets of interest. With this ease of use comes the downside of lower customizability, and this is where the Plotly graph objects library comes in handy.

To import Plotly Express, use the following statement:

import plotly.express as px

Plotly graph objects are another part of the Plotly library that provides the ability to generate aesthetic visualizations but with more customizability and an extra layer of features. While this is the case, typically, more code is needed, and it requires a more nuanced understanding of how Plotly works. Ultimately both Plotly Express and Plotly graph objects are useful options depending on our particular use case.

To import Plotly graph objects, use the following statement:

import plotly.graph_objects as go

Generating great plots quickly

To start the course, we look into the plotly.express module, where we can create graphs in two lines of code. As we progress through the course, we will utilize plotly.graph_objects to add extra customizability.

Our first plot

Here we create a super simple pie chart using the Plotly Express library. We use pandas to generate some fake data about how many purchases were made for different items of clothing at a small shop:

product = ['pants', 'shirt', 'socks', 'hat']
purchases = [4, 7, 3, 9]
data = pd.DataFrame({'product': product, 'purchases': purchases})

The code above places a list of some items and another list with the number of purchases for those items within a DataFrame, generating the following table:

product purchases
0 pants 4
1 shirt 7
2 socks 3
3 hat 9

Example setup

Firstly we will import the libraries and create a simple dataset. Then we will create a simple example pie chart to demonstrate the difference between plotly.express and graph_objects.

Note: For all our smaller coding examples, we will use fig.show(), which would normally create the interactive plot in place (as you will see in later notebook examples). However, in these smaller examples, it will generate an HTML file. You can download it to view the chart.

Press + to interact
# Import libraries
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Create dataset:
product = ['pants', 'shirt', 'socks', 'hat']
purchases = [4, 7, 3, 9]
data = pd.DataFrame({'product': product, 'purchases': purchases})
print(data.head())

Pie chart

A pie chart is a circular graph that allows us to view the percentages or proportions of a whole. In our example, we can see what percentage of purchases were “pants”, “socks,” etc.

Advantages

Disadvantages

Easy to interpret

Becomes harder to read and interpret as the number of unique values within a variable increases

Allows us to see the contribution from each individual category to the whole

If you have too many slices in a pie chart, each individual slice becomes too small and ruins the aesthetic appeal of the visualization

Pie chart: Plotly Express

Plotly Express has a method px.pie that allows us to create our pie chart, which we store in a variable fig. The data_frame argument allows us to pass in the dataset of interest while the names argument gets passed in as a string and is the name of the column with your category values. The values argument set to "purchases" is a string that represents the variable that has the count data of interest. We use fig.show() to display our data on the screen.

Press + to interact
# Import libraries
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Create dataset:
product = ['pants', 'shirt', 'socks', 'hat']
purchases = [4, 7, 3, 9]
data = pd.DataFrame({'product': product, 'purchases': purchases})
# Create the pie chart
fig = px.pie(data_frame=data, names='product', values='purchases')
# Display
fig.show()

Pie chart: graph objects

Let’s now create the same plot using Plotly graph objects.

What changes?

We first have to define the content we plan on plotting. This is achieved through the following code:

trace = go.Pie(labels=data['product'], values=data['purchases'])
  • We notice the absence of the data_frame argument, and instead, we pass in labels and values as the pandas series itself and not just a string with the name of the column.
  • We then place the data on a go.Figure object, passing data=[trace] (just one list element here) to show that we want one pie chart to be on this figure. The code can be seen below:
fig = go.Figure(data=[trace])
  • We then use fig.show() as per usual.

Note: The text in the hover display box in the plot below seems to appear slightly messier. This is something we can fix later.

Press + to interact
# Import libraries
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Create dataset:
product = ['pants', 'shirt', 'socks', 'hat']
purchases = [4, 7, 3, 9]
data = pd.DataFrame({'product': product, 'purchases': purchases})
# Create the trace
trace = go.Pie(labels=data['product'], values=data['purchases'])
# Add the trace to the figure object
fig = go.Figure(data=[trace])
# Display
fig.show()

Here is a notebook of all the code exercises used in this lesson:

Please login to launch live app!