0% found this document useful (0 votes)
10 views8 pages

PLOTLY

This document shows examples of using Plotly Express to create various charts and plots from sample datasets in Python. It demonstrates line, bar, scatter, histogram, box, violin and pie plots created with Plotly Express using Iris and Tips datasets.

Uploaded by

pranjal0981.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

PLOTLY

This document shows examples of using Plotly Express to create various charts and plots from sample datasets in Python. It demonstrates line, bar, scatter, histogram, box, violin and pie plots created with Plotly Express using Iris and Tips datasets.

Uploaded by

pranjal0981.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [1]:

import plotly.graph_objs as go
import plotly.express as pex

In [2]:

import seaborn as sns

In [3]:

df =sns.load_dataset('iris')
df.head()

Out[3]:

sepal_length sepal_width petal_length petal_width species

0 5.1 3.5 1.4 0.2 setosa

1 4.9 3.0 1.4 0.2 setosa

2 4.7 3.2 1.3 0.2 setosa

3 4.6 3.1 1.5 0.2 setosa

4 5.0 3.6 1.4 0.2 setosa

In [4]:

# line,bar,scatter,histogram,box,scatter_matrix,imshow,pie,violin

# we don't need to rembember all the agruments of all the charts


# we just need to some basic arguments
# Like ----------> data,x,y,color,size,animation_frame,template,
# pie----> names,values
# sctter ---->marginal_x,marginal_y
# scatter_matrix -->data,dimensions

In [5]:

fig = pex.line(df['petal_length'],color=df['species'])
fig.show()

7 color
setosa
versico
6 virginic

5
value

0 20 40 60 80 100 120 140

index

localhost:8888/notebooks/Plotly.ipynb 1/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [6]:

fig = pex.bar(df,x='sepal_width',y='sepal_length',color='species') #,animation_frame='species')


fig.show()

160 species
setosa

140 versico
virginic

120

100
sepal_length

80

60

40

20

0
2 2.5 3 3.5 4

sepal_width

In [7]:

fig = pex.histogram(df['sepal_width'],color=df['species'])
fig.show()

color
35 setosa
versico
virginic
30

25
count

20

15

10

0
2 2.5 3 3.5 4 4.5

value

localhost:8888/notebooks/Plotly.ipynb 2/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [8]:

fig = pex.scatter(x=df['species'],y=df['sepal_width'],color=df['species'],size=df['petal_length'])
fig.show()

4.5 color
setosa
versico
virginic
4

3.5
y

2.5

setosa versicolor virginica

In [9]:

fig = pex.pie(values=df['species'].value_counts(),names=df['species'].value_counts().index)
fig.show()

setosa
versico
virginic

33.3% 33.3%

33.3%

localhost:8888/notebooks/Plotly.ipynb 3/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [10]:

fig = pex.box(df.drop(['species'],axis=1),color=df['species'])
fig.show()

8 color
setosa
versico
7
virginic

5
value

0
sepal_length sepal_width petal_length petal_width

variable

In [11]:

fig = pex.violin(df.drop(['species'],axis=1))
fig.show()

6
value

sepal_length sepal_width petal_length petal_width

variable

localhost:8888/notebooks/Plotly.ipynb 4/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [12]:

df1 = sns.load_dataset('tips')
df1

Out[12]:

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

... ... ... ... ... ... ... ...

239 29.03 5.92 Male No Sat Dinner 3

240 27.18 2.00 Female Yes Sat Dinner 2

241 22.67 2.00 Male Yes Sat Dinner 2

242 17.82 1.75 Male No Sat Dinner 2

243 18.78 3.00 Female No Thur Dinner 2

244 rows × 7 columns

In [13]:

fig = pex.scatter(df1,x = 'tip',y='total_bill',color=df1['sex'],size=df1['size'],animation_frame=df1['size'])


fig.show()

sex
40 Fema
Male
35

30
total_bill

25

20

15

10

1 2 3 4 5 6

tip

size=2
▶ ◼

2 3 4 1 6 5

localhost:8888/notebooks/Plotly.ipynb 5/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [14]:

fig = pex.histogram(df1,x='total_bill',y='tip',histfunc='sum',color='smoker',)
fig.show()

90
smoke
N
Y
80

70

60
sum of tip

50

40

30

20

10

0
10 20 30 40 50

total_bill

In [15]:

fig = pex.scatter_matrix(df,dimensions=['sepal_width','sepal_length','petal_width','petal_length'],color='species')
fig.show()
petal_length petal_width sepal_length sepal_width

