Plotly PDF
Plotly PDF
Plotly PDF
Loading Datasets
In [2]: pokemon = pd.read_csv("C:/Users/DELL/Documents/GitHub/Public/Data-Visualization/Plotly/Datasets/pokemon_updated.csv")
pokemon.head(10)
Out[2]:
# Name Type 1 Type 2 HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary Total
3 3 VenusaurMega Venusaur Grass Poison 80 100 123 122 120 80 1 False 625
7 6 CharizardMega Charizard X Fire Dragon 78 130 111 130 85 100 1 False 634
8 6 CharizardMega Charizard Y Fire Flying 78 104 78 159 115 100 1 False 634
Out[3]:
gender race/ethnicity parental level of education lunch test preparation course math score reading score writing score
Out[4]:
Country Confirmed Recovered Deaths
Date
2020-01-22 Afghanistan 0 0 0
2020-01-22 Albania 0 0 0
2020-01-22 Algeria 0 0 0
2020-01-22 Andorra 0 0 0
2020-01-22 Angola 0 0 0
2020-01-22 Argentina 0 0 0
2020-01-22 Armenia 0 0 0
2020-01-22 Australia 0 0 0
2020-01-22 Austria 0 0 0
In [5]: spotify = pd.read_csv("C:/Users/DELL/Documents/GitHub/Public/Data-Visualization/Plotly/Datasets/spotify.csv" , index_col="Date")
spotify.head(10)
Out[5]:
Shape of You Despacito Something Just Like This HUMBLE. Unforgettable
Date
Out[6]:
longitude latitude housing_median_age total_rooms total_bedrooms population households median_income median_house_value ocean_proximity
20635 -121.09 39.48 25.0 1665.0 374.0 845.0 330.0 1.5603 78100.0 INLAND
20636 -121.21 39.49 18.0 697.0 150.0 356.0 114.0 2.5568 77100.0 INLAND
20637 -121.22 39.43 17.0 2254.0 485.0 1007.0 433.0 1.7000 92300.0 INLAND
20638 -121.32 39.43 18.0 1860.0 409.0 741.0 349.0 1.8672 84700.0 INLAND
20639 -121.24 39.37 16.0 2785.0 616.0 1387.0 530.0 2.3886 89400.0 INLAND
Out[7]:
age sex bmi children smoker region charges
Out[8]:
Age Gender Period Unemployed
Out[9]:
ticket requestor RequestorSeniority ITOwner FiledAgainst TicketType Severity Priority daysOpen Satisfaction
Out[10]:
Species Weight Length1 Length2 Length3 Height Width
Out[11]:
id diet pulse time kind
Out[12]:
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
5 Albania 1987 female 75+ years 1 35600 2.81 Albania1987 NaN 2,156,624,900 796 G.I. Generation
6 Albania 1987 female 35-54 years 6 278800 2.15 Albania1987 NaN 2,156,624,900 796 Silent
7 Albania 1987 female 25-34 years 4 257200 1.56 Albania1987 NaN 2,156,624,900 796 Boomers
8 Albania 1987 male 55-74 years 1 137500 0.73 Albania1987 NaN 2,156,624,900 796 G.I. Generation
9 Albania 1987 female 5-14 years 0 311000 0.00 Albania1987 NaN 2,156,624,900 796 Generation X
Out[13]:
Id Sepal Length (cm) Sepal Width (cm) Petal Length (cm) Petal Width (cm) Species
Out[14]:
Type Coverage OdName AREA AreaName REG RegName DEV DevName 1980 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
0 Immigrants Foreigners Afghanistan 935 Asia 5501 Southern Asia 902 Developing regions 16 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
1 Immigrants Foreigners Albania 908 Europe 925 Southern Europe 901 Developed regions 1 ... 1450 1223 856 702 560 716 561 539 620 603
2 Immigrants Foreigners Algeria 903 Africa 912 Northern Africa 902 Developing regions 80 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
3 Immigrants Foreigners American Samoa 909 Oceania 957 Polynesia 902 Developing regions 0 ... 0 0 1 0 0 0 0 0 0 0
4 Immigrants Foreigners Andorra 908 Europe 925 Southern Europe 901 Developed regions 0 ... 0 0 1 1 0 0 0 0 1 1
5 rows × 43 columns
In [15]: canada.columns
In [16]: canada.drop(columns=['AREA' , 'DEV', 'DevName' , 'REG', 'Type', 'Coverage' , 'AreaName', 'RegName' ], inplace=True)
canada.head()
Out[16]:
OdName 1980 1981 1982 1983 1984 1985 1986 1987 1988 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
0 Afghanistan 16 39 39 47 71 340 496 741 828 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
1 Albania 1 0 0 0 0 0 1 2 2 ... 1450 1223 856 702 560 716 561 539 620 603
2 Algeria 80 67 71 69 63 44 69 132 242 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
4 Andorra 0 0 0 0 0 0 2 0 0 ... 0 0 1 1 0 0 0 0 1 1
5 rows × 35 columns
Out[17]:
Country 1980 1981 1982 1983 1984 1985 1986 1987 1988 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Country
Afghanistan Afghanistan 16 39 39 47 71 340 496 741 828 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
Albania Albania 1 0 0 0 0 0 1 2 2 ... 1450 1223 856 702 560 716 561 539 620 603
Algeria Algeria 80 67 71 69 63 44 69 132 242 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
5 rows × 35 columns
In [18]: canada2 = canada.copy()
canada2.head()
Out[18]:
Country 1980 1981 1982 1983 1984 1985 1986 1987 1988 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Country
Afghanistan Afghanistan 16 39 39 47 71 340 496 741 828 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
Albania Albania 1 0 0 0 0 0 1 2 2 ... 1450 1223 856 702 560 716 561 539 620 603
Algeria Algeria 80 67 71 69 63 44 69 132 242 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
5 rows × 35 columns
In [19]: canada.index.name=None
canada.head()
Out[19]:
Country 1980 1981 1982 1983 1984 1985 1986 1987 1988 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Afghanistan Afghanistan 16 39 39 47 71 340 496 741 828 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
Albania Albania 1 0 0 0 0 0 1 2 2 ... 1450 1223 856 702 560 716 561 539 620 603
Algeria Algeria 80 67 71 69 63 44 69 132 242 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
5 rows × 35 columns
Out[20]:
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 ... 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Afghanistan 16 39 39 47 71 340 496 741 828 1076 ... 2978 3436 3009 2652 2111 1746 1758 2203 2635 2004
Albania 1 0 0 0 0 0 1 2 2 3 ... 1450 1223 856 702 560 716 561 539 620 603
Algeria 80 67 71 69 63 44 69 132 242 434 ... 3616 3626 4807 3623 4005 5393 4752 4325 3774 4331
Andorra 0 0 0 0 0 0 2 0 0 0 ... 0 0 1 1 0 0 0 0 1 1
5 rows × 34 columns
Out[22]:
American Antigua and Venezuela (Bolivarian Viet Western
Afghanistan Albania Algeria Andorra Angola Argentina Armenia Australia ... Uzbekistan Vanuatu Yemen Zambia Zimbabwe Unknown Total
Samoa Barbuda Republic of) Nam Sahara
Scatter Plot
In [23]: #Simple Scatter Plot
random_x = np.random.randint(1,100,50)
random_y = np.random.randint(1,100,50)
data = [go.Scatter(
x = random_x,
y = random_y,
mode = 'markers'
)
]
layout = go.Layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
iplot(fig)
80
60
40
20
0 20 40 60 80 100
In [24]: # Changing Marker size , shape & color using Marker parameter
x_val = np.random.randint(1,100,50)
y_val = np.random.randint(1,100,50)
data = [go.Scatter(
x = x_val,
y = y_val,
mode = 'markers',
marker = dict(
size = 10,
color = '#91bd3a', #color of marker
symbol = 'circle', # Shape of scatter plot
line = dict(width = 1) #width of boundary
)
)
]
layout = go.Layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
iplot(fig)
100
80
60
40
20
0 20 40 60 80 100
In [25]: # Defining Labels (X-Axis & Y-Axis label , Graph tile)
x_val = np.random.randint(1,100,50)
y_val = np.random.randint(1,100,50)
data = [go.Scatter(
x = x_val,
y = y_val,
mode = 'markers',
marker = dict(
size = 10,
color = '#4ED700',
symbol = 'circle',
line = dict(width = 1,color = '#0E8700')
)
)
]
layout = go.Layout(
title = '$Scatter Plot$', # Title
xaxis = dict(title = '$X-Axis$',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = '$Y-Axis$',showgrid=False,showline=False), # y-axis label
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)
100
80
60
40
20
0 20 40 60 80 100
In [26]: x_values = np.linspace(0, 100, 100) # 100 evenly spaced values
y_values = np.random.randn(100) # 100 random values
trace0 = go.Scatter(
x = x_values,
y = y_values,
mode = 'markers',
marker = dict(
size = 7,
color = '#F4D03F',
symbol = 'circle',
line = dict(width = 1,color = '#0E8700')
)
)
trace1 = go.Scatter(
x = x_values,
y = y_values-5,
mode = 'markers',
marker = dict(size = 7,
color = '#A9DFBF',
symbol = 'circle',
line = dict(width = 1,color = '#0E8700')
)
)
layout = go.Layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
iplot(fig)
trace 0
trace 1
2
−2
−4
−6
−8
0 20 40 60 80 100
In [27]: insurance.head(10)
Out[27]:
age sex bmi children smoker region charges
layout = go.Layout(
title = '$Insurance$', # Chart Title
xaxis = dict(title = '$BMI$',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = '$Charges$',showgrid=False,showline=False), # y-axis label
)
iplot(fig)
60k
50k
40k
30k
20k
10k
15 20 25 30 35 40 45 50 55
In [29]: data = [go.Scatter(
x = insurance.bmi,
y = insurance.charges,
mode = 'markers',
marker = dict(size = 7,
color = '#4ED700',
symbol = 'circle',
line = dict(width = 1,color = '#0E8700')
)
)
]
layout = go.Layout(
title = '$Insurance$', # Title
xaxis = dict(title = '$BMI$',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = '$Charges$',showgrid=False,showline=False), # y-axis label
)
# Updating Traces
fig.update_traces(
marker=dict(color="#e6e56c"),
)
iplot(fig)
60k
50k
40k
30k
20k
10k
15 20 25 30 35 40 45 50 55
In [30]: # trace0 will capture all smokers
trace0 = go.Scatter(
x = insurance[insurance.smoker=='yes'].bmi,
y = insurance[insurance.smoker=='yes'].charges,
mode = 'markers',
name = 'Smoker',
marker = dict(size = 7, color = '#F39C12',symbol = 'circle',line = dict(width = 1))
)
layout = go.Layout(
title = '$Scatter Plot$', # Title
xaxis = dict(title = '$BMI$',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = '$Charges$',showgrid=False,showline=False), # y-axis label
)
Smoker
60k Non-Smoker
50k
40k
30k
20k
10k
15 20 25 30 35 40 45 50 55
In [31]: # trace0 will capture all smokers
trace0 = go.Scatter(
x = insurance[insurance.smoker=='yes'].bmi,
y = insurance[insurance.smoker=='yes'].charges,
mode = 'markers',
name = 'Smoker',
marker = dict(size = 14, color = '#9ca4c4',symbol = 'circle',
line = dict(width = 1,color = '#5D6D7E')
)
#Layout Setting
layout = go.Layout(
title=dict(text = "Insurance Data",x=0.5,y=0.95),
title_font_size=30,
title_font_color='#F1C40F',
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=True, # Show X-Axis
linecolor='black', # Color of X-axis
tickfont_color='black', #Color of ticks
showticklabels=True, #Show X labels
dtick=5,
ticks='outside',
tickcolor='black',
),
yaxis=dict(
showgrid=False,
showline=True,
linecolor='black',
tickfont_color='black',
showticklabels=True,
ticks='outside',
tickcolor='black',
),
legend=dict(
font_size=15,
yanchor='bottom',
xanchor='right',
),
paper_bgcolor='white',
plot_bgcolor='white',
hovermode='closest',
width=970,
height=800,
)
60k
50k
40k
30k
20k
10k
15 20 25 30 35 40 45 50 55
In [32]: # Display multiple Scatter plots in one figure using Subplots
#Subplot initialization
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Subplot-1", "Subplot-2")
)
fig.show()
Subplot-1 Subplot-2
100 100 trace 0
trace 1
80 80
60 60
40 40
20 20
0 50 100 0 50 100
In [33]: # Display multiple Scatter plots in one figure using Subplots
#Subplot initialization
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Subplot-1", "Subplot-2")
)
fig.show()
Subplot-1 Subplot-2
100 100 trace 0
trace 1
80 80
60 60
40 40
20 20
0 50 100 0 50 100
fig.update_layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig.show()
40
30
20
10
y
−10
−20
−30
−40
−30 −20 −10 0 10 20 30
x
In [57]: fig = px.scatter(
insurance,
x=insurance.bmi,
y= insurance.charges,
color="smoker", # Show groups with different colors using "color" parameter
size=insurance.charges
)
fig.update_layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig.show()
70k
smoker
yes
60k no
50k
40k
charges
30k
20k
10k
15 20 25 30 35 40 45 50 55
bmi
In [46]: fig = px.scatter(insurance,
x=insurance.bmi,
y= insurance.charges,
color="smoker", # Show groups with different colors using "color" parameter
size=insurance.charges,
color_discrete_map={"yes": "#FF5722","no": "#7CB342"} #Map colors to data values
)
fig.update_layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig.show()
70k
smoker
yes
60k no
50k
40k
charges
30k
20k
10k
15 20 25 30 35 40 45 50 55
bmi
In [51]: # Using facet_col arguments to create Sub plots
fig = px.scatter(insurance,
x=insurance.bmi,
y=insurance.charges,
color=insurance.smoker, # Show groups with different colors using "color" parameter
facet_col=insurance.region, # Using facet_col arguments to create Sub plots
color_discrete_map={"yes": "#FF5722","no": "#7CB342"}, #Map colors to data values
title="Insurance Data"
)
fig.show()
Insurance Data
50k
40k
charges
30k
20k
10k
20 30 40 50 20 30 40 50 20 30 40 50 20 30 40 50
fig.update_layout(
plot_bgcolor= "#dcedc1",
paper_bgcolor="#FFFDE7",
)
fig.show()
Insurance Data
50k
40k
sex=female
charges
30k
20k
10k
60k
50k
40k
sex=male
charges
30k
20k
10k
0
20 30 40 50 20 30 40 50 20 30 40 50 20 30 40 50
fig.update_layout(
plot_bgcolor= "#dcedc1",
paper_bgcolor="#FFFDE7",
)
fig.show()
#po.plot(fig)
Insurance Data
50k
40k
sex=female
charges
30k
20k
10k
60k
50k
40k
sex=male
charges
30k
20k
10k
0
20 30 40 50 20 30 40 50 20 30 40 50 20 30 40 50
Line Plot
In [59]: #Simple Line Plot
# create traces
trace0 = go.Scatter(
x = x_values,
y = y_values,
mode = 'lines',
)
layout = go.Layout(
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=trace0,layout=layout)
iplot(fig)
−1
−2
−3
0 20 40 60 80 100
In [60]: #Line Styling
trace0 = go.Scatter(
x = x_values,
y = y_values,
mode = 'lines+markers',
)
trace1 = go.Scatter(
x = x_values,
y = y_values-5,
mode = 'lines',
)
data = [trace0, trace1]
fig = go.Figure(data=data,layout=layout)
iplot(fig)
trace 0
2 trace 1
−2
−4
−6
−8
0 20 40 60 80 100
In [350]: canada.head()
Out[350]:
American Antigua and Venezuela (Bolivarian Viet Western
Afghanistan Albania Algeria Andorra Angola Argentina Armenia Australia ... Uzbekistan Vanuatu Yemen Zambia Zimbabwe Unknown Total
Samoa Barbuda Republic of) Nam Sahara
# Layout setting
layout = go.Layout(
title = '$Immigrants$', # Title
xaxis = dict(title = 'Year',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = 'Number of Immigrants',showgrid=False,showline=False), # y-axis label
30k
25k
20k
15k
10k
5k
0
1980 1985 1990 1995 2000 2005 2010
Year
In [63]: # Plot Immigrants from China
china = go.Scatter(
x = canada.index.values,
y = canada['China'],
mode = 'lines',
name = 'China'
)
layout = go.Layout(
title = '$Immigrants$', # Title
xaxis = dict(title = 'Year',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = 'Number of Immigrants',showgrid=False,showline=False), # y-axis label
paper_bgcolor= '#FFFDE7' # Paper background color
30k
25k
20k
15k
10k
5k
0
1980 1985 1990 1995 2000 2005 2010
Year
In [64]: # Plot Immigrants from China
china = go.Scatter(
x = canada.index.values,
y = canada['China'],
mode = 'lines',
name = 'China'
)
layout = go.Layout(
title=dict(text = "Immigration Data",x=0.5,y=0.95),
xaxis = dict(title = 'Year',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = 'Number of Immigrants',showgrid=False,showline=False), # y-axis label
paper_bgcolor= '#FFFDE7',
plot_bgcolor= '#FFFDE7'
)
China
40k India
Pakistan
35k
Number of Immigrants
30k
25k
20k
15k
10k
5k
0
1980 1985 1990 1995 2000 2005 2010
Year
Out[65]:
American Antigua and United States of Venezuela (Bolivarian Viet Western
Afghanistan Albania Algeria Andorra Angola Argentina Armenia Australia ... Uruguay Uzbekistan Vanuatu Yemen Zambia Zimbabwe
Samoa Barbuda America Republic of) Nam Sahara
Out[66]:
American Antigua and Venezuela (Bolivarian Viet Western
Afghanistan Albania Algeria Andorra Angola Argentina Armenia Australia ... Uzbekistan Vanuatu Yemen Zambia Zimbabwe Unknown Total
Samoa Barbuda Republic of) Nam Sahara
layout = go.Layout(
title = 'Immigrants', # Title
title_font=dict(size=20),
xaxis = dict(title = 'Year',showgrid=False,showline=False), # x-axis label
yaxis = dict(title = 'Number of Immigrants',showgrid=False,showline=False), # y-axis label
font=dict(size=10),
width=1230,
height=650
)
Afghanistan
Albania
40k Algeria
American Samoa
Andorra
35k
Angola
Antigua and Barbuda
30k Argentina
Armenia
Number of Immigrants
Australia
25k
Austria
Azerbaijan
20k Bahamas
Bahrain
Bangladesh
15k Barbados
Belarus
Belgium
10k
Belize
Benin
5k Bhutan
Bolivia (Plurinational State of)
Bosnia and Herzegovina
0
Botswana
Brazil
1980 1985 1990 1995 2000 2005 2010
Year
In [71]: from plotly.subplots import make_subplots
#Subplot initialization
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Immigration Data", "Insurance Data")
)
Sub Plots
Immigration Data Insurance Data
China
40k 60k India
Pakistan
35k Smoker
50k Non-Smoker
Number of Immigrants
30k
40k
25k Charges
20k 30k
15k
20k
10k
10k
5k
0 0
Year BMI
fig.show()
Sine Plot
0.5
0
y
−0.5
−1
0 2 4 6 8 10
x
In [73]: fig = px.line(
x=col1,
y= col2
)
fig.add_scatter(x=col1,
y= col2+1
)
fig.show()
2 trace 1
1.5
0.5
y
−0.5
−1
0 2 4 6 8 10
x
In [74]: # Plot Immigrants from India
fig = px.line(
x=canada1.index.values,
y= canada1['India']
)
fig.update_layout(title_text='Immigrants')
fig.show()
Immigrants
Pakistan
35k Bangladesh
30k
25k
20k
y
15k
10k
5k
x
In [56]: fig = px.line(
employment,
x="Period" ,
y = "Unemployed",
color='Gender'
)
fig.show()
Gender
1M
Men
Women
0.8M
Unemployed
0.6M
0.4M
0.2M
Period
Bar Plot
In [75]: # Use go.Bar to plot Bar charts in Plotly
x = np.arange(1,10)
y = np.arange(20,110,10)
data = go.Bar(
x= x,
y= y,
)
layout = go.Layout(
title = 'Simple Bar Chart',
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
fig.show()
100
80
60
40
20
0
1 2 3 4 5 6 7 8 9
In [76]: # Use go.Bar to plot Bar charts in Plotly
x1 = [1,2,3,3,3,4,5]
y1 = [10,20,30,40,50,60,65]
data = go.Bar(
x= x1,
y= y1,
)
layout = go.Layout(
title = 'Simple Bar Chart',
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
fig = go.Figure(data=data,layout=layout)
fig.show()
120
100
80
60
40
20
0
1 2 3 4 5
In [77]: #Changing color of Bar plot
x = np.arange(1,10)
y = np.arange(20,130,10)
data = go.Bar(
x= x,
y= y,
marker={'color' : '#FFA726'} # changing color of bar plot
)
layout = go.Layout(
title = 'Simple Bar Chart',
width=970,
height=650,
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
fig = go.Figure(data=data,layout=layout)
fig.show()
Simple Bar Chart
100
80
60
40
20
0
1 2 3 4 5 6 7 8 9
In [78]: # Changing width of Bar Plot
x = np.arange(1,10)
y = np.arange(20,130,10)
wid = [0.2,]*9
data = go.Bar(
x= x,
y= y,
marker={'color' : '#FFA726'}, # Changing color of bars
width=wid # Changing width of Bars
)
layout = go.Layout(
title = 'Simple Bar Chart',
width=970,
height=650,
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Simple Bar Chart
100
80
60
40
20
0
1 2 3 4 5 6 7 8 9
In [79]: x = np.arange(1,10)
y = np.arange(20,130,10)
data = go.Bar(
x= x,
y= y,
marker_color= '#FFA726', # Changing color of Bars
marker_line_color = '#A04000', # Changing color of border
marker_line_width = 2, # Changing width of border
opacity=0.7 # Changing opacity of Bars
)
layout = go.Layout(
title = 'Simple Bar Chart',
width=970,
height=650,
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Simple Bar Chart
100
80
60
40
20
0
1 2 3 4 5 6 7 8 9
In [80]: # Simple Horizontal Bar Plot (Using orientation='h')
x = np.arange(1,10)
y = np.arange(20,130,10)
data = go.Bar(
x= y,
y= x,
marker={'color' : '#FFA726'},
orientation='h'
)
layout = go.Layout(
title = 'Simple Bar Chart',
width=970,
height=650,
xaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
yaxis=dict(
showgrid=False, # Hide Gridlines
showline=False, # Hide X-Axis
),
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Simple Bar Chart
0 20 40 60 80 100
In [81]: Assignee = ['Asif', 'Basit', 'John', 'Charles','Vignesh', 'Arun', 'Ashish', 'Varun']
Tickets_Closed = np.array([52,42,60,35,48,45,28,54])
per = (Tickets_Closed/sum(Tickets_Closed))*100
per = np.round(per, decimals=2)
#Create dataframe
helpdesk = pd.DataFrame({'Assignee' : Assignee, 'Tickets Closed' :Tickets_Closed , 'Percentage': per})
helpdesk = helpdesk.sort_values(by='Percentage')
#Update layout
fig.update_layout(
title=dict(text = "Ticket Closure Summary",x=0.46,y=0.95,font_size=20),
yaxis=dict(
showticklabels=True,showgrid=False,showline=False
),
xaxis=dict(
showticklabels=False,
domain=[0, 0.6],
showgrid=False,showline=False
),
margin=dict(l=300, r=20, t=70, b=70),
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',
)
# Labels
for perc, asg in zip(helpdesk.Percentage, helpdesk.Assignee):
fig.update_layout(annotations=annotations)
fig.show()
John 16.48%
Varun 14.84%
Asif 14.29%
Vignesh 13.19%
Arun 12.36%
Basit 11.54%
Charles 9.62%
Ashish 7.69%
trace0 = go.Bar(
x= x1,
y= y1,
marker= dict (color ='#FFA726' )
trace1 = go.Bar(
x= x2,
y= y2,
marker={'color' : '#94E413'}
)
data = [trace0,trace1]
fig = go.Figure(data=data)
fig.show()
40 trace 0
trace 1
35
30
25
20
15
10
0
1 2 3 4 5 6 7 8
Stacked Vertical Bar
In [83]: x = ['Asif','Basit','John','Batista', 'Gabriel' , 'Paul' , 'Ravi']
y1 = [17,18,29,33,38,39,42]
y2 = [20,21,22,23,21,28,25]
y3 = [5,13,11,12,13,11,16]
trace0 = go.Bar(
x= x,
y= y1,
marker= dict (color ='#FF6F00' ),
name = 'Open Tickets'
trace1 = go.Bar(
x= x,
y= y2,
marker={'color' : '#FFB300'},
name = 'Closed Tickets'
)
trace2 = go.Bar(
x= x,
y= y3,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets'
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
barmode = 'stack',
width=900,
height=800
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Cancelled Tickets
Closed Tickets
80 Open Tickets
70
60
50
40
30
20
10
0
Asif Basit John Batista Gabriel Paul Ravi
In [84]: #Displaying values in bar plot using "text" and "textposition" parameter
x = ['Asif','Basit','John','Batista', 'Gabriel' , 'Paul' , 'Ravi']
y1 = [17,18,29,33,38,39,42]
y2 = [20,21,22,23,21,28,25]
y3 = [5,13,11,12,13,11,16]
trace0 = go.Bar(
x= x,
y= y1,
marker= dict (color ='#FF6F00' ),
name = 'Open Tickets',
text=y1,
textposition='auto'
)
trace1 = go.Bar(
x= x,
y= y2,
marker={'color' : '#FFB300'},
name = 'Closed Tickets',
text=y2,
textposition='auto'
)
trace2 = go.Bar(
x= x,
y= y3,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets',
text=y3,
textposition='auto'
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
barmode = 'stack',
width=900,
height=800
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Cancelled Tickets
Closed Tickets
16
80 Open Tickets
11
70 13
12
28 25
60 11
21
23
50 13
22
40 5 42
21 39
38
20
33
30
29
20
18
17
10
0
Asif Basit John Batista Gabriel Paul Ravi
trace0 = go.Bar(
x= y1,
y= x,
marker= dict (color ='#FF6F00'),
name = 'Open Tickets',
orientation='h',
text=y1,
textposition='auto'
)
trace1 = go.Bar(
x= y2,
y= x,
marker={'color' : '#FFB300'},
name = 'Closed Tickets',
orientation='h',
text=y2,
textposition='auto'
)
trace2 = go.Bar(
x= y3,
y= x,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets',
orientation='h',
text=y3,
textposition='auto'
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
barmode = 'stack',
width=990,
height=550
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Cancelled Tickets
Ravi 42 25 16
Closed Tickets
Open Tickets
Paul 39 28 11
Gabriel 38 21 13
Batista 33 23 12
John 29 22 11
Basit 18 21 13
Asif 17 20 5
0 10 20 30 40 50 60 70 80
In [86]: x = ['Asif','Basit','John','Batista', 'Gabriel' , 'Paul' , 'Ravi']
y1 = [17,18,29,33,38,39,42]
y2 = [20,21,22,23,21,28,25]
y3 = [5,13,11,12,13,11,16]
trace0 = go.Bar(
x= y1,
y= x,
marker= dict (color ='#FF9800',line=dict(color='#F4511E',width=2)),
name = 'Open Tickets',
orientation='h',
text=y1,
textposition='auto',
opacity=0.8,
)
trace1 = go.Bar(
x= y2,
y= x,
marker= dict (color ='#7CB342',line=dict(color='#2E7D32', width=2)),
name = 'Closed Tickets',
orientation='h',
text=y2,
textposition='auto',
opacity=0.8,
)
trace2 = go.Bar(
x= y3,
y= x,
marker= dict (color ='#1E88E5',line=dict(color='#3F51B5', width=2)),
name = 'Cancelled Tickets',
orientation='h',
text=y3,
textposition='auto',
opacity=0.7,
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
barmode = 'stack',
width=990,
height=600
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Cancelled Tickets
Ravi 42 25 16 Closed Tickets
Open Tickets
Paul 39 28 11
Gabriel 38 21 13
Batista 33 23 12
John 29 22 11
Basit 18 21 13
Asif 17 20 5
0 10 20 30 40 50 60 70 80
trace1 = go.Bar(
x= x,
y= y2,
marker={'color' : '#FFB300'},
name = 'Closed Tickets'
)
trace2 = go.Bar(
x= x,
y= y3,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets'
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
width=980,
height=800
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Open Tickets
Closed Tickets
Cancelled Tickets
40
35
30
25
20
15
10
0
Asif Basit John Batista Gabriel Paul Ravi
In [88]: # Grouped Bar Chart with values displayed outside the bar (Using textposition='outside')
x = ['Asif','Basit','John','Batista', 'Gabriel' , 'Paul' , 'Ravi']
y1 = [17,18,29,33,38,39,42]
y2 = [20,21,22,23,21,28,25]
y3 = [5,13,11,12,13,11,16]
trace0 = go.Bar(
x= x,
y= y1,
marker= dict (color ='#FF6F00' ),
name = 'Open Tickets',
text=y1,
textposition='outside'
)
trace1 = go.Bar(
x= x,
y= y2,
marker={'color' : '#FFB300'},
name = 'Closed Tickets',
text=y2,
textposition='outside'
)
trace2 = go.Bar(
x= x,
y= y3,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets',
text=y3,
textposition='outside'
)
layout = go.Layout(
title= 'Open Tickets by Status' ,
barmode = 'group',
width=900,
height=800,
xaxis_tickangle=-45
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Open Tickets by Status
Open Tickets
42
Closed Tickets
Cancelled Tickets
40 39
38
35
33
30 29
28
25
25
23
22
21 21
20
20
18
17
16
15
13 13
12
11 11
10
5
5
0
if
hn
ta
ul
vi
rie
si
As
Ra
Pa
tis
Ba
Jo
ab
Ba
G
In [89]: # Grouped Bar Chart with values displayed inside the bar using "auto" textposition
x = ['Asif','Basit','John','Batista', 'Gabriel' , 'Paul' , 'Ravi']
y1 = [17,18,29,33,38,39,42]
y2 = [20,21,22,23,21,28,25]
y3 = [5,13,11,12,13,11,16]
trace0 = go.Bar(
x= x,
y= y1,
marker= dict (color ='#FF6F00' ),
name = 'Open Tickets',
text=y1,
textposition='auto'
)
trace1 = go.Bar(
x= x,
y= y2,
marker={'color' : '#FFB300'},
name = 'Closed Tickets',
text=y2,
textposition='auto'
)
trace2 = go.Bar(
x= x,
y= y3,
marker={'color' : '#F7DC6F'},
name = 'Cancelled Tickets',
text=y3,
textposition='auto'
)
layout = go.Layout(
title=dict(text = "Tickets by Status",x=0.5,y=0.95,font_size=25),
barmode = 'group',
width=980,
height=800,
xaxis_tickangle=-45,
xaxis_tickfont_size=14,
yaxis=dict(
title='Number of Tickets',
titlefont_size=16,
tickfont_size=14,
),
xaxis=dict(
title='Assignee',
titlefont_size=16,
tickfont_size=14,
),
legend=dict(
x=0,
y=1,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.08 # gap between bars of the same location coordinate.
)
data = [trace0,trace1,trace2]
fig = go.Figure(data=data, layout=layout)
fig.show()
Tickets by Status
Open Tickets
Closed Tickets
42
40 Cancelled Tickets
39
38
35
33
30
29
28
Number of Tickets
25
25
23
22
20 21 21
20
18
17
15 16
13 13
12
10 11 11
5
5
0
if
hn
ta
vi
ie
u
si
As
Ra
Pa
tis
r
Ba
Jo
ab
Ba
Assignee
x = np.arange(1,10)
y = np.arange(20,110,10)
fig = px.bar(x=x, y=y)
fig.show()
100
80
60
y
40
20
0
1 2 3 4 5 6 7 8 9
x
In [91]: x = np.arange(1,10)
y = np.arange(20,110,10)
fig = px.bar(x=x, y=y,color=y)
fig.layout.title.text = "Bar Plot - Plotly Express"
fig.update_traces(textposition='outside')
fig.show()
100 color
100
90
80
80
70
60
y
60
40
50
40
20
30
20
0
1 2 3 4 5 6 7 8 9
x
In [92]: x = np.arange(1,10)
y = np.arange(20,110,10)
fig = px.bar(x=x, y=y,color=y,text=y)
fig.layout.title.text = "Bar Plot - Plotly Express"
fig.update_traces(textposition='outside')
fig.show()
100
100 color
100
90
90
80
80
70 80
60 70
60
50
y
60
40
40
50
30
40
20
20
30
20
0
1 2 3 4 5 6 7 8 9
rating = pd.DataFrame(data=review,index=index1,columns=col1)
rating
Out[93]:
Strongly Agree Agree Neutral Disagree Strongly Disagree
Out[94]:
Strongly Agree Agree Neutral Disagree Strongly Disagree Total
Out[95]:
Strongly Agree Agree Neutral Disagree Strongly Disagree Total Strongly Agree (%) Agree (%) Neutral (%) Disagree (%) Strongly Disagree (%)
Python 428 111 70 101 80 790 NaN NaN NaN NaN NaN
Java 370 222 80 104 70 846 NaN NaN NaN NaN NaN
Julia 298 121 90 102 60 671 NaN NaN NaN NaN NaN
C++ 310 141 100 109 56 716 NaN NaN NaN NaN NaN
C 400 121 110 107 78 816 NaN NaN NaN NaN NaN
In [96]: for i in range(0,len(rating['Strongly Agree'])):
k=0
for j in range(int(len(rating.iloc[0])/2)+1 , len(rating.iloc[0])):
rating.iat[i,j] = np.round((rating.iat[i,k] / rating.iat[i,5])*100 ,decimals=2)
k=k+1
rating
Out[96]:
Strongly Agree Agree Neutral Disagree Strongly Disagree Total Strongly Agree (%) Agree (%) Neutral (%) Disagree (%) Strongly Disagree (%)
Python 428 111 70 101 80 790 54.18 14.05 8.86 12.78 10.13
Java 370 222 80 104 70 846 43.74 26.24 9.46 12.29 8.27
Julia 298 121 90 102 60 671 44.41 18.03 13.41 15.20 8.94
C++ 310 141 100 109 56 716 43.30 19.69 13.97 15.22 7.82
C 400 121 110 107 78 816 49.02 14.83 13.48 13.11 9.56
In [100]: fig = go.Figure()
# Layout setting
fig.update_layout(
title=dict(text = "Best Programming Language",x=0.44,y=0.95,font_size=20),
barmode = 'stack',
width=1000,
height=500,
margin=dict(l=70, r=0, t=70, b=70),
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',
)
annotations =[]
fig.update_layout(annotations=annotations)
fig.show()
Best Programming Language
0 20 40 60 80 100
In [99]: #Optimized code for above visualization
fig = go.Figure()
cols = ['#2E7D32' , '#8BC34A' , '#D4E157' , '#FFB300' , '#FF7043']
fig.update_layout(
title=dict(text = "Best Programming Language",x=0.44,y=0.95,font_size=20),
barmode = 'stack',
width=1000,
height=500,
margin=dict(l=70, r=0, t=70, b=70),
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',
)
annotations =[]
i=0
for j in range(1,6):
if j==1:
for perc, lang in zip(rating.iloc[:,5+j],rating.index):
# labeling the bar percentage
annotations.append(dict(xref='x',
yref='y',
y=lang,
x=perc/2 ,
text=str(perc) + ' %',
font=dict(family='Arial', size=12,color='white'),
showarrow=False))
sum1 = rating.iloc[:,5+j]
else:
i=0
for perc, lang in zip(rating.iloc[:,5+j],rating.index):
# labeling the bar percentage
annotations.append(dict(xref='x',
yref='y',
y=lang,
x=perc/2 + sum1[i],
text=str(perc) + ' %',
font=dict(family='Arial', size=12,color='black'),
showarrow=False))
i+=1
sum1 = sum1+ rating.iloc[:,5+j]
fig.update_layout(annotations=annotations)
fig.show()
0 20 40 60 80 100
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20)
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
26.5%
42.5%
17.7%
13.3%
In [400]: #Changing color of Pie Chart
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
26.5%
42.5%
17.7%
13.3%
In [232]: # Displaying Label , Value & percent in Pie Chart using "textinfo" parameter
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
textinfo='label+value+percent'
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
Medium
30
26.5%
Low
48
42.5%
High
20
17.7%
Critical
15
13.3%
In [233]: # Changing label orientation using "insidetextorientation" paramter
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors,line=dict(color='#797D7F', width=1.5)),
textinfo='percent',
hoverinfo='label+value',
textfont_size=15,
insidetextorientation='radial'
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
26.5% 42.5%
%
.7
17
13.3%
In [234]: #Explode 4th Slice using "pull" parameter
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
textinfo='label+value+percent',
pull=[0, 0, 0, 0.2] #Explode 4th Slice
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
Medium
30
26.5%
Low
48
42.5%
High
20
17.7%
Critical
15
13.3%
In [235]: #Explode 3rd & 4th Slice using "pull" parameter
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
textinfo='label+value+percent',
pull=[0, 0, 0.2, 0.2]
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
Medium
30
26.5%
Low
48
42.5%
High
20
17.7%
Critical
15
13.3%
In [236]: # Simple Donut Chart
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
textinfo='label+value+percent',
hole=.4
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
Medium
30
26.5%
Low
48
42.5%
High
20
17.7%
Critical
15
13.3%
In [237]: #Explode 4th Slice using "pull" parameter
tickets = [48 , 30 , 20 , 15]
status = ['Low' , 'Medium' , 'High' , 'Critical']
colors = ['#8BC34A','#D4E157','#FFB300','#FF7043']
data = go.Pie(
values= tickets,
labels= status,
marker=dict(colors=colors),
textinfo='label+value+percent',
hole=.4,
pull=[0, 0, 0, 0.2]
)
layout = go.Layout(
title=dict(text = "Tickets by Priority",x=0.46,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Tickets by Priority
Low
Medium
High
Critical
Medium
30
26.5%
Low
48
42.5%
High
20
17.7%
Critical
15
13.3%
In [401]: employment.head()
Out[401]:
Age Gender Period Unemployed
fig = go.Figure(data=data,layout=layout)
fig.show()
Unemployment Data
25 to 34 years
45 to 54 years
35 to 44 years
55 to 64 years
20 to 24 years
65 years and over
20.6% 21.5%
16 to 19 years
5.9%
18.7%
6.52%
13%
13.8%
In [403]: suicide.head()
Out[403]:
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
In [404]: data = go.Pie(
values= suicide['suicides_no'],
labels= suicide['age'],
textinfo='percent',
hoverinfo='label+value'
)
layout = go.Layout(
title=dict(text = "Suicide Data",x=0.44,y=0.95,font_size=20),
width=800,
height=650
)
fig = go.Figure(data=data,layout=layout)
fig.show()
Suicide Data
35-54 years
55-74 years
25-34 years
15-24 years
75+ years
5-14 years
24.6%
36.3%
16.7%
9.68% 0.774%
12%
In [242]: # Display multiple Pie plots in one figure using Subplots
#Subplot initialization
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Tickets by Priority", "Tickets by Assignee"),
specs=[[{'type':'domain'}, {'type':'domain'}]]
)
fig.add_trace(go.Pie(
values= Open,
labels= Assignee,
marker=dict(colors=colors),
textinfo='label+value+percent'
),
row=1, col=2
)
fig.update_layout(
paper_bgcolor= '#FFFDE7',
plot_bgcolor= '#FFFDE7',
title=dict(text = "Help Desk",x=0.5,y=0.95),
title_font_size=30
)
fig.show()
Help Desk
Tickets by Priority Tickets by Assignee
Low
Medium
High
Paul Ravi Critical
39 42
Medium 18.1% Ravi
19.4%
30 Paul
26.5%
Gabriel
Low
Asif Batista
48
17
42.5% Gabriel John
7.87%
38 Basit
17.6% Asif
Basit
High 18
20 8.33%
17.7%
Batista John
Critical 33 29
15 15.3% 13.4%
13.3%
In [243]: # Display multiple Donut charts in one figure using Subplots
#Subplot initialization
fig = make_subplots(
rows=1,
cols=2,
subplot_titles=("Tickets by Priority", "Tickets by Assignee"),
specs=[[{'type':'domain'}, {'type':'domain'}]]
)
fig.add_trace(go.Pie(
values= Open,
labels= Assignee,
hole = .4,
marker=dict(colors=colors),
textinfo='label+value+percent',
hoverinfo='label'
),
row=1, col=2
)
fig.update_layout(
paper_bgcolor= '#FFFDE7',
plot_bgcolor= '#FFFDE7',
title=dict(text = "Help Desk",x=0.5,y=0.95),
title_font_size=30
)
fig.show()
Help Desk
Tickets by Priority Tickets by Assignee
Low
Medium
Paul Ravi High
Medium 39 42 Critical
30 18.1% 19.4% Ravi
26.5% Paul
Low Gabriel
48 Asif Batista
42.5% 17
Gabriel John
7.87%
38 Basit
17.6% Asif
Basit
High 18
20 8.33%
17.7% Batista John
Critical 33 29
15 15.3% 13.4%
13.3%
In [407]: # Display multiple Pie & Donut plots in one figure using Subplots
#Subplot initialization
fig = make_subplots(
rows=2,
cols=2,
subplot_titles=("Tickets by Priority", "Tickets by Severity",
"Tickets by Group", "Tickets by Status"),
specs=[[{'type':'domain'}, {'type':'domain'}],[{'type':'domain'}, {'type':'domain'}]]
)
fig.update_layout(
paper_bgcolor= '#FFFDE7',
plot_bgcolor= '#FFFDE7',
title=dict(text = "Help Desk",x=0.49,y=0.97,font_size=30),
width=950,
height=900,
showlegend=False
)
fig.show()
Help Desk
Tickets by Priority Tickets by Severity
Medium
sev-2
30 sev-1
29
26.5% Low 33
29.9%
48 34%
42.5%
High
20 Sev-3
17.7% 18 Sev-4
Critical 18.6% 17
15 17.5%
13.3%
Problem
New
In
30
Pro
29
Serv
26.5%
29.9%
33
gr
34
42.5
es
ice D
%
48
s
%
esk
Application
20 Pending
17.7% 18 Assigned
Change 17
18.6%
15 17.5%
13.3%
Out[408]:
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
In [409]: fig = px.pie(suicide, values='suicides_no', names='generation', title='Country')
fig.update_layout(
title=dict(text = "Suicide Data",x=0.44,y=0.95,font_size=20),
width=800,
height=650
)
fig.update_traces(textposition='outside', textinfo='percent+label')
fig.show()
Suicide Data
Boomers
Silent
Generation X
Silent
26.4% Millenials
G.I. Generation
Boomers Generation Z
33.9%
Generation Z
0.236%
Millenials
9.24%
Bubble Chart
In [101]: #Simple Bubble Chart
x = np.arange(1,10)
y = np.random.randint(1,100,9)
op = np.random.uniform(0.2,1 ,9)
data = go.Scatter(
x = x,
y = y,
mode = 'markers',
marker = dict(size = y),
)
fig = go.Figure(data=data)
fig.show()
80
70
60
50
40
30
20
10
1 2 3 4 5 6 7 8 9
In [102]: x = np.arange(1,10)
y = np.random.randint(1,100,9)
op = np.random.uniform(0.2,1 ,9)
data = go.Scatter(
x = x,
y = y,
mode = 'markers',
marker = dict(size = y,opacity = op), # Changing opacity & size of bubbles
)
fig = go.Figure(data=data)
fig.show()
100
80
60
40
20
0
1 2 3 4 5 6 7 8 9
In [104]: x = np.arange(1,10)
y = np.random.randint(1,100,9)
op = np.random.uniform(0.2,1 ,9)
hexval = [hex(x) for x in np.random.randint(0,16777215,10)]
hexval = ['#' + hexval[i][2:] for i in range(0,10)] #Generate Hex color list
data = go.Scatter(
x = x,
y = y,
mode = 'markers',
marker = dict(size = y,color = hexval) # Changing color & size of bubbles
)
fig = go.Figure(data=data)
fig.show()
110
100
90
80
70
60
50
40
30
0 2 4 6 8 10
In [105]: suicide.head()
Out[105]:
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
In [106]: # Bubble Chart using plotly.express
fig = px.scatter(
suicide.query("year==1987"),
x="population",
y="suicides_no",
size="suicides_no",
color="age",
hover_name="country",
size_max=60
)
fig.show()
8000 age
15-24 years
7000 35-54 years
75+ years
25-34 years
6000
55-74 years
5-14 years
5000
suicides_no
4000
3000
2000
1000
population
Area Plot
In [108]: # Simple Area plot
x = np.arange(1,31)
y = np.random.normal(10,11,size=30)
y = np.square(y)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, fill='tozeroy')) # fill down to xaxis
fig.show()
600
500
400
300
200
100
0
5 10 15 20 25 30
In [110]: #Changing color of area plot using marker color
x = np.arange(1,31)
y = np.random.normal(10,11,size=30)
y = np.square(y)
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, fill='tozeroy',marker = dict(color = 'gold'))) # fill down to xaxis
fig.show()
1200
1000
800
600
400
200
0
5 10 15 20 25 30
In [111]: x=np.arange(1,7)
y1 = np.array([1,5,9,3,17,1])
y2 = np.array([2,6,12,4,19,2])
y3 = np.array([3,7,16,5,22,1])
y4 = np.array([4,8,18,6,25,2])
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y1, fill='tozeroy',marker = dict(color = '#00b159'))) # fill down to xaxis
fig.add_trace(go.Scatter(x=x, y=y2, fill='tonexty',marker = dict(color = '#ffc425'))) # fill to trace0 y
fig.add_trace(go.Scatter(x=x, y=y3, fill='tonexty',marker = dict(color = '#3498DB'))) # fill to trace1 y
fig.add_trace(go.Scatter(x=x, y=y4, fill='tonexty',marker = dict(color = '#ff3b30'))) # fill to trace2 y
fig.update_layout(width = 980 , height = 600)
fig.show()
trace 3
25
trace 2
trace 1
trace 0
20
15
10
0
1 2 3 4 5 6
In [112]: #Stacked Area Chart (Using stackgroup parameter)
x=np.arange(1,7)
y1 = np.array([1,5,9,3,17,1])
y2 = np.array([2,6,12,4,19,2])
y3 = np.array([3,7,16,5,22,1])
y4 = np.array([4,8,18,6,25,2])
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y1,
marker = dict(color = '#00b159'),
stackgroup='one' # The stackgroup parameter is used to create a Stacked Area Chart
)
)
fig.add_trace(go.Scatter(
x=x,
y=y2,
marker = dict(color = '#ffc425'),
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y3,
marker = dict(color = '#3498DB'),
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y4,
marker = dict(color = '#ff3b30'),
stackgroup='one'
)
)
fig.show()
trace 3
80 trace 2
trace 1
trace 0
70
60
50
40
30
20
10
0
1 2 3 4 5 6
In [113]: #Stacked Area Chart
x=['A','B','C','D','E']
y1 = np.array([30,10,40,20,60])
y2 = np.array([10,20,10,20,10])
y3 = np.array([5,10,5,10,5])
y4 = np.array([10,5,10,5,10])
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y1,
marker = dict(color = '#00b159'), # Color of trace0
stackgroup='one' # The stackgroup parameter is used to create a Stacked Area Chart
)
)
fig.add_trace(go.Scatter(
x=x,
y=y2,
marker = dict(color = '#ffc425'),
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y3,
marker = dict(color = '#3498DB'),
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y4,
marker = dict(color = '#ff3b30'),
stackgroup='one'
)
)
fig.show()
trace 3
trace 2
80
trace 1
trace 0
70
60
50
40
30
20
10
0
A B C D E
In [114]: #Stacked Area Chart Without Boundary Lines (Using mode = 'none')
x=['A','B','C','D','E']
y1 = np.array([30,10,40,20,60])
y2 = np.array([10,20,10,20,10])
y3 = np.array([5,10,5,10,5])
y4 = np.array([10,5,10,5,10])
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y1,
mode='none',
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y2,
mode='none',
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y3,
mode='none',
stackgroup='one'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y4,
mode='none',
stackgroup='one'
)
)
fig.show()
trace 3
trace 2
80
trace 1
trace 0
70
60
50
40
30
20
10
0
A B C D E
In [115]: #100 Percent Stacked Area Chart (Using groupnorm parameter)
x=['A','B','C','D','E']
y1 = np.array([30,10,40,20,60])
y2 = np.array([10,20,10,20,10])
y3 = np.array([5,10,5,10,5])
y4 = np.array([10,5,10,5,10])
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y1,
marker = dict(color = '#00b159'),
stackgroup='one',
groupnorm='percent' #normalization for the sum of the stackgroup
)
)
fig.add_trace(go.Scatter(
x=x,
y=y2,
marker = dict(color = '#ffc425'),
stackgroup='one',
groupnorm='percent'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y3,
marker = dict(color = '#3498DB'),
stackgroup='one',
groupnorm='percent'
)
)
fig.add_trace(go.Scatter(
x=x,
y=y4,
marker = dict(color = '#ff3b30'),
stackgroup='one',
groupnorm='percent'
)
)
fig.update_layout(
width = 990,
height = 650,
xaxis_type='category',
yaxis=dict(
range=[1, 100],
ticksuffix='%'
)
)
fig.show()
100%
trace 3
trace 2
90% trace 1
trace 0
80%
70%
60%
50%
40%
30%
20%
10%
A B C D E
In [116]: # Area Chart with interioir filling using fill='tonexty'
x=['A','B','C','D','E']
y1 = np.array([30,10,40,20,60])
y2 = np.array([10,20,10,20,10])
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x,
y=y1,
marker = dict(color = '#00b159'),
fill = None
)
)
fig.add_trace(go.Scatter(
x=x,
y=y2,
fill='tonexty', # fill to trace0 y
marker = dict(color = '#ffc425'),
)
)
fig.show()
trace 1
60
trace 0
50
40
30
20
10
A B C D E
Out[117]:
Strongly Agree Agree Neutral Disagree Strongly Disagree Total Strongly Agree (%) Agree (%) Neutral (%) Disagree (%) Strongly Disagree (%)
Python 428 111 70 101 80 790 54.18 14.05 8.86 12.78 10.13
Java 370 222 80 104 70 846 43.74 26.24 9.46 12.29 8.27
Julia 298 121 90 102 60 671 44.41 18.03 13.41 15.20 8.94
C++ 310 141 100 109 56 716 43.30 19.69 13.97 15.22 7.82
C 400 121 110 107 78 816 49.02 14.83 13.48 13.11 9.56
In [118]: #Basic table in Plotly
fig = go.Figure(data=[go.Table(
header=dict(
values=list(rating.columns),
),
cells=dict(values=[
rating['Strongly Agree'] ,
rating['Agree'] ,
rating['Neutral'] ,
rating['Disagree'] ,
rating['Strongly Disagree'],
rating['Total'],
rating['Strongly Agree (%)'],
rating['Agree (%)'],
rating['Neutral (%)'],
rating['Disagree (%)'],
rating['Strongly Disagree (%)']
],
)
)
]
)
fig.show()
Strongly Agree Neutral Disagree Strongly Total Strongly Agree Neutral Disagree Strongly
Agree Disagree Agree (%) (%) (%) Disagree
(%) (%)
Strongly Agree Neutral Disagree Strongly Total Strongly Agree Neutral Disagree Strongly
Agree Disagree Agree (%) (%) (%) Disagree
(%) (%)
fig = go.Figure(data=[go.Table(
header=dict(
values=list(insurance.columns), # Header values
line_color='black', # Line Color of header
fill_color='orange', # background color of header
align='center', # Align header at center
height=40, # Height of Header
font=dict(color='white', size=18), # Font size & color of header text
),
cells=dict(values=[
insurance.age , # Column values
insurance.sex,
insurance.bmi,
insurance.children,
insurance.smoker,
insurance.region,
insurance.charges
],
line_color='darkgrey', # Line color of the cell
fill_color='lightcyan', # Color of the cell
align='left' # Align text to left in cell
)
)
]
)
fig.show()
age sex bmi children smoker region charges
19 female 27.9 0 yes southwest 16884.924
18 male 33.77 1 no southeast 1725.5523
28 male 33 3 no southeast 4449.462
33 male 22.705 0 no northwest 21984.47061
32 male 28.88 0 no northwest 3866.8552
31 female 25.74 0 no southeast 3756.6216
46 female 33.44 1 no southeast 8240.5896
37 female 27.74 3 no northwest 7281.5056
37 male 29.83 2 no northeast 6406.4107
60 female 25.84 0 no northwest 28923.136919999
25 male 26.22 0 no northeast 2721.3208
62 female 26.29 0 yes southeast 27808.7251
23 male 34.4 0 no southwest 1826.8429999999
56 female 39.82 0 no southeast 11090.7178
27 male 42.13 0 yes southeast 39611.7577
In [143]: # Styled Table in Plotly
rowEvenColor = 'lightgrey'
rowOddColor = 'white'
fig = go.Figure(data=[go.Table( columnwidth = [80,80,80,80,80,80,120,80,80,80,120],
header=dict(
values=[
'<b>Strongly Agree</b>',
'<b>Agree</b>',
'<b>Neutral</b>',
'<b>Disagree</b>',
'<b>Strongly Disagree</b>',
'<b>Total</b>',
'<b>Strongly Agree (%)</b>',
'<b>Agree (%)</b>',
'<b>Neutral (%)</b>',
'<b>Disagree (%)</b>',
'<b>Strongly Disagree (%)</b>'
],
fill_color='#8BC34A',
line = dict(color = '#689F38' , width = 4),
align='center',
font_size=12,
font_color = 'white'
),
cells=dict(values=[
rating['Strongly Agree'] ,
rating['Agree'] ,
rating['Neutral'] ,
rating['Disagree'] ,
rating['Strongly Disagree'],
rating['Total'],
rating['Strongly Agree (%)'],
rating['Agree (%)'],
rating['Neutral (%)'],
rating['Disagree (%)'],
rating['Strongly Disagree (%)']
],
fill_color = [[rowOddColor,rowEvenColor]*5],
line = dict(color = 'lightgreen' , width = 4),
align ='center',
font_size=12,
font = dict(color = 'darkslategray', size = 11),
height=40
)
)
]
)
fig.update_layout(width=990, height=500)
fig.show()
Strongly Agree Neutral Disagree Strongly Total Strongly Agree Neutral Disagree Strongly
Agree Disagree Agree (%) (%) (%) (%) Disagree
(%)
310 141 100 109 56 716 43.3 19.69 13.97 15.22 7.82
400 121 110 107 78 816 49.02 14.83 13.48 13.11 9.56
fig = ff.create_table(insurance.tail(5))
fig.show()
fig = ff.create_table(insurance.tail(5),height_constant=50)
fig.show()
Out[52]:
India Pakistan China
import plotly.graph_objs as go
import plotly.figure_factory as ff
fig.update_layout(
title=dict(text = "Immigration Data",x=0.5,y=0.98), # Figure title along with Alignment values
paper_bgcolor= '#dbdbdb', # Figure background
margin = {'t':50, 'b':100},
xaxis = {'domain': [0, .5] , 'title' : 'Migrants'},
xaxis2 = {'domain': [0.6, 1.] , 'title' : 'Year'},
yaxis2 = {'anchor': 'x2', 'title': 'Count'},
width = 990,
height = 600
)
China
India Pakistan China India
40k Pakistan
30k
8670 972 6682
25k
Count
8147 1201 3308
20k
10k
5704 668 1527
5k
import plotly.graph_objs as go
import plotly.figure_factory as ff
fig.add_trace( go.Scatter(
x = canada.index.values,
y = canada['India'],
mode = 'lines',
name = 'India',
xaxis='x2', yaxis='y2'
)
)
fig.add_trace( go.Scatter(
x = canada.index.values,
y = canada['Pakistan'],
mode = 'lines',
name = 'Pakistan',
xaxis='x2', yaxis='y2'
)
)
fig.update_layout(
title_text = '2016 Hockey Stats',
height = 800,
margin = {'t':50, 'l':20},
yaxis = {'domain': [0, .3]},
yaxis2 = {'domain': [.4, 1], 'anchor': 'x2', 'title': 'Goals'},
xaxis2 = {'anchor': 'y2'},
)
fig.show()
2016 Hockey Stats
China
India
40k Pakistan
35k
30k
25k
Goals
20k
15k
10k
5k
Plotly3
Viridis
Cividis
Inferno
Magma
Plasma
Blackbody
Bluered
Electric
Hot
Jet
Rainbow
Blues
BuGn
BuPu
GnBu
Greens
Greys
OrRd
Oranges
PuBu
PuBuGn
PuRd
Purples
RdBu
RdPu
Reds
YlGn
YlGnBu
YlOrBr
YlOrRd
turbid
thermal
haline
solar
ice
gray
deep
dense
algae
matter
speed
amp
tempo
Burg
Burgyl
Redor
Oryel
Peach
Pinkyl
Mint
Blugrn
Darkmint
Emrld
Aggrnyl
Bluyl
Teal
Tealgrn
Purp
Purpor
Sunset
Magenta
Sunsetdark
Agsunset
Brwnyl
In [313]: #Cyclical Color scales
fig = px.colors.cyclical.swatches_cyclical()
fig.show()
plotly.colors.cyclical
plotly.colors.cyclical
Twilight
IceFire
Edge
Phase
HSV
mrybm
mygbm
In [315]: #Diverging Color scales
fig = px.colors.diverging.swatches().update_layout(margin_b=10)
fig.show()
plotly.colors.diverging
BrBG
PRGn
PiYG
PuOr
RdBu
RdGy
RdYlBu
RdYlGn
Spectral
balance
delta
curl
Armyrose
Fall
Geyser
Temps
Tealrose
Tropic
Earth
Picnic
Portland
In [318]: #Qualitative Color scales
fig = px.colors.qualitative.swatches()
fig.show()
plotly.colors.qualitative
Plotly
D3
G10
T10
Alphabet
Dark24
Light24
Set1
Pastel1
Dark2
Set2
Pastel2
Set3
Antique
Bold
Pastel
Prism
Safe
Vivid
Sunburst Chart
In [273]: insurance = pd.read_csv('C:/Users/DELL/Documents/GitHub/Data-Visualization/insurance.csv')
insurance.head(10)
Out[273]:
age sex bmi children smoker region charges
male
female
female
female
no
male
no
male
yes
male southeast
northeast
yes
female
female southwest
northwest yes
no
male
female
no
yes
male
male
female
male
female
In [56]: fig = px.sunburst(insurance, path=['region', 'smoker' , 'sex'], values='charges' , color= 'charges')
fig.update_layout (height = 800 , width = 800)
fig.show()
charges
male 5M
female
female
female
4.5M
no
male
4M
no
male
yes
male 3.5M
southeast
northeast
yes
3M
female
female southwest
northwest yes
2.5M
no
male
female
no
2M
yes
male
male 1.5M
female
male
female
1M
In [308]: # Using inbuilt color scales in Sunburst Chart
fig = px.sunburst(
insurance,
path=['region', 'smoker' , 'sex'],
values='charges' , color= 'charges' ,
color_continuous_scale=px.colors.sequential.Aggrnyl
)
charges
male 5M
female
female
female
4.5M
no
male
4M
no
male
yes
male 3.5M
southeast
northeast
yes
3M
female
female southwest
northwest yes
2.5M
no
male
female
no
2M
yes
male
male 1.5M
female
male
female
1M
In [295]: # Using color scales in Sunburst Chart
fig = px.sunburst(
insurance,
path=['region', 'smoker' , 'sex'],
values='charges' ,
color= 'charges' ,
color_continuous_scale=["#8BC34A", "#FF6F00"] #Explicitly Constructing a Color Sequence
)
charges
male 5M
female
female
female
4.5M
no
male
4M
no
male
yes
male 3.5M
southeast
northeast
yes
3M
female
female southwest
northwest yes
2.5M
no
male
female
no
2M
yes
male
male 1.5M
female
male
female
1M
In [62]: # Using color scales in Sunburst Chart
fig = px.sunburst(
insurance,
path=['region', 'smoker' , 'sex'],
values='charges' , color= 'charges' ,
color_continuous_scale=["#689F38","#F9A825", "#FF6F00"] #Explicitly Constructing a Color Sequence
)
charges
male 5M
female
female
female
4.5M
no
male
4M
no
yes male
male 3.5M
southeast
northeast
yes
3M
female
female southwest
northwest yes
2.5M
no
male
female
no
2M
yes
male
male 1.5M
female
male
female 1M
In [65]: suicide = pd.read_csv("suicide.csv")
suicide.head(10)
Out[65]:
country year sex age suicides_no population suicides/100k pop country-year HDI for year gdp_for_year ($) gdp_per_capita ($) generation
0 Albania 1987 male 15-24 years 21 312900 6.71 Albania1987 NaN 2,156,624,900 796 Generation X
1 Albania 1987 male 35-54 years 16 308000 5.19 Albania1987 NaN 2,156,624,900 796 Silent
2 Albania 1987 female 15-24 years 14 289700 4.83 Albania1987 NaN 2,156,624,900 796 Generation X
3 Albania 1987 male 75+ years 1 21800 4.59 Albania1987 NaN 2,156,624,900 796 G.I. Generation
4 Albania 1987 male 25-34 years 9 274300 3.28 Albania1987 NaN 2,156,624,900 796 Boomers
5 Albania 1987 female 75+ years 1 35600 2.81 Albania1987 NaN 2,156,624,900 796 G.I. Generation
6 Albania 1987 female 35-54 years 6 278800 2.15 Albania1987 NaN 2,156,624,900 796 Silent
7 Albania 1987 female 25-34 years 4 257200 1.56 Albania1987 NaN 2,156,624,900 796 Boomers
8 Albania 1987 male 55-74 years 1 137500 0.73 Albania1987 NaN 2,156,624,900 796 G.I. Generation
9 Albania 1987 female 5-14 years 0 311000 0.00 Albania1987 NaN 2,156,624,900 796 Generation X
In [66]: fig = px.sunburst(suicide, path=['sex', 'age' , 'generation'], values='suicides_no')
fig.update_layout (height = 900 , width = 900)
fig.show()
Silent
Generation X
Silent
Boomers
35-54 years
Boomers
55-74 years
G.I.
Gen
erati
on
e
al
m
Generation X
Millenials
Silent
Ge
n
G.I. Generation
era
tio
Si
Generation X
len
nX
Boomers
t
Silent
Millenials
Generation
Generatio
nX
Z
In [67]: fig = px.sunburst(
suicide, path=['sex', 'age' , 'generation'],
values='suicides_no' ,
color= 'suicides_no' ,
color_continuous_scale=["#8BC34A","#FF6F00"]
)
5M
Silent
Generation X
Silent
4M
Boomers
35-54 years
Boomers
55-74 years
G.I.
Gen
erati
on
3M
e
al
m
5-14 years Generation Z
Millenials
Millenials
Silent 1M
Ge
ne
G.I. Generation
rat
Si
Generation X
ion
len
Boomers
X
Silent
Millenials
Generation
Generation
Z
X
Sankey Diagram
In [123]: #Simple Sankey Diagram
fig = go.Figure(
go.Sankey(
node = {
"label": ["India", "USA", "China", "Pakistan", "Bangladesh", "Mexico"],
},
link = {
"source": [0, 1, 2, 3, 4, 0, 2, 5],
"target": [1, 2, 3, 4, 5, 3, 5, 3],
"value": [300, 400, 200, 450, 700, 200,150, 200]
}
)
)
fig.show()
Mexico
India USA
China
Bangladesh
Pakistan
In [124]: #Simple Sankey Diagram
fig = go.Figure(
go.Sankey(
node = dict(
thickness = 40, # Changing thickness of nodes
color = "lightgreen", # Changing color of the node
line = dict(color = "red", width = 0.5), # Changing line color
label = ["India", "USA", "China", "Pakistan", "Bangladesh", "Mexico"],
),
link = {
"source": [0, 1, 2, 3, 4, 0, 2, 5],
"target": [1, 2, 3, 4, 5, 3, 5, 3],
"value": [300, 400, 200, 450, 550, 200,150, 200]
}
)
)
fig.show()
Mexico
USA
India
China
Bangladesh
Pakistan
In [125]: #Simple Sankey Diagram
fig = go.Figure(
go.Sankey(
node = {
"label": ["Married: NO", "Married: Yes",
"Pet: No", "Pet: Yes",
"Happy: Yes", "Happy: No"],
"color" : px.colors.qualitative.Set3 # Node color
},
link = dict(
source = [0, 0, 1, 1, 2, 2, 3, 5],
target = [2, 3, 2, 3, 5, 4, 4, 3],
value = [200, 300, 400, 600, 150, 350,700],
color = px.colors.qualitative.Set2 # Color of links
)
)
)
fig.show()
Pet: Yes
Married: Yes
Happy: Yes
Pet: No
Married: NO
Happy: No
In [ ]:
In [ ]:
END