Python Plotly
Python Plotly
Plotly
Plotly is an open-source data visualization library that allows you to create interactive and dynamic visualizations in Python, R, and other
programming languages. It provides a wide range of chart types, including scatter plots, bar charts, line charts, pie charts, maps, and more.
Plotly offers a high-level interface through its Plotly Express module, which simpli es the creation of interactive visualizations with a concise
syntax. It also provides a lower-level interface through its graph_objects module, allowing for more customization and ne-grained control over
the visual elements.
One of the key features of Plotly is its interactivity. You can create interactive visualizations that respond to user interactions such as hover,
click, and zoom. Plotly also supports animations, allowing you to create dynamic and engaging visualizations.
Plotly visualizations can be rendered in Jupyter notebooks, as standalone HTML les, or embedded in web applications. You can also share
your visualizations online with others or collaborate with team members by using Plotly's cloud-based platform, Plotly Chart Studio.
Overall, Plotly is a powerful and versatile data visualization library that empowers you to create professional-looking and interactive
visualizations to explore, analyze, and communicate your data effectively.
import plotly.graph_objects as go
import plotly.express as px
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
25
20
15
Y
10
0
1 1.5 2 2.5 3 3.5 4 4.5 5
In this example, we create a basic line plot using the go.Figure class from plotly.graph_objects. We de ne the x and y values as lists
representing the x and y coordinates of the points to be plotted. We use the go.Scatter trace to de ne the data for the line plot. We then update
the layout of the gure using the update_layout method to set the title and labels for the x and y axes. Finally, we display the gure using
g.show().
df = px.data.iris()
species
8
setosa
versicolor
7.5 virginica
7
sepal_length
6.5
5.5
4.5
sepal_width
In this example, we create a scatter plot using the px.scatter function from plotly.express. We use the df DataFrame containing the iris dataset.
We specify the x and y variables as "sepal_width" and "sepal_length", respectively. We also set the color of the markers based on the "species"
column and the size of the markers based on the "petal_length" column. Additionally, we include "petal_width" as hover data, which is displayed
when hovering over the markers. We update the layout with a title and display the gure.
Bar Chart
df = px.data.tips()
Bar Chart
sex
Female
1200
Male
1000
800
total_bill
600
400
200
0
Sun Sat Thur Fri
day
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 1/7
7/6/23, 4:40 PM Plotly.ipynb - Colaboratory
In this example, we create a bar chart using the px.bar function. We use the df DataFrame containing the tips dataset. We set the x and y
variables as "day" and "total_bill", respectively. We also color the bars based on the "sex" column and set the barmode to "group" for grouped
bars. We update the layout with a title and display the gure.
Pie Chart
df = px.data.tips()
Pie Chart
Sat
Sun
Thur
Fri
33.8% 35.6%
23.5%
7.1%
In this example, we create a pie chart using the px.pie function. We use the df DataFrame containing the tips dataset. We specify the values as
the "tip" column and the names as the "day" column. We set the title of the chart and display the gure.
3D Scatter Plot
df = px.data.iris()
3D Scatter Plot
species
setosa
versicolor
virginica
In this example, we create a 3D scatter plot using the px.scatter_3d function. We use the df DataFrame containing the iris dataset. We specify
the x, y, and z variables as"sepal_width", "sepal_length", and "petal_length", respectively. We also set the color of the markers based on the
"species" column. We update the layout with a title and display the gure.
Area Plot
import plotly.graph_objects as go
import plotly.express as px
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 6, 8, 2]
y2 = [2, 2, 7, 10, 5]
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', fill='tozeroy', name='Y1'))
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', fill='tozeroy', name='Y2'))
fig.update_layout(title='Area Plot')
fig.show()
Area Plot
10 Y1
Y2
0
1 1.5 2 2.5 3 3.5 4 4.5 5
In this example, we create an area plot using the go.Scatter trace. We de ne two sets of y-values (y1 and y2) and specify the x-values (x). We
use the mode parameter to set the line mode to 'lines', and the ll parameter to 'tozeroy' to ll the area under the lines. We add the traces to the
gure and update the layout with a title before displaying the gure.
Heatmap
z = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 2/7
7/6/23, 4:40 PM Plotly.ipynb - Colaboratory
Heatmap
8
Z
Y 5
X
2
1
A B C
Here, we create a heatmap using the go.Heatmap trace. We specify the values (z) as a 2D array, and the x and y labels. We create a gure with
the heatmap trace, update the layout with a title, and display the gure.
Bubble Chart
df = px.data.gapminder().query("year == 2007")
Bubble Chart
continent
Asia
80 Europe
Africa
Americas
Oceania
70
lifeExp
60
50
40
2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 2 3 4 5 6
1000 10k
gdpPercap
In this example, we create a bubble chart using the px.scatter function. We use the df DataFrame containing the gapminder dataset for the year
2007. We specify the x and y variables as 'gdpPercap' and 'lifeExp', respectively. We set the size of the bubbles based on the 'pop' column, color
them based on the 'continent' column, and include the country names as hover information. We use the log_x parameter to set a logarithmic x-
axis scale, and size_max to control the maximum size of the bubbles. We update the layout with a title and display the gure.
Choropleth Map
df = px.data.gapminder().query("year == 2007")
Choropleth Map
gdpPercap
40k
30k
20k
10k
In this example, we create a choropleth map using the px.choropleth function. We use the df DataFrame containing the gapminder dataset for
the year 2007. We specify the locations as 'iso_alpha' and the color as 'gdpPercap'. We include the country names as hover information and set
the projection to 'natural earth'. We update the layout with a title and display the gure.
Sankey Diagram
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 3/7
7/6/23, 4:40 PM Plotly.ipynb - Colaboratory
Sankey Diagram
In this example, we create a Sankey diagram using the go.Sankey trace. We de ne the labels, sources, targets, and values for the diagram. We
pass this data to the go.Figure constructor to create the gure. We then update the layout with a title before displaying the gure.
Sunburst Chart
A D
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
E
fig = go.Figure(go.Sunburst(
labels=["A", "B", "C", "D", "E", "F"],
parents=["", "",
B "", "A", "A", "C"],
values=[10, 14, 12, 8, 6, 4], C
))
fig.update_layout(title='Sunburst Chart')
fig.show()
Sunburst Chart
D
A
C
B
In this example, we create a Sunburst chart using the go.Sunburst trace. We de ne the labels, parents, and values for each level of the Sunburst
chart. We pass this data to the go.Figure constructor to create the gure. We then update the layout with a title before displaying the gure.
df = px.data.tips()
Dinner
4
Sat
3
Male
Thur 2
Lunch
Fri
1
Here, we create a Parallel Categories diagram using the px.parallel_categories function. We use the df DataFrame containing the tips dataset.
We specify the dimensions as ['sex', 'day', 'time'], and set the color based on the "size" column. We update the layout with a title and display the
gure.
df = px.data.iris()
species
setosa
versicolor
virginica
In this example, we create a 3D scatter plot with colored markers and size using the px.scatter_3d function. We use the df DataFrame
containing the iris dataset. We specify the x, y, and z variables as "sepal_width", "sepal_length", and "petal_length", respectively. We color the
markers based on the "species" column and set the size based on the "petal_width" column. We update the layout with a title and display the
gure.
Treemap
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
fig = go.Figure(go.Treemap(
labels=labels,
parents=parents,
values=values,
))
fig.update_layout(title='Treemap')
fig.show()
Treemap
A C B
D E F
In this example, we create a Treemap using the go.Treemap trace. We de ne the labels, parents, and values for each level of the Treemap. We
pass this data to the go.Figure constructor to create the gure. We then update the layout with a title before displaying the gure.
df = px.data.wind()
W E
0 0.5 1 1.5 2 2.5
WSW ESE
SW SE
SSW SSE
S
In this example, we create a Polar Scatter Plot using the px.scatter_polar function. We use the df DataFrame containing wind data. We specify
the radial axis as "frequency" and the angular axis as "direction". We color the markers based on the "strength" column and use the "strength"
column for symbol and size parameters. We update the layout with a title and display the gure.
Funnel Chart
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
fig = go.Figure(go.Funnel(
y=labels,
x=values,
textinfo="value+percent initial",
marker=dict(color=["#EB144C", "#FFAA00", "#FFEB84", "#009B72"])
))
fig.update_layout(title='Funnel Chart')
fig.show()
Funnel Chart
1000
Website Visits
100%
600
Downloads
60%
400
Sign-ups
40%
200
Purchases
20%
In this example, we create a Funnel chart using the go.Funnel trace. We de ne the labels and values for each stage of the funnel. We specify the
textinfo parameter to display both the value and the percentage at each stage. We customize the colors of the funnel stages using the marker
parameter. We update the layout with a title and display the gure.
Contour Plot
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]]
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 5/7
7/6/23, 4:40 PM Plotly.ipynb - Colaboratory
Contour Plot
5
24
4.5
20
4
3.5 16
3
12
2.5
8
2
4
1.5
1
1 1.5 2 2.5 3 3.5 4 4.5 5
Here, we create a Contour plot using the go.Contour trace. We de ne the x, y, and z values as lists and a 2D array, respectively. We pass this
data to the go.Figure constructor to create the gure. We then update the layout with a title before displaying the gure.
Radar Chart
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
fig = go.Figure(data=go.Scatterpolar(
r=values,
theta=categories,
fill='toself'
))
fig.update_layout(polar=dict(
radialaxis=dict(visible=True)),
title='Radar Chart'
)
fig.show()
Radar Chart
Marketing
Development
Sales
0 10 20 30 40 50 60 70 80 90
Customer Support
Finance
In this example, we create a Radar chart using the go.Scatterpolar trace. We de ne the categories (axes) and corresponding values for each
category. We pass this data to the go.Figure constructor to create the gure. We use ll='toself' to connect the data points and ll the area. We
update the layout with a polar con guration and a title before displaying the gure.
Network Graph
nodes = [
go.Scatter(
x=[0, 1, 2, 3],
y=[0, 1, 0, 1],
mode='markers',
marker=dict(size=10),
text=['Node A', 'Node B', 'Node C', 'Node D']
)
]
edges = [
go.Scatter(
x=[0, 1, 2],
y=[0, 1, 0],
mode='lines',
line=dict(width=1),
hoverinfo='none'
)
]
Network Graph
trace 0
1
trace 1
0.8
0.6
0.4
0.2
Here, we create a Network graph using the go.Scatter trace. We de ne the nodes and their coordinates as scatter points, and the edges
connecting the nodes as lines. We pass this data to the go.Figure constructor to create the gure. We update the layout with a title before
displaying the gure.
df = px.data.tips()
fig = ff.create_2d_density(
x=df['total_bill'],
y=df['tip'],
colorscale='Viridis'
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 6/7
7/6/23, 4:40 PM Plotly.ipynb - Colaboratory
colorscale= Viridis ,
hist_color='rgba(0,0,0,0)'
)
fig.update_layout(title='Density Contour Plot')
fig.show()
30
20
10
0
10
0 20 40 0 20 40
In this example, we create a Density contour plot using the ff.create_2d_density function from plotly. gure_factory. We pass in the x and y
values from a DataFrame (df). We specify the colorscale and set hist_color to 'rgba(0,0,0,0)' to make the histogram background transparent. We
update the layout with a title before displaying the gure.
1s completed at 4:29 PM
https://colab.research.google.com/drive/1KG8Ed7DK6bsZOHF_CaIm8e231Iga7cNO#scrollTo=09y-8n9Ux9qX&printMode=true 7/7