4
species
setosa
3 versico
virginic
2

8
7
6
5
4

2 3 4 4 5 6 7 8 0 1 2 2 4 6

sepal_width sepal_length petal_width petal_length

localhost:8888/notebooks/Plotly.ipynb 6/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [16]:

fig = pex.scatter(df,x='sepal_width',y='sepal_length',color='species',marginal_x='box',marginal_y='histogram',trendline='ols',template='pl
fig.show()

species
setosa
versico
virginic

7
sepal_length

4
2 2.5 3 3.5 4 4.5

sepal_width

In [17]:

fig = pex.scatter(df,x='sepal_width',y='sepal_length',color='species',marginal_x='box',marginal_y='histogram',trendline='ols',template='pl
fig.show()# trendline='ols'

species
setosa
versico
virginic

7
sepal_length

4
2 2.5 3 3.5 4 4.5

sepal_width

localhost:8888/notebooks/Plotly.ipynb 7/8
12/25/22, 12:47 PM Plotly - Jupyter Notebook

In [18]:

#Heatmap
fig = pex.imshow(df.drop(['species'],axis=1),text_auto=True, aspect="auto")
fig.show()

0 5.1

4.9
4.7

4.6
3.5

3
3.2

3.1
1.4

1.4
1.3

1.5
0.2

0.2
0.2

0.2

5 3.6 1.4 0.2


5.4 3.9 1.7 0.4

4.6 3.4 1.4 0.3


5 3.4 1.5 0.2

4.4 2.9 1.4 0.2


4.9 3.1 1.5 0.1

5.4 3.7 1.5 0.2

4.8 3.4 1.6 0.2

4.8 3 1.4 0.1

4.3 3 1.1 0.1

5.8 4 1.2 0.2

5.7 4.4 1.5 0.4


5.4 3.9 1.3 0.4

5.1 3.5 1.4 0.3

20
5.7 3.8 1.7 0.3

5.1 3.8 1.5 0.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4.4 1.3

5.6 3 4.1 1.3

5.5 2.5 4 1.3

5.5 2.6 4.4 1.2

6.1 3 4.6 1.4

5.8 2.6 4 1.2

5 2.3 3.3 1

5.6 2.7 4.2 1.3


5.7 3 4.2 1.2

5.7 2.9 4.2 1.3


6.2 2.9 4.3 1.3

100
5.1 2.5 3 1.1

5.7 2.8 4.1 1.3

6.3 3.3 6 2.5

5.8 2.7 5.1 1.9

7.1 3 5.9 2.1

6.3 2.9 5.6 1.8


6.5 3 5.8 2.2

7.6 3 6.6 2.1


4.9 2.5 4.5 1.7

7.3 2.9 6.3 1.8


6.7 2.5 5.8 1.8

7.2 3.6 6.1 2.5

6.5 3.2 5.1 2

6.4 2.7 5.3 1.9

6.8 3 5.5 2.1

5.7 2.5 5 2

5.8 2.8 5.1 2.4

6.4 3.2 5.3 2.3

6.5 3 5.5 1.8

7.7 3.8 6.7 2.2

120
7.7 2.6 6.9 2.3

6 2.2 5 1.5

6.9 3.2 5.7 2.3

5.6 2.8 4.9 2

7.7 2.8 6.7 2

6.3 2.7 4.9 1.8

6.7 3.3 5.7 2.1


7.2 3.2 6 1.8

6.2 2.8 4.8 1.8


6.1 3 4.9 1.8

6.4 2.8 5.6 2.1


7.2 3 5.8 1.6

7.4 2.8 6.1 1.9

7.9 3.8 6.4 2


6.4 2.8 5.6 2.2

6.3 2.8 5.1 1.5


6.1 2.6 5.6 1.4

7.7 3 6.1 2.3


6.3 3.4 5.6 2.4

6.4 3.1 5.5 1.8

140
6 3 4.8 1.8

6.9 3.1 5.4 2.1

6.7 3.1 5.6 2.4

6.9 3.1 5.1 2.3

5.8 2.7 5.1 1.9

6.8 3.2 5.9 2.3

6.7 3.3 5.7 2.5

6.7 3 5.2 2.3

6.3 2.5 5 1.9

6.5 3 5.2 2

6.2 3.4 5.4 2.3

5.9 3 5.1 1.8

sepal_length sepal_width petal_length petal_width

In [ ]:

localhost:8888/notebooks/Plotly.ipynb 8/8

You might also like