Module 8 · Plotly and maps · Lesson 39 of 66

Interactive charts with Plotly

Charts you can hover, zoom and pan, with Plotly Express and pandas' Plotly backend.

In this lesson · about 18 minutes

  • Draw scatter, bar, box and histogram charts with Plotly Express
  • Switch pandas charts to Plotly with one setting
  • Build a 3D surface with graph_objects

Plotly makes interactive charts: hover a point to read its values, drag to zoom, double-click to reset. The charts are drawn by JavaScript in the page, so they work in notebooks, web apps and dashboards.

There are two ways in:

  • Plotly Express (import plotly.express as px): one function per chart, given a DataFrame and column names, like Seaborn. Start here.
  • Graph objects (import plotly.graph_objects as go): build a go.Figure from traces such as go.Scatter and a layout. More typing, more control.

Show a figure with fig.show(), or make fig the last line of the cell. On this page the chart appears under the cell. Try hovering and zooming.

pandas with the Plotly backend

Exercise

A bar chart

Use Plotly Express to draw a bar chart of df2, with Category on the x axis and Values as the bar heights.

Code that runs before yours
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(np.random.randn(100, 4), columns=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Values': [32, 43, 50]})

Box plots and histograms

px.box(df) draws a box for every column; hover a box to read its median and quartiles. px.histogram(df, x='A', nbins=25) counts values into about 25 bins. Click a name in the legend to hide or show that series.

Exercise

An interactive histogram

Draw a histogram of column A of df with 25 bins.

Code that runs before yours
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(np.random.randn(100, 4), columns=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Values': [32, 43, 50]})

Bubbles and scatter matrices

In px.scatter, size= sets each point's size from a column, which makes a bubble chart, and color= colors the points. px.scatter_matrix(df) is Plotly's pair plot.

Exercise

A bubble chart

Make a scatter plot of df with A on x and B on y, colored by column C.

Code that runs before yours
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(np.random.randn(100, 4), columns=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Values': [32, 43, 50]})

3D surfaces with graph objects

A surface takes a grid of heights as z. Build it with go.Figure(data=[go.Surface(z=...)]). Drag the chart to turn it around.

Exercise

Your own surface

Draw a 3D surface whose heights are the table surf below, using the 'RdBu' color scale.

Code that runs before yours
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.DataFrame(np.random.randn(100, 4), columns=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame({'Category': ['A', 'B', 'C'], 'Values': [32, 43, 50]})
surf = pd.DataFrame({'x': [1, 2, 3, 4, 5], 'y': [10, 20, 30, 20, 10], 'z': [5, 4, 3, 2, 1]})

Exercises solved: 0 of 4

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.