Knowledge Base

Chapter Summary: The plotly Library

Interactive Graphs

An interactive graph lets you interact with it.

Interactive graphs are best used when:

  • You have a really complex line graph or histogram with a large amount of data.
  • You need to break down individual segments of the graph while working with it.
  • You need to put a lot of information on one graph.

An interactive graph is a prototype for another visualization alternative: a dashboard. A dashboard is a thematically grouped data visualization that lets you quickly answer questions about a product or business.

The plotly library allows you to plot interactive graphs in Python. It’s based on plotly.js, which, in turn, is based on the well-known d3.js data visualization library. You can learn more about d3.js here.

Basic plotly Graphs

If you’re creating a plot based on data from a DataFrame, use Plotly Express, an API designed to give you quick access to the library’s main methods. Here’s how to import it:

1import plotly.express as px

Like seaborn, plotly has built-in data sets.

1data = px.data.election()
2print(data.head())

Line chart

One special feature of the interactive line chart is that it lets you view the values of a line just by hovering the cursor over it. We build line plots by calling the line() method with the following arguments:

  • data — data
  • X — data on the X axis
  • Y — data on the Y axis
  • title — the chart's title
1import plotly.express as px
2
3fig = px.line(data, x='column1', y='column2', title='Plot title')
4fig.show()

Let’s rotate the lables on the X axis using the update_xaxes() method. We’ll pass a rotation angle in degrees to the tickangle argument:

1fig.update_xaxes(tickangle=45) # rotate the tick labels on the X axis 45 degrees
2fig.show()

Histograms

We’ll add the color parameter and separate the data by the type of election system:

We plot histograms using the bar() method with the same arguments that we use for line charts:

1import plotly.express as px
2
3fig = px.bar(data, x='column1', y='column2', title='Plot title')
4fig.update_xaxes(tickangle=45)
5fig.show()

We’ll add the color parameter and separate the data by the type of election system:

1fig = px.bar(data, x='column1', y='column2', color='column3', title='Grouped plot title')
2fig.update_xaxes(tickangle=45)
3fig.show()

Pie Charts

We plot pie charts in plotly using the Pie() method with labels (the names of the proportions) and values (their values)

1from plotly import graph_objects as go
2
3fig = go.Figure(data=[go.Pie(labels=labels_series, values=values_series)])
4fig.show()

Funnel Charts

We use the Funnel() method to build funnels. Its arguments are:

  • y: the names of the funnel’s stages
  • x: the number of users at a particular stage
1from plotly import graph_objects as go
2
3fig = go.Figure(go.Funnel(y = y_series, x = x_series))
4fig.show()

We can get more detailed information by hovering the cursor over each step of the funnel:

  • The percentage of users at this step compared with users at the first step
  • The percentage of users at this step compared with users at the previous step
  • The percentage of users at this step compared with the total of all values
Send Feedback
close
  • Bug
  • Improvement
  • Feature
Send Feedback
,