Plotly and the Plotly Figure

Introduction to Data Visualization with Plotly in Python

Alex Scriven

Data Scientist

What is Plotly?

 

  • A JavaScript graphing library
    • Don't worry - no need to know JavaScript!
  • Plotly has a Python wrapper

 

Plotly logo

Introduction to Data Visualization with Plotly in Python

Why Plotly?

 

Plotly has a number of unique advantages:

  • Fast and easy to use
  • Low code/low effort options using plotly.express
  • (If desired) Extremely customizable
  • Built-in interactivity
Introduction to Data Visualization with Plotly in Python

Creating Plotly Figures

 

Plotly graphs can be created:

  1. With plotly.express for quick plots (px)
  2. With plotly.graph_objects (go) for more customization

$$

📈 We will spend most of our time on px

Introduction to Data Visualization with Plotly in Python

The importance of documentation

Save the links to key documentation!

  1. Interactive documentation

  2. Detailed reference page for specific plots

$$

For Scatter plots:

Introduction to Data Visualization with Plotly in Python

Creating our Figure

  A basic plotly figure:

import plotly.express as px

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] temperatures = [28, 27, 25, 31, 32, 35, 36]
fig = px.bar( x=days, y=temperatures, title="Temperatures of the week")
fig.show()
Introduction to Data Visualization with Plotly in Python

Our Figure revealed

Bar chart of weekly temperatures

Introduction to Data Visualization with Plotly in Python

The Plotly Figure

 

Plotly Figure components:

  • layout: Dictionary controlling style of the figure
    • One layout per figure
  • data: List of dictionaries setting graph type and data itself
    • Data + type = a trace. There are over 40 types!
    • Can have multiple traces per graph
  • frames: For animated plots (beyond this course)
Introduction to Data Visualization with Plotly in Python

Inside a Plotly Figure

 

print(fig)
Figure({'data': [{'type': 'bar',
              'x': array(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
              , 'Saturday','Sunday'], dtype=object),
              'y': {'bdata': 'HBsZHyAjJA==....'}],
    'layout': {'title': {'text': 'Temperatures of the week'}}})
Introduction to Data Visualization with Plotly in Python

Plotly's instant interactivity

$$

  • Hover over data points
  • Extra interactive buttons

Hover options

Introduction to Data Visualization with Plotly in Python

Let's practice!

Introduction to Data Visualization with Plotly in Python

Preparing Video For Download...