{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Geographic Visualization using plotly/geoplotib - Neerja Doshi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dataset - The dataset used here is the World Happiness Report data for 2015. It can be found here: https://www.kaggle.com/unsdsn/world-happiness/data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/vnd.plotly.v1+html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# import the necessary packages\n", "import pandas as pd\n", "import numpy as np\n", "\n", "import geoplotlib\n", "from geoplotlib.colors import ColorMap\n", "from geoplotlib.colors import create_set_cmap\n", "import pyglet\n", "from sklearn.cluster import KMeans\n", "from geoplotlib.layers import BaseLayer\n", "from geoplotlib.core import BatchPainter\n", "from geoplotlib.utils import BoundingBox\n", "\n", "import plotly.graph_objs as go\n", "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n", "init_notebook_mode(connected=True)\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Country Region Happiness Rank Happiness Score \\\n", "0 Switzerland Western Europe 1 7.587 \n", "1 Iceland Western Europe 2 7.561 \n", "2 Denmark Western Europe 3 7.527 \n", "3 Norway Western Europe 4 7.522 \n", "4 Canada North America 5 7.427 \n", "\n", " Standard Error Economy (GDP per Capita) Family \\\n", "0 0.03411 1.39651 1.34951 \n", "1 0.04884 1.30232 1.40223 \n", "2 0.03328 1.32548 1.36058 \n", "3 0.03880 1.45900 1.33095 \n", "4 0.03553 1.32629 1.32261 \n", "\n", " Health (Life Expectancy) Freedom Trust (Government Corruption) \\\n", "0 0.94143 0.66557 0.41978 \n", "1 0.94784 0.62877 0.14145 \n", "2 0.87464 0.64938 0.48357 \n", "3 0.88521 0.66973 0.36503 \n", "4 0.90563 0.63297 0.32957 \n", "\n", " Generosity Dystopia Residual \n", "0 0.29678 2.51738 \n", "1 0.43630 2.70201 \n", "2 0.34139 2.49204 \n", "3 0.34699 2.46531 \n", "4 0.45811 2.45176 \n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ISO 3166 Country CodeCountryLatitudeLongitude
0ADAndorra42.501.50
1AEUnited Arab Emirates24.0054.00
2AFAfghanistan33.0065.00
3AGAntigua and Barbuda17.05-61.80
4AIAnguilla18.25-63.17
\n", "
" ], "text/plain": [ " ISO 3166 Country Code Country Latitude Longitude\n", "0 AD Andorra 42.50 1.50\n", "1 AE United Arab Emirates 24.00 54.00\n", "2 AF Afghanistan 33.00 65.00\n", "3 AG Antigua and Barbuda 17.05 -61.80\n", "4 AI Anguilla 18.25 -63.17" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('2015.csv')\n", "print(df.head())\n", "map_data = pd.read_csv('countries.csv')\n", "map_data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Choropleth\n", "This map shows the happiness rank of all the countries in the world in 2015. The darker the colour, the higher the rank, i.e. the happier the people in that country. It can be seen that the countries of North America (the US, Mexico and Canada), Australia, New Zealand and the western countries of Europe have the happiest citizens. Countries that have lower happiness scores are the ones that are either war struck (eg., Iraq) or are highly underdeveloped as can be said of the countries in Africa like Congo and Chad. The top 5 countries are:\n", "1. Switzerland\n", "2. Iceland\n", "3. Denmark\n", "4. Norway\n", "5. Canada\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "autocolorscale": true, "colorbar": { "title": "Happiness" }, "colorscale": [ [ 0, "rgb(5, 10, 172)" ], [ 0.35, "rgb(40, 60, 190)" ], [ 0.5, "rgb(70, 100, 245)" ], [ 0.6, "rgb(90, 120, 245)" ], [ 0.7, "rgb(106, 137, 247)" ], [ 1, "rgb(240, 210, 250)" ] ], "locationmode": "country names", "locations": [ "Switzerland", "Iceland", "Denmark", "Norway", "Canada", "Finland", "Netherlands", "Sweden", "New Zealand", "Australia", "Israel", "Costa Rica", "Austria", "Mexico", "United States", "Brazil", "Luxembourg", "Ireland", "Belgium", "United Arab Emirates", "United Kingdom", "Oman", "Venezuela", "Singapore", "Panama", "Germany", "Chile", "Qatar", "France", "Argentina", "Czech Republic", "Uruguay", "Colombia", "Thailand", "Saudi Arabia", "Spain", "Malta", "Taiwan", "Kuwait", "Suriname", "Trinidad and Tobago", "El Salvador", "Guatemala", "Uzbekistan", "Slovakia", "Japan", "South Korea", "Ecuador", "Bahrain", "Italy", "Bolivia", "Moldova", "Paraguay", "Kazakhstan", "Slovenia", "Lithuania", "Nicaragua", "Peru", "Belarus", "Poland", "Malaysia", "Croatia", "Libya", "Russia", "Jamaica", "North Cyprus", "Cyprus", "Algeria", "Kosovo", "Turkmenistan", "Mauritius", "Hong Kong", "Estonia", "Indonesia", "Vietnam", "Turkey", "Kyrgyzstan", "Nigeria", "Bhutan", "Azerbaijan", "Pakistan", "Jordan", "Montenegro", "China", "Zambia", "Romania", "Serbia", "Portugal", "Latvia", "Philippines", "Somaliland region", "Morocco", "Macedonia", "Mozambique", "Albania", "Bosnia and Herzegovina", "Lesotho", "Dominican Republic", "Laos", "Mongolia", "Swaziland", "Greece", "Lebanon", "Hungary", "Honduras", "Tajikistan", "Tunisia", "Palestinian Territories", "Bangladesh", "Iran", "Ukraine", "Iraq", "South Africa", "Ghana", "Zimbabwe", "Liberia", "India", "Sudan", "Haiti", "Congo (Kinshasa)", "Nepal", "Ethiopia", "Sierra Leone", "Mauritania", "Kenya", "Djibouti", "Armenia", "Botswana", "Myanmar", "Georgia", "Malawi", "Sri Lanka", "Cameroon", "Bulgaria", "Egypt", "Yemen", "Angola", "Mali", "Congo (Brazzaville)", "Comoros", "Uganda", "Senegal", "Gabon", "Niger", "Cambodia", "Tanzania", "Madagascar", "Central African Republic", "Chad", "Guinea", "Ivory Coast", "Burkina Faso", "Afghanistan", "Rwanda", "Benin", "Syria", "Burundi", "Togo" ], "reversescale": true, "text": [ "Switzerland", "Iceland", "Denmark", "Norway", "Canada", "Finland", "Netherlands", "Sweden", "New Zealand", "Australia", "Israel", "Costa Rica", "Austria", "Mexico", "United States", "Brazil", "Luxembourg", "Ireland", "Belgium", "United Arab Emirates", "United Kingdom", "Oman", "Venezuela", "Singapore", "Panama", "Germany", "Chile", "Qatar", "France", "Argentina", "Czech Republic", "Uruguay", "Colombia", "Thailand", "Saudi Arabia", "Spain", "Malta", "Taiwan", "Kuwait", "Suriname", "Trinidad and Tobago", "El Salvador", "Guatemala", "Uzbekistan", "Slovakia", "Japan", "South Korea", "Ecuador", "Bahrain", "Italy", "Bolivia", "Moldova", "Paraguay", "Kazakhstan", "Slovenia", "Lithuania", "Nicaragua", "Peru", "Belarus", "Poland", "Malaysia", "Croatia", "Libya", "Russia", "Jamaica", "North Cyprus", "Cyprus", "Algeria", "Kosovo", "Turkmenistan", "Mauritius", "Hong Kong", "Estonia", "Indonesia", "Vietnam", "Turkey", "Kyrgyzstan", "Nigeria", "Bhutan", "Azerbaijan", "Pakistan", "Jordan", "Montenegro", "China", "Zambia", "Romania", "Serbia", "Portugal", "Latvia", "Philippines", "Somaliland region", "Morocco", "Macedonia", "Mozambique", "Albania", "Bosnia and Herzegovina", "Lesotho", "Dominican Republic", "Laos", "Mongolia", "Swaziland", "Greece", "Lebanon", "Hungary", "Honduras", "Tajikistan", "Tunisia", "Palestinian Territories", "Bangladesh", "Iran", "Ukraine", "Iraq", "South Africa", "Ghana", "Zimbabwe", "Liberia", "India", "Sudan", "Haiti", "Congo (Kinshasa)", "Nepal", "Ethiopia", "Sierra Leone", "Mauritania", "Kenya", "Djibouti", "Armenia", "Botswana", "Myanmar", "Georgia", "Malawi", "Sri Lanka", "Cameroon", "Bulgaria", "Egypt", "Yemen", "Angola", "Mali", "Congo (Brazzaville)", "Comoros", "Uganda", "Senegal", "Gabon", "Niger", "Cambodia", "Tanzania", "Madagascar", "Central African Republic", "Chad", "Guinea", "Ivory Coast", "Burkina Faso", "Afghanistan", "Rwanda", "Benin", "Syria", "Burundi", "Togo" ], "type": "choropleth", "z": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 82, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158 ] } ], "layout": { "geo": { "projection": { "type": "Orthographic" }, "showframe": false }, "title": "Global Happiness" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "scl = [[0,\"rgb(5, 10, 172)\"],[0.35,\"rgb(40, 60, 190)\"],[0.5,\"rgb(70, 100, 245)\"],\\\n", " [0.6,\"rgb(90, 120, 245)\"],[0.7,\"rgb(106, 137, 247)\"],[1,\"rgb(240, 210, 250)\"]]\n", "\n", "\n", "# scl = [[0.0, 'rgb(50,10,143)'],[0.2, 'rgb(117,107,177)'],[0.4, 'rgb(158,154,200)'],\\\n", "# [0.6, 'rgb(188,189,220)'],[0.8, 'rgb(218,208,235)'],[1.0, 'rgb(250,240,255)']]\n", "data = dict(type = 'choropleth', \n", " colorscale = scl,\n", " autocolorscale = True,\n", " reversescale = True,\n", " locations = df['Country'],\n", " locationmode = 'country names',\n", " z = df['Happiness Rank'], \n", " text = df['Country'],\n", " colorbar = {'title':'Happiness'})\n", "layout = dict(title = 'Global Happiness', \n", " geo = dict(showframe = False, \n", " projection = {'type': 'Orthographic'}))\n", "\n", "choromap3 = go.Figure(data = [data], layout=layout)\n", "iplot(choromap3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Symbol Map\n", "To take our analysis further, we plot a symbol map where the size of the circles represents the Happiness Score while the colour represents the GDP of the countries. Larger the circle, happier the citizens while darker the circle, higher the GDP.\n", "From this plot, we can see that the top 5 countries we have seen above definitely have a much higher GDP. ** This seems to imply that more well off a country is economically, the happier its citizens are. ** Also, the underdeveloped countries like Chad, Congo, Burundi and Togo have a very low GDP and also a very low happiness score. While we cannot directly say that low GDP implies lower happiness, it seems like an important factor. This trend remain consistent throughout all the countries. ** There are almost no countries that have a high GDP but low happiness index or vice versa. **\n", "\n", "Also geographic location and neighbours may be playing an important role. We can clusters/regions with countries having similar GDPs and similar Happiness Scores. ** So countries that have a good economy and good relations with their neighbours benefit from mutual growth and this is also reflected in their happiness scores. Countries that have disturbed neighbourhoods, like in middle east Asia (Iraq, Afghanistan, etc.), show much lower growth/economic prosperity as well as lower happiness scores. **\n", "\n", "One thing that can also be noted is that in general, countries which are known for their lower population densities (https://www.worldatlas.com/articles/the-10-least-densely-populated-places-in-the-world-2015.html) like Denmark and Iceland are much happier than the more densly populated countries." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CountryRegionHappiness RankHappiness ScoreStandard ErrorEconomy (GDP per Capita)FamilyHealth (Life Expectancy)FreedomTrust (Government Corruption)GenerosityDystopia ResidualISO 3166 Country CodeLatitudeLongitude
0SwitzerlandWestern Europe17.5870.034111.396511.349510.941430.665570.419780.296782.51738CH47.08.0
1IcelandWestern Europe27.5610.048841.302321.402230.947840.628770.141450.436302.70201IS65.0-18.0
2DenmarkWestern Europe37.5270.033281.325481.360580.874640.649380.483570.341392.49204DK56.010.0
3NorwayWestern Europe47.5220.038801.459001.330950.885210.669730.365030.346992.46531NO62.010.0
4CanadaNorth America57.4270.035531.326291.322610.905630.632970.329570.458112.45176CA60.0-95.0
\n", "
" ], "text/plain": [ " Country Region Happiness Rank Happiness Score \\\n", "0 Switzerland Western Europe 1 7.587 \n", "1 Iceland Western Europe 2 7.561 \n", "2 Denmark Western Europe 3 7.527 \n", "3 Norway Western Europe 4 7.522 \n", "4 Canada North America 5 7.427 \n", "\n", " Standard Error Economy (GDP per Capita) Family \\\n", "0 0.03411 1.39651 1.34951 \n", "1 0.04884 1.30232 1.40223 \n", "2 0.03328 1.32548 1.36058 \n", "3 0.03880 1.45900 1.33095 \n", "4 0.03553 1.32629 1.32261 \n", "\n", " Health (Life Expectancy) Freedom Trust (Government Corruption) \\\n", "0 0.94143 0.66557 0.41978 \n", "1 0.94784 0.62877 0.14145 \n", "2 0.87464 0.64938 0.48357 \n", "3 0.88521 0.66973 0.36503 \n", "4 0.90563 0.63297 0.32957 \n", "\n", " Generosity Dystopia Residual ISO 3166 Country Code Latitude Longitude \n", "0 0.29678 2.51738 CH 47.0 8.0 \n", "1 0.43630 2.70201 IS 65.0 -18.0 \n", "2 0.34139 2.49204 DK 56.0 10.0 \n", "3 0.34699 2.46531 NO 62.0 10.0 \n", "4 0.45811 2.45176 CA 60.0 -95.0 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = df.merge(map_data, how='left', on = ['Country'])\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2.839, 7.5870000000000006)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['Happiness Score'].min(), df['Happiness Score'].max()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "lat": [ 47, 65, 56, 62, 60, 64, 52.5, 62, -41, -27, 31.5, 10, 47.33, 23, 38, -10, 49.75, 53, 50.83, 24, 54, 21, 8, 1.37, 9, 51, -30, 25.5, 46, -34, 49.75, -33, 4, 15, 25, 40, 35.83, 23.5, 29.34, 4, 11, 13.83, 15.5, 41, 48.67, 36, null, -2, 26, 42.83, -17, null, -23, 48, 46, 56, 13, -10, 53, 52, 2.5, 45.17, null, null, 18.25, null, 35, 28, null, 40, -20.28, 22.25, 59, -5, 16, 39, 41, 10, 27.5, 40.5, 30, 31, 42, 35, -15, 46, 44, 39.5, 57, 13, null, 32, 41.83, -18.25, 41, 44, -29.5, 19, null, 46, -26.5, 39, 33.83, 47, 15, 39, 34, null, 24, null, 49, 33, -29, 8, -20, 6.5, 20, 15, 19, null, 28, 8, 8.5, 20, 1, 11.5, 40, -22, 22, 42, -13.5, 7, 6, 43, 27, 15, -12.5, 17, null, -12.17, 1, 14, -1, 16, 13, null, -20, 7, 15, 11, null, 13, 33, -2, 9.5, null, -3.5, 8 ], "locationmode": "country names", "lon": [ 8, -18, 10, 10, -95, 26, 5.75, 15, 174, 133, 34.75, -84, 13.33, -102, -97, -55, 6.17, -8, 4, 54, -2, 57, -66, 103.8, -80, 9, -71, 51.25, 2, -64, 15.5, -56, -72, 100, 45, -4, 14.58, 121, 47.66, -56, -61, -88.92, -90.25, 64, 19.5, 138, null, -77.5, 50.55, 12.83, -65, null, -58, 68, 15, 24, -85, -76, 28, 20, 112.5, 15.5, null, null, -77.5, null, 33, 3, null, 60, 57.55, 114.17, 26, 120, 106, 35, 75, 8, 90.5, 47.5, 70, 36, 19, 105, 30, 25, 21, -8, 25, 122, null, -5, 22, 35, 20, 18, 28.5, -70.67, null, 105, 31.5, 22, 35.83, 20, -86.5, 71, 9, null, 90, null, 32, 44, 24, -2, 30, -9.5, 77, 30, -72.42, null, 84, 38, -11.5, -12, 38, 43, 45, 24, 98, 43.5, 34, 81, 12, 25, 30, 48, 18.5, -4, null, 44.25, 32, -14, 11.75, 8, 105, null, 47, 21, 19, -10, null, -2, 65, 30, 2.25, null, 30, 1.17 ], "marker": { "autocolorscale": false, "cmax": 1.69042, "cmin": 0, "color": [ 1.3965100000000001, 1.3023200000000001, 1.32548, 1.459, 1.32629, 1.29025, 1.32944, 1.33171, 1.2501799999999998, 1.33358, 1.2285700000000002, 0.95578, 1.33723, 1.02054, 1.3945100000000001, 0.98124, 1.5639100000000001, 1.33596, 1.30782, 1.42727, 1.26637, 1.36011, 1.04424, 1.52186, 1.0635299999999999, 1.32792, 1.10715, 1.69042, 1.27778, 1.05351, 1.17898, 1.06166, 0.91861, 0.9669, 1.39541, 1.23011, 1.2074, 1.29098, 1.5542200000000002, 0.99534, 1.21183, 0.76454, 0.74553, 0.63244, 1.1689100000000001, 1.27074, 1.24461, 0.8640200000000001, 1.32376, 1.25114, 0.68133, 0.59448, 0.75985, 1.1225399999999999, 1.18498, 1.14723, 0.59325, 0.9001899999999999, 1.0319200000000002, 1.12555, 1.12486, 1.08254, 1.13145, 1.13764, 0.81038, 1.2080600000000001, 1.20813, 0.93929, 0.80148, 0.95847, 1.0076100000000001, 1.38604, 1.15174, 0.8282700000000001, 0.63216, 1.06098, 0.47428000000000003, 0.65435, 0.77042, 1.02389, 0.59543, 0.9019799999999999, 0.9743799999999999, 0.89012, 0.47038, 1.04345, 0.92053, 1.15991, 1.11312, 0.70532, 0.18847, 0.7347899999999999, 0.91851, 0.08308, 0.8786700000000001, 0.8322299999999999, 0.37545, 0.8953700000000001, 0.5906600000000001, 0.82819, 0.71206, 1.15406, 1.0256399999999999, 1.12094, 0.59532, 0.39047, 0.88113, 0.59867, 0.39753, 1.0088, 0.79907, 0.98549, 0.9204899999999999, 0.5455800000000001, 0.271, 0.0712, 0.64499, 0.52107, 0.26673, 0, 0.35996999999999996, 0.19072999999999998, 0.33024000000000003, 0.45407, 0.36471, 0.44025, 0.7682100000000001, 0.99355, 0.27108000000000004, 0.7419, 0.016040000000000002, 0.83524, 0.4225, 1.01216, 0.8818, 0.5464899999999999, 0.75778, 0.26074, 0.67866, 0.23906, 0.21101999999999999, 0.36498, 1.06024, 0.0694, 0.46038, 0.2852, 0.20824, 0.0785, 0.34193, 0.17417, 0.46534, 0.25811999999999996, 0.31982, 0.22208000000000003, 0.28665, 0.6632, 0.0153, 0.20868 ], "colorbar": { "title": "GDP per Capita" }, "colorscale": [ [ 0, "rgb(5, 10, 172)" ], [ 0.35, "rgb(40, 60, 190)" ], [ 0.5, "rgb(70, 100, 245)" ], [ 0.6, "rgb(90, 120, 245)" ], [ 0.7, "rgb(106, 137, 247)" ], [ 1, "rgb(220, 220, 220)" ] ], "line": { "color": "rgba(102, 102, 102)", "width": 1 }, "opacity": 0.8, "reversescale": true, "size": [ 22.761000000000003, 22.683, 22.581, 22.566000000000003, 22.281000000000002, 22.218000000000004, 22.133999999999997, 22.092, 21.858, 21.852, 21.834, 21.678, 21.6, 21.561, 21.357, 20.948999999999998, 20.838, 20.82, 20.811, 20.703, 20.601000000000003, 20.558999999999997, 20.43, 20.394, 20.358, 20.25, 20.009999999999998, 19.833000000000002, 19.725, 19.722, 19.515, 19.455000000000002, 19.431, 19.365000000000002, 19.233, 18.987, 18.906000000000002, 18.894, 18.884999999999998, 18.807000000000002, 18.503999999999998, 18.39, 18.369, 18.008999999999997, 17.985, 17.961, 17.951999999999998, 17.924999999999997, 17.88, 17.843999999999998, 17.669999999999998, 17.666999999999998, 17.633999999999997, 17.565, 17.544, 17.499, 17.483999999999998, 17.472, 17.439, 17.373, 17.31, 17.276999999999997, 17.262, 17.148, 17.127, 17.085, 17.067, 16.815, 16.767, 16.644, 16.431, 16.422, 16.287, 16.197, 16.080000000000002, 15.996000000000002, 15.858, 15.803999999999998, 15.758999999999997, 15.636000000000003, 15.582, 15.576, 15.576, 15.419999999999998, 15.386999999999999, 15.372, 15.368999999999998, 15.306000000000001, 15.294, 15.219000000000001, 15.171, 15.039, 15.020999999999999, 14.913, 14.876999999999999, 14.847, 14.693999999999999, 14.655, 14.628, 14.622, 14.600999999999999, 14.571000000000002, 14.517, 14.399999999999999, 14.364, 14.358, 14.216999999999999, 14.145, 14.082, 14.058, 14.043, 14.030999999999999, 13.925999999999998, 13.899000000000001, 13.830000000000002, 13.713000000000001, 13.695, 13.649999999999999, 13.553999999999998, 13.550999999999998, 13.541999999999998, 13.535999999999998, 13.520999999999999, 13.308, 13.256999999999998, 13.107, 13.049999999999999, 12.995999999999999, 12.921, 12.890999999999998, 12.876, 12.812999999999999, 12.756, 12.654, 12.582, 12.231, 12.099, 11.985, 11.966999999999999, 11.868000000000002, 11.793, 11.712, 11.688, 11.535, 11.457, 11.343, 11.043, 11.033999999999999, 11.001, 10.968, 10.965, 10.761, 10.725000000000001, 10.395, 10.02, 9.018, 8.715, 8.517 ], "symbol": "circle" }, "mode": "markers", "text": [ "Switzerland
Happiness Score 7.587", "Iceland
Happiness Score 7.561", "Denmark
Happiness Score 7.527", "Norway
Happiness Score 7.522", "Canada
Happiness Score 7.427", "Finland
Happiness Score 7.406", "Netherlands
Happiness Score 7.378", "Sweden
Happiness Score 7.364", "New Zealand
Happiness Score 7.286", "Australia
Happiness Score 7.284", "Israel
Happiness Score 7.278", "Costa Rica
Happiness Score 7.226", "Austria
Happiness Score 7.2", "Mexico
Happiness Score 7.187", "United States
Happiness Score 7.119", "Brazil
Happiness Score 6.983", "Luxembourg
Happiness Score 6.946", "Ireland
Happiness Score 6.94", "Belgium
Happiness Score 6.937", "United Arab Emirates
Happiness Score 6.901", "United Kingdom
Happiness Score 6.867", "Oman
Happiness Score 6.853", "Venezuela
Happiness Score 6.81", "Singapore
Happiness Score 6.798", "Panama
Happiness Score 6.786", "Germany
Happiness Score 6.75", "Chile
Happiness Score 6.67", "Qatar
Happiness Score 6.611", "France
Happiness Score 6.575", "Argentina
Happiness Score 6.574", "Czech Republic
Happiness Score 6.505", "Uruguay
Happiness Score 6.485", "Colombia
Happiness Score 6.477", "Thailand
Happiness Score 6.455", "Saudi Arabia
Happiness Score 6.411", "Spain
Happiness Score 6.329", "Malta
Happiness Score 6.302", "Taiwan
Happiness Score 6.298", "Kuwait
Happiness Score 6.295", "Suriname
Happiness Score 6.269", "Trinidad and Tobago
Happiness Score 6.168", "El Salvador
Happiness Score 6.13", "Guatemala
Happiness Score 6.123", "Uzbekistan
Happiness Score 6.003", "Slovakia
Happiness Score 5.995", "Japan
Happiness Score 5.987", "South Korea
Happiness Score 5.984", "Ecuador
Happiness Score 5.975", "Bahrain
Happiness Score 5.96", "Italy
Happiness Score 5.948", "Bolivia
Happiness Score 5.89", "Moldova
Happiness Score 5.889", "Paraguay
Happiness Score 5.878", "Kazakhstan
Happiness Score 5.855", "Slovenia
Happiness Score 5.848", "Lithuania
Happiness Score 5.833", "Nicaragua
Happiness Score 5.828", "Peru
Happiness Score 5.824", "Belarus
Happiness Score 5.813", "Poland
Happiness Score 5.791", "Malaysia
Happiness Score 5.77", "Croatia
Happiness Score 5.759", "Libya
Happiness Score 5.754", "Russia
Happiness Score 5.716", "Jamaica
Happiness Score 5.709", "North Cyprus
Happiness Score 5.695", "Cyprus
Happiness Score 5.689", "Algeria
Happiness Score 5.605", "Kosovo
Happiness Score 5.589", "Turkmenistan
Happiness Score 5.548", "Mauritius
Happiness Score 5.477", "Hong Kong
Happiness Score 5.474", "Estonia
Happiness Score 5.429", "Indonesia
Happiness Score 5.399", "Vietnam
Happiness Score 5.36", "Turkey
Happiness Score 5.332", "Kyrgyzstan
Happiness Score 5.286", "Nigeria
Happiness Score 5.268", "Bhutan
Happiness Score 5.253", "Azerbaijan
Happiness Score 5.212", "Pakistan
Happiness Score 5.194", "Jordan
Happiness Score 5.192", "Montenegro
Happiness Score 5.192", "China
Happiness Score 5.14", "Zambia
Happiness Score 5.129", "Romania
Happiness Score 5.124", "Serbia
Happiness Score 5.123", "Portugal
Happiness Score 5.102", "Latvia
Happiness Score 5.098", "Philippines
Happiness Score 5.073", "Somaliland region
Happiness Score 5.057", "Morocco
Happiness Score 5.013", "Macedonia
Happiness Score 5.007", "Mozambique
Happiness Score 4.971", "Albania
Happiness Score 4.959", "Bosnia and Herzegovina
Happiness Score 4.949", "Lesotho
Happiness Score 4.898", "Dominican Republic
Happiness Score 4.885", "Laos
Happiness Score 4.876", "Mongolia
Happiness Score 4.874", "Swaziland
Happiness Score 4.867", "Greece
Happiness Score 4.857", "Lebanon
Happiness Score 4.839", "Hungary
Happiness Score 4.8", "Honduras
Happiness Score 4.788", "Tajikistan
Happiness Score 4.786", "Tunisia
Happiness Score 4.739", "Palestinian Territories
Happiness Score 4.715", "Bangladesh
Happiness Score 4.694", "Iran
Happiness Score 4.686", "Ukraine
Happiness Score 4.681", "Iraq
Happiness Score 4.677", "South Africa
Happiness Score 4.642", "Ghana
Happiness Score 4.633", "Zimbabwe
Happiness Score 4.61", "Liberia
Happiness Score 4.571", "India
Happiness Score 4.565", "Sudan
Happiness Score 4.55", "Haiti
Happiness Score 4.518", "Congo (Kinshasa)
Happiness Score 4.517", "Nepal
Happiness Score 4.514", "Ethiopia
Happiness Score 4.512", "Sierra Leone
Happiness Score 4.507", "Mauritania
Happiness Score 4.436", "Kenya
Happiness Score 4.419", "Djibouti
Happiness Score 4.369", "Armenia
Happiness Score 4.35", "Botswana
Happiness Score 4.332", "Myanmar
Happiness Score 4.307", "Georgia
Happiness Score 4.297", "Malawi
Happiness Score 4.292", "Sri Lanka
Happiness Score 4.271", "Cameroon
Happiness Score 4.252", "Bulgaria
Happiness Score 4.218", "Egypt
Happiness Score 4.194", "Yemen
Happiness Score 4.077", "Angola
Happiness Score 4.033", "Mali
Happiness Score 3.995", "Congo (Brazzaville)
Happiness Score 3.989", "Comoros
Happiness Score 3.956", "Uganda
Happiness Score 3.931", "Senegal
Happiness Score 3.904", "Gabon
Happiness Score 3.896", "Niger
Happiness Score 3.845", "Cambodia
Happiness Score 3.819", "Tanzania
Happiness Score 3.781", "Madagascar
Happiness Score 3.681", "Central African Republic
Happiness Score 3.678", "Chad
Happiness Score 3.667", "Guinea
Happiness Score 3.656", "Ivory Coast
Happiness Score 3.655", "Burkina Faso
Happiness Score 3.587", "Afghanistan
Happiness Score 3.575", "Rwanda
Happiness Score 3.465", "Benin
Happiness Score 3.34", "Syria
Happiness Score 3.006", "Burundi
Happiness Score 2.905", "Togo
Happiness Score 2.839" ], "type": "scattergeo" } ], "layout": { "geo": { "countrywidth": 0.5, "landcolor": "rgb(250, 250, 250)", "projection": { "type": "Mercator" }, "showland": true, "subunitwidth": 0.5 }, "title": "Happiness Scores by GDP" } }, "text/html": [ "
" ], "text/vnd.plotly.v1+html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df['text']=df['Country'] + '
Happiness Score ' + (df['Happiness Score']).astype(str)\n", "scl = [ [0,\"rgb(5, 10, 172)\"],[0.35,\"rgb(40, 60, 190)\"],[0.5,\"rgb(70, 100, 245)\"],\\\n", " [0.6,\"rgb(90, 120, 245)\"],[0.7,\"rgb(106, 137, 247)\"],[1,\"rgb(220, 220, 220)\"] ]\n", "\n", "data = [ dict(\n", " type = 'scattergeo',\n", " locationmode = 'country names',\n", " lon = df['Longitude'],\n", " lat = df['Latitude'],\n", " text = df['text'],\n", " mode = 'markers',\n", " marker = dict(\n", " size = df['Happiness Score']*3,\n", " opacity = 0.8,\n", " reversescale = True,\n", " autocolorscale = False,\n", " symbol = 'circle',\n", " line = dict(\n", " width=1,\n", " color='rgba(102, 102, 102)'\n", " ),\n", " colorscale = scl,\n", " cmin = 0,\n", " color = df['Economy (GDP per Capita)'],\n", " cmax = df['Economy (GDP per Capita)'].max(),\n", " colorbar=dict(\n", " title=\"GDP per Capita\"\n", " )\n", " ))]\n", "\n", "layout = dict(\n", " title = 'Happiness Scores by GDP',\n", " geo = dict(\n", "# scope='usa',\n", " projection=dict( type='Mercator' ),\n", " showland = True,\n", " landcolor = \"rgb(250, 250, 250)\",\n", "# subunitcolor = \"rgb(217, 217, 217)\",\n", "# countrycolor = \"rgb(217, 217, 217)\",\n", " countrywidth = 0.5,\n", " subunitwidth = 0.5\n", " ),\n", " )\n", "\n", "symbolmap = go.Figure(data = data, layout=layout)\n", "\n", "iplot(symbolmap)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Extra plot\n", "A cluster plot may also be used to see if the clustered regions coincide with any of the regions above. (This plot opens in a new window)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\"\"\"\n", "Example of keyboard interaction\n", "\"\"\"\n", "\n", "class KMeansLayer(BaseLayer):\n", "\n", " def __init__(self, data):\n", " self.data = data\n", " self.k = 2\n", "\n", "\n", " def invalidate(self, proj):\n", " self.painter = BatchPainter()\n", " x, y = proj.lonlat_to_screen(self.data['Longitude'], self.data['Latitude'])\n", "\n", " k_means = KMeans(n_clusters=self.k)\n", " k_means.fit(np.vstack([x,y]).T)\n", " labels = k_means.labels_\n", "\n", " self.cmap = create_set_cmap(set(labels), 'hsv')\n", " for l in set(labels):\n", " self.painter.set_color(self.cmap[l])\n", " self.painter.convexhull(x[labels == l], y[labels == l])\n", " self.painter.points(x[labels == l], y[labels == l], 2)\n", " \n", " \n", " def draw(self, proj, mouse_x, mouse_y, ui_manager):\n", " ui_manager.info('Use left and right to increase/decrease the number of clusters. k = %d' % self.k)\n", " self.painter.batch_draw()\n", "\n", "\n", " def on_key_release(self, key, modifiers):\n", " if key == pyglet.window.key.LEFT:\n", " self.k = max(2,self.k - 1)\n", " return True\n", " elif key == pyglet.window.key.RIGHT:\n", " self.k = self.k + 1\n", " return True\n", " return False\n", " \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "data = geoplotlib.utils.DataAccessObject(df)\n", "geoplotlib.add_layer(KMeansLayer(data))\n", "geoplotlib.set_smoothing(True)\n", "geoplotlib.set_bbox(geoplotlib.utils.BoundingBox.DK)\n", "geoplotlib.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:anaconda2]", "language": "python", "name": "conda-env-anaconda2-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.13" } }, "nbformat": 4, "nbformat_minor": 2 }