Data visualisation with Plotly

Exploratory analysis

Roland Krause

MADS6

2026-03-04

Data visualization with plotly

Learning objectives

  • Interactive graphs with plotly’s main chart types

Materials

Plotly

The plotly library is an interactive, open-source plotting library that supports over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases.

Built on top of the Plotly JavaScript library (plotly.js), plotly enables Python users to create beautiful interactive web-based visualizations that can be displayed in Jupyter notebooks, saved to standalone HTML files, or served as part of pure Python-built web applications using Dash.

Setup for plotly usage

Python

plotly can be imported as below and is commonly aliased as px.

```{python}
import plotly.express as py
pd.options.plotting.backend = 'plotly'
```

R

```{r}
install.packages("plotly")
library(plotly)
```

Restaurant tips data

Food servers’ tips in restaurants may be influenced by many factors, including the nature of the restaurant, size of the party, and table locations in the restaurant.

Load the data

In one restaurant, a food server recorded the following data on all customers they served during an interval of two and a half months in early 1990.

tips = px.data.tips()
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

Distribution plots

Histograms

Plotly histograms will automatically bin numerical or date data.

fig = px.histogram(tips,
                  x="day", 
                  color = "smoker")
fig.show()

Normalized histograms

  • The default mode is to represent the count of samples in each bin.

  • With the histnorm argument, it is also possible to represent the percentage or fraction of samples in each bin (histnorm='percent' or 'probability').

fig = px.histogram(tips, 
                  x="total_bill",
                  color = "smoker", 
                  histnorm='probability density')
fig.show()

Fine tuning a histogram

fig = px.histogram(tips, x="total_bill",
                   title='Histogram of bills',
                   labels={'total_bill':'total bill'}, # can specify one label per column
                   opacity=0.8,
                   log_y=True, # represent bars with log scale
                   color= "sex")
fig.show()

Combined histograms

fig = px.histogram(tips, 
                    x="total_bill", 
                    color="sex", 
                    marginal="rug", # can be `box`, `violin`
                    hover_data=tips.columns)
fig.show()

Bivariate distributions

fig = px.density_heatmap(tips, 
                        x="total_bill", 
                        y="tip", 
                        marginal_x="histogram", 
                        marginal_y="histogram")
fig.show()

Scatter plots

fig = px.scatter(tips, 
                x="total_bill",
                y="tip", 
                color="sex")
fig.show()

Categorical scatter plots

fig = px.scatter(tips, 
                x="day", 
                y="total_bill", 
                color="smoker", 
                title="Total bill over the days for smoker/non-smoker")
fig.show()

Box plots

fig = px.box(tips, 
            x="time", 
            y="total_bill", 
            color = "smoker")
fig.show()

Violin plots

fig = px.violin(tips, 
                y="tip", 
                x="smoker", 
                color="sex", 
                box=True, 
                points="all",
                hover_data=tips.columns)
fig.show()

Bar plots

fig = px.bar(
    tips, 
    x = "day", 
    y = "total_bill", 
    color = "smoker", 
    title = "Total bill over the days for smoker/non-smoker"
    )
fig.show()

Point plots

fig = px.scatter(
    tips, 
    y="total_bill", 
    x="tip", 
    color="smoker", 
    symbol="day")
fig.update_traces(marker_size=10)
fig.show()

Map plot

gapminder = px.data.gapminder()
fig = px.scatter_geo(gapminder, 
                    locations="iso_alpha",
                    color="lifeExp",
                    hover_name="country", 
                    size="gdpPercap",
                    animation_frame="year",
                    projection="natural earth")
fig.show()

Before we stop

We learned to

  • Use plotly for data exploration
  • Run Python inside R

Contributions

Izabela Ferreira da Silva (Original author)

Thank you for your attention!