Debian Codegraph @

examples/Marks/Object Model/Market Map.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import pandas as pd\n",
10 "from ipywidgets import Label, VBox, Layout\n",
11 "from bqplot.market_map import MarketMap\n",
12 "from bqplot import (ColorScale, ColorAxis, DateScale, \n",
13 " LinearScale, Axis, Lines, Figure)"
14 ]
15 },
16 {
17 "cell_type": "markdown",
18 "metadata": {},
19 "source": [
20 "## Get Data"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": null,
26 "metadata": {},
27 "outputs": [],
28 "source": [
29 "data = pd.read_csv('../../data_files/country_codes.csv', index_col=[0])\n",
30 "country_codes = data.index.values\n",
31 "country_names = data['Name']"
32 ]
33 },
34 {
35 "cell_type": "markdown",
36 "metadata": {},
37 "source": [
38 "## Basic Market Map"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "execution_count": null,
44 "metadata": {},
45 "outputs": [],
46 "source": [
47 "market_map = MarketMap(names=country_codes, \n",
48 " # basic data which needs to set for each map\n",
49 " ref_data=data, \n",
50 " # Data frame which can be used for different properties of the map\n",
51 " # Axis and scale for color data\n",
52 " tooltip_fields=['Name'],\n",
53 " layout=Layout(width='800px', height='600px'))\n",
54 "\n",
55 "market_map"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": null,
61 "metadata": {},
62 "outputs": [],
63 "source": [
64 "market_map.colors = ['MediumSeaGreen']"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": null,
70 "metadata": {},
71 "outputs": [],
72 "source": [
73 "market_map.font_style = {'font-size': '16px', 'fill':'white'}"
74 ]
75 },
76 {
77 "cell_type": "code",
78 "execution_count": null,
79 "metadata": {},
80 "outputs": [],
81 "source": [
82 "market_map.title = 'Country Map'\n",
83 "market_map.title_style = {'fill': 'Red'}"
84 ]
85 },
86 {
87 "cell_type": "markdown",
88 "metadata": {},
89 "source": [
90 "## GDP data with grouping by continent\n",
91 "\n",
92 "World Bank national accounts data, and OECD National Accounts data files. (The World Bank: GDP per capita (current US$))"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "metadata": {},
99 "outputs": [],
100 "source": [
101 "gdp_data = pd.read_csv('../../data_files/gdp_per_capita.csv', index_col=[0], parse_dates=True)\n",
102 "gdp_data.fillna(method='backfill', inplace=True)\n",
103 "gdp_data.fillna(method='ffill', inplace=True)"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": null,
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "col = ColorScale(scheme='Greens')\n",
113 "continents = data['Continent'].values\n",
114 "ax_c = ColorAxis(scale=col, label='GDP per Capita', visible=False)\n",
115 "\n",
116 "data['GDP'] = gdp_data.iloc[-1]\n",
117 "\n",
118 "market_map = MarketMap(names=country_codes, groups=continents, # Basic data which needs to set for each map\n",
119 " cols=25, row_groups=3, # Properties for the visualization\n",
120 " ref_data=data, # Data frame used for different properties of the map\n",
121 " tooltip_fields=['Name', 'Continent', 'GDP'], # Columns from data frame to be displayed as tooltip\n",
122 " tooltip_formats=['', '', '.1f'],\n",
123 " scales={'color': col}, axes=[ax_c],\n",
124 " layout=Layout(min_width='800px', min_height='600px')) # Axis and scale for color data"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": null,
130 "metadata": {},
131 "outputs": [],
132 "source": [
133 "deb_output = Label()\n",
134 "def selected_index_changed(change):\n",
135 " deb_output.value = str(change.new)\n",
136 " \n",
137 "market_map.observe(selected_index_changed, 'selected')\n",
138 "VBox([deb_output, market_map])"
139 ]
140 },
141 {
142 "cell_type": "code",
143 "execution_count": null,
144 "metadata": {},
145 "outputs": [],
146 "source": [
147 "# Attribute to show the names of the groups, in this case the continents\n",
148 "market_map.show_groups = True"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {},
155 "outputs": [],
156 "source": [
157 "# Setting the selected countries\n",
158 "market_map.show_groups = False\n",
159 "market_map.selected = ['PAN', 'FRA', 'PHL']"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {},
166 "outputs": [],
167 "source": [
168 "# changing selected stroke and hovered stroke variable\n",
169 "market_map.selected_stroke = 'yellow'\n",
170 "market_map.hovered_stroke = 'violet'"
171 ]
172 },
173 {
174 "cell_type": "markdown",
175 "metadata": {},
176 "source": [
177 "## Setting the color based on data"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": null,
183 "metadata": {},
184 "outputs": [],
185 "source": [
186 "# Adding data for color and making color axis visible\n",
187 "market_map.colors=['#ccc']\n",
188 "market_map.color = data['GDP']\n",
189 "ax_c.visible = True"
190 ]
191 },
192 {
193 "cell_type": "markdown",
194 "metadata": {},
195 "source": [
196 "## Adding a widget as tooltip"
197 ]
198 },
199 {
200 "cell_type": "code",
201 "execution_count": null,
202 "metadata": {},
203 "outputs": [],
204 "source": [
205 "# Creating the figure to be displayed as the tooltip\n",
206 "sc_x = DateScale()\n",
207 "sc_y = LinearScale()\n",
208 "\n",
209 "ax_x = Axis(scale=sc_x, grid_lines='dashed', label='Date')\n",
210 "ax_y = Axis(scale=sc_y, orientation='vertical', grid_lines='dashed',\n",
211 " label='GDP', label_location='end', label_offset='-1em')\n",
212 "\n",
213 "line = Lines(x= gdp_data.index.values, y=[], scales={'x': sc_x, 'y': sc_y}, colors=['orange'])\n",
214 "fig_tooltip = Figure(marks=[line], axes=[ax_x, ax_y])"
215 ]
216 },
217 {
218 "cell_type": "code",
219 "execution_count": null,
220 "metadata": {},
221 "outputs": [],
222 "source": [
223 "market_map = MarketMap(names=country_codes, groups=continents,\n",
224 " cols=25, row_groups=3,\n",
225 " color=data['GDP'], scales={'color': col}, axes=[ax_c],\n",
226 " ref_data=data, tooltip_widget=fig_tooltip,\n",
227 " freeze_tooltip_location=True,\n",
228 " colors=['#ccc'],\n",
229 " layout=Layout(min_width='900px', min_height='600px'))\n",
230 "\n",
231 "# Update the tooltip chart\n",
232 "hovered_symbol = ''\n",
233 "def hover_handler(self, content):\n",
234 " global hovered_symbol\n",
235 " symbol = content.get('data', '')\n",
236 " \n",
237 " if(symbol != hovered_symbol):\n",
238 " hovered_symbol = symbol\n",
239 " if(gdp_data.get(hovered_symbol) is not None):\n",
240 " line.y = gdp_data[hovered_symbol].values\n",
241 " fig_tooltip.title = content.get('ref_data', {}).get('Name', '')\n",
242 "\n",
243 "# Custom msg sent when a particular cell is hovered on\n",
244 "market_map.on_hover(hover_handler)\n",
245 "market_map"
246 ]
247 },
248 {
249 "cell_type": "markdown",
250 "metadata": {},
251 "source": [
252 "This notebook uses data derived from the World Bank dataset.\n",
253 "- The World Bank: GDP per capita (current US$)\n",
254 "- The World Bank: Country Codes \n",
255 "\n",
256 "See the LICENSE file for more information."
257 ]
258 }
259 ],
260 "metadata": {
261 "anaconda-cloud": {},
262 "kernelspec": {
263 "display_name": "Python 3",
264 "language": "python",
265 "name": "python3"
266 },
267 "language_info": {
268 "codemirror_mode": {
269 "name": "ipython",
270 "version": 3
271 },
272 "file_extension": ".py",
273 "mimetype": "text/x-python",
274 "name": "python",
275 "nbconvert_exporter": "python",
276 "pygments_lexer": "ipython3",
277 "version": "3.7.6"
278 }
279 },
280 "nbformat": 4,
281 "nbformat_minor": 2
282 }