@@ -55,47 +55,59 @@ countries = (
55
55
.unique()
56
56
)
57
57
58
- data = {" x " : [], " y " : [], " colors" : [], " years" : []}
58
+ data = {" line_x " : [], " line_y " : [], " 1952 " : [], " 2002 " : [], " colors" : [], " years" : [], " countries " : []}
59
59
60
60
for 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(
62
64
[
63
65
df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ],
64
66
df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ],
65
67
None ,
66
68
]
67
69
)
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 ]),
71
71
72
72
fig = go.Figure(
73
73
data = [
74
74
go.Scatter(
75
- x = data[" x " ],
76
- y = data[" y " ],
75
+ x = data[" line_x " ],
76
+ y = data[" line_y " ],
77
77
mode = " lines" ,
78
+ showlegend = False ,
78
79
marker = dict (
79
- color = " grey" ,
80
- ),
80
+ color = ' grey'
81
+ )
81
82
),
82
83
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" ,
86
88
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
+ )
91
103
),
92
104
]
93
105
)
94
106
95
107
fig.update_layout(
96
108
title = " Life Expectancy in Europe: 1952 and 2002" ,
97
109
height = 1000 ,
98
- showlegend = False ,
110
+ legend_itemclick = False
99
111
)
100
112
101
113
fig.show()
@@ -123,49 +135,64 @@ countries = (
123
135
.sort_values(by = [" lifeExp" ], ascending = True )[" country" ]
124
136
.unique()
125
137
)
126
-
127
- data = {" x " : [], " y " : [], " colors" : [], " years" : []}
138
+
139
+ data = {" line_x " : [], " line_y " : [], " 1952 " : [], " 2002 " : [], " colors" : [], " years" : [], " countries " : []}
128
140
129
141
for 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(
131
145
[
132
146
df.loc[(df.year == 1952 ) & (df.country == country)][" lifeExp" ].values[0 ],
133
147
df.loc[(df.year == 2002 ) & (df.country == country)][" lifeExp" ].values[0 ],
134
148
None ,
135
149
]
136
150
)
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 ]),
152
+
140
153
141
154
fig = go.Figure(
142
155
data = [
143
156
go.Scatter(
144
- x = data[" x " ],
145
- y = data[" y " ],
157
+ x = data[" line_x " ],
158
+ y = data[" line_y " ],
146
159
mode = " markers+lines" ,
160
+ showlegend = False ,
147
161
marker = dict (
148
- symbol = " arrow" , color = " black" , size = 16 , angleref = " previous" , standoff = 8
149
- ),
162
+ symbol = " arrow" ,
163
+ color = " black" ,
164
+ size = 16 ,
165
+ angleref = " previous" ,
166
+ standoff = 8
167
+ )
168
+ ),
169
+ go.Scatter(
170
+ x = data[" 1952" ],
171
+ y = countries,
172
+ name = " 1952" ,
173
+ mode = " markers" ,
174
+ marker = dict (
175
+ color = " silver" ,
176
+ size = 16 ,
177
+ )
150
178
),
151
179
go.Scatter(
152
- x = data[" x " ],
153
- y = data[ " y " ] ,
154
- text = data[ " years " ] ,
180
+ x = data[" 2002 " ],
181
+ y = countries ,
182
+ name = " 2002 " ,
155
183
mode = " markers" ,
156
184
marker = dict (
157
- color = data[ " colors " ] ,
185
+ color = " lightskyblue " ,
158
186
size = 16 ,
159
187
),
160
- hovertemplate = """ Country: %{y} <br> Life Expectancy: %{x} <br> Year: %{text} <br><extra></extra>""" ,
161
188
),
162
189
]
163
190
)
164
191
165
192
fig.update_layout(
166
193
title = " Life Expectancy in Europe: 1952 and 2002" ,
167
194
height = 1000 ,
168
- showlegend = False ,
195
+ legend_itemclick = False
169
196
)
170
197
171
198
0 commit comments