{ "cells": [ { "cell_type": "markdown", "id": "5dae00ab", "metadata": {}, "source": [ "# Montenegrin Independence Referendum" ] }, { "cell_type": "markdown", "id": "eaacf775", "metadata": {}, "source": [ "Let's explore the results of the Montenegrin independence referendum in 2006.\n", "\n", "The dataset was downloaded from Wikipedia article\n", "[\"2006 Montenegrin independence referendum\"](https://en.wikipedia.org/wiki/2006_Montenegrin_independence_referendum)." ] }, { "cell_type": "code", "execution_count": 1, "id": "ade29f8d", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:44:27.750988Z", "iopub.status.busy": "2024-08-23T10:44:27.750906Z", "iopub.status.idle": "2024-08-23T10:44:28.078874Z", "shell.execute_reply": "2024-08-23T10:44:28.078449Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "ec0a99bf", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:44:28.080540Z", "iopub.status.busy": "2024-08-23T10:44:28.080397Z", "iopub.status.idle": "2024-08-23T10:44:28.082854Z", "shell.execute_reply": "2024-08-23T10:44:28.082615Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html() " ] }, { "cell_type": "code", "execution_count": 3, "id": "f9e8d752-506c-4f73-b0ec-522fb3e4f7e3", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:44:28.084111Z", "iopub.status.busy": "2024-08-23T10:44:28.084030Z", "iopub.status.idle": "2024-08-23T10:44:28.086275Z", "shell.execute_reply": "2024-08-23T10:44:28.086021Z" } }, "outputs": [], "source": [ "def get_data(df):\n", " # Blank or invalid votes:\n", " df[\"Blank\"] = df[\"Voted\"] - df[\"Yes\"] - df[\"No\"]\n", " df = df[[\"Municipality\", \"Registered\", \"No\", \"Yes\", \"Blank\"]]\n", " return pd.melt(df, id_vars=[\"Municipality\", \"Registered\"], var_name=\"Vote\", value_name=\"Number\")\n", "\n", "def get_geodata(df):\n", " from lets_plot.geo_data import geocode_states\n", " country = \"Montenegro\"\n", " municipalities_geocoder = geocode_states(names=df[\"Municipality\"]).scope(country)\n", " return municipalities_geocoder, pd.merge(\n", " municipalities_geocoder.get_boundaries(resolution=15),\n", " df[[\"Municipality\", \"Yes%\"]],\n", " left_on=\"state\",\n", " right_on=\"Municipality\"\n", " )" ] }, { "cell_type": "code", "execution_count": 4, "id": "18e79c96", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:44:28.087439Z", "iopub.status.busy": "2024-08-23T10:44:28.087365Z", "iopub.status.idle": "2024-08-23T10:44:29.266711Z", "shell.execute_reply": "2024-08-23T10:44:29.266250Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] } ], "source": [ "raw_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/montenegrin_referendum_2006.csv\")\n", "df = get_data(raw_df)\n", "municipalities_geocoder, boundaries_gdf = get_geodata(raw_df)" ] }, { "cell_type": "code", "execution_count": 5, "id": "8c2eefa3", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:44:29.268291Z", "iopub.status.busy": "2024-08-23T10:44:29.268214Z", "iopub.status.idle": "2024-08-23T10:44:29.545681Z", "shell.execute_reply": "2024-08-23T10:44:29.545203Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_livemap(data_size_zoomin=3) + \\\n", " geom_map(aes(fill=\"Yes%\"), data=boundaries_gdf,\n", " tooltips=layer_tooltips().title(\"@state\")\n", " .line(\"'Yes' votes|@{Yes%}\")\n", " .format(\"Yes%\", \"{} %\")) + \\\n", " geom_pie(aes(\"Municipality\", paint_a=\"Vote\", weight=\"Number\", size=\"..sum..\", group=\"Municipality\"),\n", " data=df, map=municipalities_geocoder, map_join=[\"Municipality\", \"state\"],\n", " fill_by='paint_a', hole=.2, stroke=1.5, stroke_side='both',\n", " tooltips=layer_tooltips().title(\"@Municipality\")\n", " .line(\"Vote|@Vote\")\n", " .line(\"Number|@{..count..}\")\n", " .line(\"Percent|@{..prop..}\")\n", " .line(\"Total voted|@{..sum..}\")\n", " .line(\"Registered|@Registered\")\n", " .format(\"..prop..\", \".2%\")\n", " .format(\"Registered\", \",d\")) + \\\n", " scale_fill_gradientn([\"#a50026\", \"#d73027\", \"#66bd63\", \"#4daf4a\", \"#006837\"]) + \\\n", " scale_manual('paint_a', values=[\"#e41a1c\", \"#4daf4a\", \"#999999\"]) + \\\n", " scale_size(name=\"Total voted\", range=[3, 8]) + \\\n", " ggtitle(\"Results of the Montenegrin independence referendum, 2006\") + \\\n", " ggsize(900, 900) + \\\n", " theme(legend_position='none', plot_title=element_text(size=24, face='bold'))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }