1 Introduction to Plotly

Plotly’s Python graphing library makes it easy to create interactive, publication-quality graphs. This document demonstrates how to create various plots, including line plots, scatter plots, bar charts, box plots, histograms, and more.

Code
library(reticulate)
py_require(c('plotly','pandas','seaborn','psutil','requests'))
Code
import plotly
print(plotly.__version__)
6.2.0
Code
import seaborn as sns
import plotly.express as px
import pandas as pd

# Load an example dataset
tips = sns.load_dataset("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

2 Scatter Plot

A scatter plot is used to visualize the relationship between two continuous variables.

Code
fig = px.scatter(tips, x="tip", y="total_bill")
fig.show()
24681001020304050
tiptotal_bill

2.1 Color by Group

You can color the points based on a categorical variable to see how the relationship varies across different groups.

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

2.2 Size by Group

The size of the points can be mapped to a variable to add another dimension to the plot.

Code
fig = px.scatter(tips, x="tip", y="total_bill", size="size")
fig.show()
24681001020304050
tiptotal_bill

3 Line Plot

A line plot is suitable for showing the trend of a variable over time.

Code
dowjones = sns.load_dataset("dowjones")
dowjones.head()
        Date  Price
0 1914-12-01  55.00
1 1915-01-01  56.55
2 1915-02-01  56.00
3 1915-03-01  58.30
4 1915-04-01  66.45
Code
import plotly.express as px
fig = px.line(dowjones, x="Date", y="Price")
fig.show()
1920193019401950196002004006008001000
DatePrice

3.1 Line Plot with Dots

You can also add markers to the line plot to highlight the data points. ::: {.cell}

Code
import plotly.express as px
fig = px.line(dowjones, x="Date", y="Price", markers=True)
fig.show()
19201930194019501960197002004006008001000
DatePrice

:::

3.2 Color by Group

You can plot multiple lines on the same plot to compare different groups.

Code
import random

# Create datasets for comparison
dowjones2 = dowjones.copy()
dowjones2['type'] = 'old'

dowjones3 = dowjones.copy()
dowjones3['Price'] = dowjones3['Price'] + random.random() * 200
dowjones3['type'] = 'new'

dowjones4 = pd.concat([dowjones2, dowjones3], ignore_index=True)
dowjones4 = dowjones4.sort_values('Date').reset_index(drop=True)
Code
dowjones4.head()
        Date       Price type
0 1914-12-01   55.000000  old
1 1914-12-01  202.637115  new
2 1915-01-01  204.187115  new
3 1915-01-01   56.550000  old
4 1915-02-01   56.000000  old
Code
import plotly.express as px
fig = px.line(dowjones4, x="Date", y="Price", color='type')
fig.show()
1920193019401950196002004006008001000
typeoldnewDatePrice

4 Histogram

A histogram visualizes the distribution of a single continuous variable.

Code
fig = px.histogram(tips, x="total_bill")
fig.show()
1020304050051015202530
total_billcount

4.1 Color by Group

You can create separate histograms for different groups to compare their distributions.

Code
fig = px.histogram(tips, x="total_bill", color='sex', barmode='group')
fig.show()
1020304050051015
sexFemaleMaletotal_billcount

5 Bar Chart

A bar chart is used to display categorical data with bars of lengths proportional to the values they represent.

Code
fig = px.bar(tips, x='sex', y='total_bill', color='sex')
fig.show()
FemaleMale050010001500200025003000
sexFemaleMalesextotal_bill

6 Box Plot

A box plot shows the distribution of a dataset, including the median, quartiles, and potential outliers.

Code
fig = px.box(tips, y="total_bill")
fig.show()
1020304050
total_bill

6.1 Color by Group

You can group box plots by a categorical variable to compare distributions.

Code
fig = px.box(tips, y="total_bill", x='sex', color='sex')
fig.show()
FemaleMale1020304050
sexFemaleMalesextotal_bill

7 Strip Plot

A strip plot is a scatter plot for a categorical variable, showing the distribution of data points.

Code
fig = px.strip(tips, x="day", y="total_bill")
fig.show()
SunSatThurFri1020304050
daytotal_bill

7.1 Color by Group

You can color the points in a strip plot to distinguish between different groups.

Code
fig = px.strip(tips, x="day", y="total_bill", color='sex')
fig.show()
SunSatThurFri1020304050
sexFemaleMaledaytotal_bill

8 Facet Plot

Facet plots create subplots for different subsets of the data, allowing for easy comparison.

Code
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day")
fig.show()
02040246810020400204002040
sexFemaleMaletotal_billtotal_billtotal_billtotal_billtipday=Sunday=Satday=Thurday=Fri

8.1 Three Plots per Row

You can control the layout of the facet grid by specifying the number of columns to wrap.

Code
fig = px.scatter(tips, x="total_bill", y="tip", color="sex", facet_col="day", facet_col_wrap=3)
fig.show()
02040510510
sexFemaleMaletotal_billtiptipday=Friday=Sunday=Satday=Thur

9 Customizing Title, Size, and Axis Names

9.1 Add Title

You can add a title to your plot and center it.

Code
fig = px.scatter(tips, x="tip", y="total_bill", title="Total Bill Title").update_layout(title_x=0.5)
fig.show()
24681001020304050
Total Bill Titletiptotal_bill

9.2 Adjust Size

The size of the plot can be customized to fit your needs.

Code
fig = px.scatter(tips, x="tip", y="total_bill")
fig.update_layout(
    autosize=False,
    width=200,
    height=200
)
051002040
tiptotal_bill
Code
fig.show()
051002040
tiptotal_bill

9.3 Change Axis Names

You can change the labels of the x and y axes for clarity.

Code
fig = px.scatter(tips, x="tip", y="total_bill",
                 labels={
                     "tip": "New X Label",
                     "total_bill": "New Y Label"
                 })
fig.show()
24681001020304050
New X LabelNew Y Label

10 Applying Themes

Plotly offers several built-in themes to change the appearance of your plots.

11 Saving a Plot

To save a plot, you may need to install the kaleido package: pip install -U kaleido.

Code
df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada')

12 Animated Plot

Plotly supports animations for many chart types, though smooth transitions are best supported for scatter and bar plots. More information can be found here.

Code
import plotly.express as px
df = px.data.gapminder()
px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100, 100000], range_y=[25, 90])
1002345678910002345678910k23456789100k30405060708090
continentAsiaEuropeAfricaAmericasOceaniayear=1952195219571962196719721977198219871992199720022007gdpPercaplifeExp

13 Reference

Back to top