@@ -55,47 +55,59 @@ countries = (
5555 .unique()
5656)
5757
58- data = {" x " : [], " y " : [], " colors" : [], " years" : []}
58+ data = {" line_x " : [], " line_y " : [], " 1952 " : [], " 2002 " : [], " colors" : [], " years" : [], " countries " : []}
5959
6060for country in countries:
61- data[" x" ].extend(
61+ data[" 1952" ].extend([df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ]])
62+ data[" 2002" ].extend([df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ]])
63+ data[" line_x" ].extend(
6264 [
6365 df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ],
6466 df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ],
6567 None ,
6668 ]
6769 )
68- data[" y" ].extend([country, country, None ]),
69- data[" colors" ].extend([" green" , " blue" , " brown" ]),
70- data[" years" ].extend([" 1952" , " 2002" , None ])
70+ data[" line_y" ].extend([country, country, None ]),
7171
7272fig = go.Figure(
7373 data = [
7474 go.Scatter(
75- x = data[" x " ],
76- y = data[" y " ],
75+ x = data[" line_x " ],
76+ y = data[" line_y " ],
7777 mode = " lines" ,
78+ showlegend = False ,
7879 marker = dict (
79- color = " grey" ,
80- ),
80+ color = " grey"
81+ )
8182 ),
8283 go.Scatter(
83- x = data[" x" ],
84- y = data[" y" ],
85- mode = " markers+text" ,
84+ x = data[" 1952" ],
85+ y = countries,
86+ mode = " markers" ,
87+ name = " 1952" ,
8688 marker = dict (
87- color = data[" colors" ],
88- size = 10 ,
89- ),
90- hovertemplate = """ Country: %{y} <br> Life Expectancy: %{x} <br><extra></extra>""" ,
89+ color = " green" ,
90+ size = 10
91+ )
92+
93+ ),
94+ go.Scatter(
95+ x = data[" 2002" ],
96+ y = countries,
97+ mode = " markers" ,
98+ name = " 2002" ,
99+ marker = dict (
100+ color = " blue" ,
101+ size = 10
102+ )
91103 ),
92104 ]
93105)
94106
95107fig.update_layout(
96108 title = " Life Expectancy in Europe: 1952 and 2002" ,
97109 height = 1000 ,
98- showlegend = False ,
110+ legend_itemclick = False
99111)
100112
101113fig.show()
@@ -124,48 +136,62 @@ countries = (
124136 .unique()
125137)
126138
127- data = {" x " : [], " y " : [], " colors" : [], " years" : []}
139+ data = {" line_x " : [], " line_y " : [], " 1952 " : [], " 2002 " : [], " colors" : [], " years" : [], " countries " : []}
128140
129141for country in countries:
130- data[" x" ].extend(
142+ data[" 1952" ].extend([df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ]])
143+ data[" 2002" ].extend([df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ]])
144+ data[" line_x" ].extend(
131145 [
132146 df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ],
133147 df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ],
134148 None ,
135149 ]
136150 )
137- data[" y" ].extend([country, country, None ]),
138- data[" colors" ].extend([" silver" , " lightskyblue" , " white" ]),
139- data[" years" ].extend([" 1952" , " 2002" , None ])
151+ data[" line_y" ].extend([country, country, None ]),
140152
141153fig = go.Figure(
142154 data = [
143155 go.Scatter(
144- x = data[" x " ],
145- y = data[" y " ],
156+ x = data[" line_x " ],
157+ y = data[" line_y " ],
146158 mode = " markers+lines" ,
159+ showlegend = False ,
147160 marker = dict (
148- symbol = " arrow" , color = " black" , size = 16 , angleref = " previous" , standoff = 8
149- ),
161+ symbol = " arrow" ,
162+ color = " black" ,
163+ size = 16 ,
164+ angleref = " previous" ,
165+ standoff = 8
166+ )
167+ ),
168+ go.Scatter(
169+ x = data[" 1952" ],
170+ y = countries,
171+ name = " 1952" ,
172+ mode = " markers" ,
173+ marker = dict (
174+ color = " silver" ,
175+ size = 16 ,
176+ )
150177 ),
151178 go.Scatter(
152- x = data[" x " ],
153- y = data[ " y " ] ,
154- text = data[ " years " ] ,
179+ x = data[" 2002 " ],
180+ y = countries ,
181+ name = " 2002 " ,
155182 mode = " markers" ,
156183 marker = dict (
157- color = data[ " colors " ] ,
184+ color = " lightskyblue " ,
158185 size = 16 ,
159186 ),
160- hovertemplate = """ Country: %{y} <br> Life Expectancy: %{x} <br> Year: %{text} <br><extra></extra>""" ,
161187 ),
162188 ]
163189)
164190
165191fig.update_layout(
166192 title = " Life Expectancy in Europe: 1952 and 2002" ,
167193 height = 1000 ,
168- showlegend = False ,
194+ legend_itemclick = False
169195)
170196
171197
0 commit comments