3 Express plot building

The plotly.express module provides a terse, consistent, high-level wrapper around plotly.graph_objects1 for rapid data exploration and figure generation. One of the nice things about plotly.express is that it is designed to work well with pandas.DataFrame objects, a very common data structure used within statistical analysis in Python. plotly.express also has comprehensive, easy to read documentation at https://www.plotly.express/plotly_express/

3.1 Basic plots

Standard statistical plots can be created very quickly using the plotly.express module and a pandas.DataFrame.

3.1.1 Scatter plots

We begin by loading the module, plus some example data from the course package.

import plotly.express as px
import jrpyvisualisation
bond = jrpyvisualisation.datasets.load_bond()

The data here are information on James Bond films, the top of which can be viewed with

##                     Film         Actor  Kills  ...  Alcohol  Nationality Number
## 0                 Dr. No  Sean Connery      4  ...   Medium     Scottish      1
## 1  From Russia With Love  Sean Connery     11  ...     High     Scottish      2
## 2             Goldfinger  Sean Connery      9  ...   Medium     Scottish      3
## 3            Thunderball  Sean Connery     20  ...   Medium     Scottish      4
## 4    You Only Live Twice  Sean Connery     21  ...   Medium     Scottish      5
## [5 rows x 8 columns]

plotly.express has a number of similarly defined functions which take as the first argument a pandas.DataFrame plus names of variables which map to visual elements. For example to create a scatter plot of the James Bond film data with the ‘Kills’ variables mapped to the x-axis and the ‘Relationships’ variable mapped to the y-axis:

fig = px.scatter(bond, x='Kills', y='Relationships')