Python Plotly
Python Plotly
About Plotly
Plotly is a Data Viz library by the company Plotly based out of Canada with support in languages such as Python, Js, Julia etc.
Advantages
Does not work with live data streams. Dash can be explored for that.
In [4]: gap.head()
Out[4]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
Scatter plot
we define the x and y values for the scatter plot, create a scatter plot using go.Scatter(), customize the plot layout using
update_layout(), and finally display the plot using fig.show()
Out[5]:
country continent year lifeExp pop gdpPercap iso_alpha iso_num
1667 West Bank and Gaza Asia 2007 73.422 4018332 3025.349798 PSE 275
1679 Yemen, Rep. Asia 2007 62.698 22211743 2280.769906 YEM 887
50k
40k
30k
GDP
20k
10k
In [7]: # plot life exp and gdp scatter plot -> continent as color -> pop as size -> hover name -> range_x/range_y ->
px.scatter(temp_df, x='lifeExp', y='gdpPercap',
color='continent',size='pop',size_max=100, hover_name='country')
50k
40k
30k
gdpPercap
20k
10k
−10k
Plot animation
plot animation is often used to show the evolution of data or to highlight trends and patterns over time. It can be achieved using
various libraries and techniques, such as Plotly, Matplotlib, or GIF creation tools, to create visually engaging and informative animated
plots.
100k
80k
60k
gdpPercap
40k
20k
−20k
30 40 50 60 70 80 90
lifeExp
year=1952
Line Plot
A line plot is a graphical representation of data points connected by straight lines to show the relationship and trends between two
continuous variables.
1.1B
1B
0.9B
0.8B
pop
0.7B
0.6B
0.5B
0.4B
Out[10]:
country China India Pakistan
year
70
65
60
value
55
50
45
40
3D Line plot
A 3D line plot is a visualization technique that represents data using lines in a three-dimensional space, showing the relationship
between three variables in a single plot.
Bar chart
A bar chart is a graphical representation that uses rectangular bars to compare categories or groups of data based on their values.
1B
0.8B
0.6B
pop
0.4B
0.2B
Out[14]:
country China India Pakistan
year
1.318683B
1.2804B
1.230075B
1.16497B
1B
1.110396B
1.084035B
1.034173B
1.000281B
9
959M
943.455M
8
872M
862.03M
788M
7
754.55M
708M
665.77M
6
637.408M
634M
567M
556.2635M
5
506M
454M
4
409M
372M
3
value
153.4035M
135.5648M
120.065M
100M
105.1869M
9
91.46209M
8
78.15269M
7
69.32592M
6
60.6419M
5
53.10067M
46.67994M
4
41.34656M
5
countr
A
2 A
A
1B A
A
5
A
A
2 B
B
pop
100M B
B
5
B
B
2 B
B
10M B
B
5
B
C
4B
3.5B
3B
2.5B
pop
2B
1.5B
1B
0.5B
0
Asia Europe Africa Americas Oceania
continent
year=1952
boxplot
A boxplot is a compact visualization that displays the summary statistics of a dataset, including the median, quartiles, and outliers,
using a box-and-whisker representation.
In [18]: # boxplot
# using the tips dataset
df = px.data.tips()
# plotting the box chart
fig = px.box(df, x="day", y="total_bill")
# showing the plot
fig.show()
50
40
30
total_bill
20
10
histogram
A histogram is a graphical representation that organizes data into bins and displays the frequency or count of occurrences of each bin.
In [19]: # histogram
# plot histogram of life expt of all countries in 2007 -> nbins -> text_auto
temp_df = gap[gap['year'] == 2007]
px.histogram(temp_df, x='lifeExp',nbins=10,text_auto=True)
40
39
35
30 31
25
count
20
15
12 12
10
10
9
8
7
5
1
0
Gantt Chart
Gantt refers to a type of bar chart that visually represents project schedules or timelines by showing tasks or activities as horizontal
bars along a time axis.
Gantt Chart
1w 1m 6m YTD 1y all
Job C
Job B
20
16
15
2
6 8
10 4 9
count
10 1
4 5
1
9
8
7 5
6 5
5
5 5 5
1
4 4 4
3
2 2
1
1
1
0
Pie chart
A pie chart is a circular statistical graphic that represents data as slices of a pie, with each slice representing a proportion or
percentage of the whole.
0.0515%
0.117%
0.343%
G
0.614%
0.701% T
0.767% F
12.1%
14.1% 0.777% U
0.79% I
0.894%
S
0.929%
10.4% P
0.933%
1.25% R
1.29% N
1.4% G
1.54% P
10.4% 1.7%
B
1.73%
1.75 C
% S
1.7
7%
1.8 H
9.92% 1. 2% S
83
%
2.
A
83
%
3.8% S
6.9% 6.57%
B
D
In [23]: # plot pie chart of world pop in 1952 continent wise -> -> explode(pull)
temp_df = gap[gap['year'] == 1952].groupby('continent')['pop'].sum().reset_index()
px.pie(temp_df, values='pop', names='continent')
17.4%
14.3%
58%
9.87%
0.444%
Sunburst plot
A Sunburst plot in Plotly is a circular hierarchical visualization that represents data in a radial form, showing the hierarchy of categories
through concentric circles and arcs.
India
Ind
on China
es
Pak ia
ista
n
Bang
lades
h Asia
Japan
Philippines
New Zealand
Oceania
Australia Iceland
Montenegro
Slovenia
Albania
Ireland
Euro
Croatia
Bosnia and Herzegovina
Norway
Finland
Slovak Republic
Vietnam
Denmark
Bulgaria
Switzerland
pe
Austria
Sweden
Hungary
Serbia
Czech Republic
Belgium
Portuga
l
Iran
Greece
Netherlan
ds
Romani
a
Poland
and
Thail ep. Unit
Spain
a, R Italy
Kore
nm
ar
Africa Americas ed
Kin
gdo
Mya tan m
han
is Fra
Afg l nce
Ge Turke
pa rabia
Ne
di A
Sau
q
Ira ysia m. Re
p.
rm y
an
la e
Ma ,D
rea n
y
Ko .
a Rep
ria
iw
Ta en, a
m
Ye ank
L
Unite
Sri a
e
ri dia
Sy
Trinidad
Jamaic
Panam
and
Tobago
bo China
Puer Urugu a
a
Nic Cost to
ig
ay
Rico
Par ara a Rica
El gua
Sal agu
Cam ng, Do Ho vad ay
Ko m nd
ng in ura or
Ho el ica Ha s
n
Isra an re and
Gaza
Re Boliv iti
G
Ve
Jord apo
pu ia
uat Cu
p.
Bank
Sing
West non bl
ic
N
Ecu em ba
Leba
ne
olia
Oman
Mong
ad ala
Kuwait
Bahrain
Re
zu Chile or
P ela
M
er
Ar lom
hi t
Ca
ia
u
m.
Co
p
ge
na a
d Stat
op
nt ia
ex
y
da
Bra
De
in
Eg
ica
b
o,
ico
Su h Afr
ng
Et
zil
Co
Ken ania
n
ut
da
So
Alge co
z
es
ya
Tan
c
ria
oro
a
bique
gand
ascar
oire
a
M
n
Mozam
na Faso
Ghan
Cote d'Iv
Cameroo
Madag
U
Zimbabwe
Malawi
Burki
Senegal
Angola
Zambia
Tunisia
Sierra Leon
Somalia
Guinea
Rwanda
Burundi
Mali
Chad
enin
Congo, Re
Mauritan
Eritre
Liby
Tog
Guinea-B
Libe
Nam
Botsw
Leso
Ga
Ma
Swa
Equa
G
In [25]: px.sunburst(tips,path=['sex','smoker','day','time'],values='total_bill',
color='size')
Dinner
Lunch
Di
nn Sat
er
Thur Dinner
i
Fr
No Sun
Dinner
Sat
le
Ma
Sun Dinner
Yes
Fri
Female Yes
Lunch
Dinner
Thu
r L
Sun unch
Dinner Sat
r No
T hu Dinner
i
Fri Sat Fr
er
Lu
nc
D
Dinner
h
in
ne
nn
r
Di
Dinner
h
Lunch
nc
Lu
Dinner
Tree Map
A treemap in Plotly is a type of interactive data visualization that represents hierarchical data using nested rectangles, with each
rectangle's size proportional to a specific attribute or value associated with it.
In [26]: # Treemap
temp_df = gap[gap['year'] == 2007]
px.treemap(temp_df, path=[px.Constant('World'),'continent','country'],
values='pop',color='lifeExp')
World
Asia Africa Americas
Ethiopia
Congo, Dem. Rep. South Africa
Brazil
Sudan Algeria Uganda Ghana
Tanzania
Indonesia Bangladesh Philippines Vietnam Iran Zimbabwe Senegal
Mali Zambia
Madagascar
Colombia Canada Venezue
Guatemala Do
Argentina
Togo
Congo, Rep. Mauritania
Chad Rwanda
Morocco
Namibia Lesotho
Peru B
Thailand
Afghanistan
Nepal
Saudi Arabia Liberia
Cameroon Eritrea
Cuba
Gambia
Guinea-Bissau Mauritius Swaziland
Guinea Burundi
H
Djibouti
Comoros
Malaysia
Hungary
Israel
Singapore
West Bank and Gaza
Myanmar
Greece
Syria
Sweden
Korea, Dem. Rep.
Spain Romania
Lebanon Mongolia
Jordan
Portugal
Oman
Kuwait
Austria
Heatmap
Heat map in Plotly refers to a graphical representation of data where values are depicted using colors on a grid-like structure, allowing
for easy visualization and analysis of patterns, trends, and variations in the data.
In [27]: # Heatmap -> find heatmap of all continents with year on avg life exp
temp_df = gap.pivot_table(index='year',columns='continent',
values='lifeExp',aggfunc='mean')
px.imshow(temp_df)
1950
1960
1970
year
1980
1990
2000
3D scatter plot
A 3D scatter plot in Plotly is a visual representation of data points in a three-dimensional space, where each point is defined by its x, y,
and z coordinates, allowing for the visualization of relationships and patterns in three dimensions.
In [28]: # 3d scatterplot
# plot a 3d scatter plot of all country data for 2007
temp_df = gap[gap['year'] == 2007]
px.scatter_3d(temp_df, x='lifeExp',y='pop',z='gdpPercap',
log_y=True,color='continent',hover_name='country')
In [29]: px.scatter_3d(iris,x='sepal_length',y='sepal_width',
z='petal_length',color='species')
Scatter_matrix
A scatter matrix in Plotly is a compact grid of scatter plots that displays the relationships and correlations between multiple variables in
a single visualization.
sepal_length 8
7
6
5
4
sepal_width
2
petal_length
2
petal_width
Facet Plot
Facet plot in Plotly refers to the visualization technique that divides a dataset into subsets based on one or more categorical variables,
creating a grid of smaller plots for each subset, allowing for easy comparison and exploration of relationships between variables.
smoker=No smoker=Yes
10
6
tip
10
6
tip
In [32]: px.histogram(tips,x='total_bill',facet_row='sex')
15
count
10
15
count
10
gdpPercap
100k
50k
100k
50k
100k
50k
100k
50k
3D Surface plot
A 3D surface plot in Plotly refers to the visualization of data as a three-dimensional surface, where the x, y, and z values are plotted in
a three-dimensional space to represent the variation of a variable across multiple dimensions.
3D Surface Plot
Contour Plot
A contour plot in Plotly is a graphical representation that displays the 2D variation of a continuous variable through contour lines.
3D Surface Plot
10
−5
10
SubPlots
Subplots in Plotly refer to the arrangement of multiple plots or charts within a single figure, allowing for side-by-side or stacked
visualizations.
In [37]: # Subplots
from plotly.subplots import make_subplots
In [39]: fig.add_trace(
go.Scatter(x=[1,9,5],y=[2,10,1]),
row = 1,
col = 1
)
fig.add_trace(
go.Histogram(x=[1,9,5,22,109,134,56,78,12,34,89]),
row = 1,
col = 2
)
fig.update_layout(title='Subplot Demo')
fig.show()
Subplot Demo
10 6
5
2
0
2 4 6 8 0 50 100
Scatter_geo
scatter_geo in Plotly is a function used to create scatter plots on geographical maps.
countr
A
A
A
A
A
A
A
B
B
B
B
B
B
B
B
B
B
B
C
Scatter Polar
Scatter Polar in Plotly is a visualization technique that represents data points in a polar coordinate system, where the distance from
the center represents one variable, and the angle represents another variable.
0°
315° 45°
270° 90°
Sun Fri Sat Thur
225° 135°
Scatter terenary
Scatter ternary in Plotly refers to the creation of triangular scatter plots where data points are represented within a ternary diagram.
This type of plot allows the visualization of three variables that add up to a constant sum, typically represented by the three vertices of
the triangle. It provides insights into the relative proportions and relationships between the variables.
In [43]: df = px.data.iris()
plot = px.scatter_ternary(df, a = 'sepal_width',
b = 'sepal_length',
c='petal_width',
color = 'species',
size = 'petal_length')
plot.show()
sepal_width
10
0.8 0.2
0.6 0.4
0.4 0.6
0.2 0.8
0 1
In [44]: df = px.data.iris()
plot = px.scatter_ternary(df, a = 'sepal_width',
b = 'sepal_length',
c = 'petal_width',
color = 'species',
size = 'petal_length',
symbol = 'species_id')
plot.show()
sepal_width
10
0.8 0.2
0.6 0.4
0.4 0.6
0.2 0.8
0 1
3D Cones
3D cones in Plotly are graphical representations of cone-shaped objects in a three-dimensional space, used for visualizing data or
geometric concepts.
3D Volume Plots
3D volume plots in Plotly refer to the creation of visual representations that depict volumetric data in a three-dimensional space,
allowing for the visualization of complex structures and distributions.
C:\Users\user\AppData\Local\Temp/ipykernel_2640/3749250901.py:15: RuntimeWarning:
3D streamtube plots
3D streamtube plots in Plotly refer to the creation of visualizations that depict fluid flow using streamlines that are enclosed within a
tube-like structure, providing a three-dimensional representation of the flow behavior.
3D Mesh Plots
3D mesh plots in Plotly refer to visualizations that represent a surface or a mesh-like structure using a combination of x, y, and z
coordinates in a three-dimensional space.
3D Scatter points
3D Scatter points in Plotly refer to the visual representation of data points in a three-dimensional space using markers or symbols. It
allows for the plotting of data points with three different variables, where each variable corresponds to the position of the point along
the x, y, and z axes.
fig.show()
Create tables
Creating tables in Plotly refers to the process of generating tabular data structures with customizable formatting and styling for visual
representation and analysis purposes.
A B C
10 40 11
20 20 22
30 10 32
40 50 40
A B
10 40
20 20
30 10
40 50
Sankey Diagram
A sankey diagram is a visualization used to depict a flow from one set of values to another.
Dendograms
A dendrogram is a diagram representing a tree. The figure factory called create_dendrogram performs hierarchical clustering on data
and represents the resulting tree. Values on the tree depth axis correspond to distances between clusters.
1.5
0.5
0
8 0 2 3 7 9 4 5 1 6
Hi
IIIH
Candle Stick
The candlestick chart is a style of financial chart that describes o for a given x coordinate (most likely time). The boxes represent the
spread between the open and close values and the lines represent the spread between the low and high values.
In [55]: # candlestick
from datetime import datetime
open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
dates = [datetime(year=2013, month=10, day=10),
datetime(year=2013, month=11, day=10),
datetime(year=2013, month=12, day=10),
datetime(year=2014, month=1, day=10),
datetime(year=2014, month=2, day=10)]
fig = go.Figure(data=[go.Candlestick(x=dates,
open=open_data, high=high_data,
low=low_data, close=close_data)])
fig.show()
34.5
34
33.5
33
32.5
Oct 2013 Nov 2013 Dec 2013 Jan 2014 Feb 2014
34.5
34
33.5
33
32 5
streamline plot
streamline plots are based on the representation on a 2-D vector field which is explained as velocity fields, which are consist of closed
curves that are tangent to the velocity field. Streamlining is the fastest technique and more efficient for getting the data. Velocity values
are interpolated when determining the streamlines. Streamlines are initialized on the boundary of the x-y domain.
0.5
−0.5
−1
N
NNW NNE
NW NE
WNW ENE
W E
0-1
2-3
4-4
5-6
WSW ESE
SW SE
SSW SSE
Bullet Chart
This method is used to create bullet charts. This function can take both dataframes or a sequence of dictionaries.
Syntax: plotly.figure_f
width=700)
fig.show()
Bullet Chart
25 4.5
250 2000
500
4
200 20 3.5
400 1500
150 15
300 2.5
1000
2
100 10
200
1.5
500
1
50 5 100
0.5
0 0 0 0 0
dict(
label='Play',
method='animate',
args=[None, {'frame': {'duration': 50, 'redraw': True}, 'fromcurrent': True, 'transition'
),
dict(
label='Pause',
method='animate',
args=[[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate', 'transitio
)
],
)
]
)
# Display the animation
fig.show()
3D Animation Example
Play
Pause
)
)]) for i in range(2, len(t))]
# Add frames to the figure
fig.frames = frames
# Update animation settings
fig.update_layout(
title='3D Animation with Multiple Colors',
scene=dict(
xaxis_title='X-axis',
yaxis_title='Y-axis',
zaxis_title='Z-axis'
),
updatemenus=[
dict(
type='buttons',
buttons=[
dict(
label='Play',
method='animate',
args=[None, {'frame': {'duration': 50, 'redraw': True}, 'fromcurrent': True, 'transition'
),
dict(
label='Pause',
method='animate',
args=[[None], {'frame': {'duration': 0, 'redraw': False}, 'mode': 'immediate', 'transitio
)
],
)
]
)
# Display the animation
fig.show()
Play
Pause
80k
70k
50k
40k
30k
20k
10k
0
20 30 40 50 60 70 80
Life Expectancy
year=2007
7.5
6.5
Sepal Length
5.5
4.5
4
1.5 2 2.5 3 3.5 4 4.5 5
Sepal Width
petal width=2.3
12
10
8
Tip
0
0 10 20 30 40 50
Total Bill
time=Lunch
In [ ]: