{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5fdfb18e-ca5f-4f5f-89be-41becc33d768",
   "metadata": {},
   "source": [
    "## Introduction to Data Science\n",
    "\n",
    "#### University of Redlands - DATA 101\n",
    "#### Prof: Joanna Bieri [joanna_bieri@redlands.edu](mailto:joanna_bieri@redlands.edu)\n",
    "#### [Class Website: data101.joannabieri.com](https://data101.joannabieri.com)\n",
    "\n",
    "---------------------------------------\n",
    "# Homework Day 10\n",
    "---------------------------------------\n",
    "\n",
    "GOALS:\n",
    "\n",
    "1. Practice the principles of good data visualization.\n",
    "2. Work on more advanced plotting.\n",
    "\n",
    "----------------------------------------------------------\n",
    "\n",
    "This homework has **3 questions** and **1 exercise**.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0153569-41e3-4696-9b34-83ba90a0b5cf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:09.283709Z",
     "iopub.status.busy": "2026-10-01T18:22:09.283627Z",
     "iopub.status.idle": "2026-10-01T18:22:10.169953Z",
     "shell.execute_reply": "2026-10-01T18:22:10.169527Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import plotly.express as px\n",
    "from plotly.subplots import make_subplots\n",
    "import plotly.io as pio\n",
    "pio.renderers.default = 'notebook_connected'\n",
    "\n",
    "# This stops a few warning messages from showing\n",
    "pd.options.mode.chained_assignment = None \n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f04534cf-2528-4945-b330-85488a6beb94",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.172454Z",
     "iopub.status.busy": "2026-10-01T18:22:10.172377Z",
     "iopub.status.idle": "2026-10-01T18:22:10.356761Z",
     "shell.execute_reply": "2026-10-01T18:22:10.356479Z"
    }
   },
   "outputs": [],
   "source": [
    "file_name = 'https://joannabieri.com/introdatascience/data/brexit.csv'\n",
    "DF = pd.read_csv(file_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8475cd35-0915-44d5-93b5-ba44258050a7",
   "metadata": {},
   "source": [
    "## Do opinions about Brexit depend on region?\n",
    "\n",
    "**Am I clearly answering my question with these pictures?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2d4227ac-cb9b-494e-a180-7573fb99523a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.359027Z",
     "iopub.status.busy": "2026-10-01T18:22:10.358944Z",
     "iopub.status.idle": "2026-10-01T18:22:10.363015Z",
     "shell.execute_reply": "2026-10-01T18:22:10.362872Z"
    }
   },
   "outputs": [],
   "source": [
    "mask = DF['location'] != 'total'\n",
    "DF_plot=DF[mask]\n",
    "\n",
    "DF_plot['location'] = DF_plot['location'].replace('london','London')\n",
    "DF_plot['location'] = DF_plot['location'].replace('rest_of_south','Rest of South')\n",
    "DF_plot['location'] = DF_plot['location'].replace('midlands_wales','Midlands and Wales')\n",
    "DF_plot['location'] = DF_plot['location'].replace('north','North')\n",
    "DF_plot['location'] = DF_plot['location'].replace('scot','Scotland')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "86742477-b441-4a63-af20-7dcf56bb560a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.364656Z",
     "iopub.status.busy": "2026-10-01T18:22:10.364579Z",
     "iopub.status.idle": "2026-10-01T18:22:10.536719Z",
     "shell.execute_reply": "2026-10-01T18:22:10.536483Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "        <script>\n",
       "        window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
       "        if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
       "        </script>\n",
       "        <script type=\"module\">import \"https://cdn.plot.ly/plotly-4.1.1.min\"</script>\n",
       "        "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div style=\"height:500px; width:800px;\">            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js\"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
       "        <script charset=\"utf-8\" src=\"https://cdn.plot.ly/plotly-4.1.1.min.js\" integrity=\"sha256-O24V1F27f8pb0glCkelh3cVHLNiHAJ5gCaVtq2aNch8=\" crossorigin=\"anonymous\"></script>                <div id=\"e0e93fdb-045a-45b8-8ef2-0d6100312ecd\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>            <script>                window.PLOTLYENV=window.PLOTLYENV || {};                                if (document.getElementById(\"e0e93fdb-045a-45b8-8ef2-0d6100312ecd\")) {                    Plotly.newPlot(                        \"e0e93fdb-045a-45b8-8ef2-0d6100312ecd\",                        [{\"hovertemplate\":\"vote=Right\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003elocation=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"rgb(136, 204, 238)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"PwDxAJEAsAAnAA==\"},\"xaxis\":\"x\",\"y\":[\"London\",\"Rest of South\",\"Midlands and Wales\",\"North\",\"Scotland\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=Wrong\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003elocation=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"rgb(204, 102, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"bgABAZgAsABcAA==\"},\"xaxis\":\"x\",\"y\":[\"London\",\"Rest of South\",\"Midlands and Wales\",\"North\",\"Scotland\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=Don’t know\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003elocation=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"rgb(221, 204, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"GDE5MAo=\"},\"xaxis\":\"x\",\"y\":[\"London\",\"Rest of South\",\"Midlands and Wales\",\"North\",\"Scotland\"],\"yaxis\":\"y\",\"type\":\"bar\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"rgb(51,51,51)\"},\"error_y\":{\"color\":\"rgb(51,51,51)\"},\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"baxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"contour\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergl\"}],\"scattermap\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattermap\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"rgb(237,237,237)\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"rgb(217,217,217)\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"colorscale\":{\"sequential\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"sequentialminus\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]]},\"colorway\":[\"#F8766D\",\"#A3A500\",\"#00BF7D\",\"#00B0F6\",\"#E76BF3\"],\"font\":{\"color\":\"rgb(51,51,51)\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"rgb(237,237,237)\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"rgb(237,237,237)\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"fillcolor\":\"black\",\"line\":{\"width\":0},\"opacity\":0.3},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"}}},\"xaxis\":{\"anchor\":\"y\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"Count of Votes\"}},\"yaxis\":{\"anchor\":\"x\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"Vote\"},\"categoryorder\":\"array\",\"categoryarray\":[\"London\",\"Rest of South\",\"Midlands and Wales\",\"North\",\"Scotland\"]},\"legend\":{\"title\":{\"text\":\"vote\"},\"tracegroupgap\":0},\"margin\":{\"t\":60},\"barmode\":\"relative\",\"title\":{\"text\":\"Count of Votes in YouGov Survey - Location Level\"},\"autosize\":false,\"width\":800,\"height\":500},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('e0e93fdb-045a-45b8-8ef2-0d6100312ecd');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_categories = ['London','Rest of South','Midlands and Wales','North','Scotland']\n",
    "\n",
    "fig = px.bar(DF_plot,y='location',x='count',\n",
    "             color='vote',\n",
    "             color_discrete_sequence=px.colors.qualitative.Safe)\n",
    "\n",
    "fig.update_layout(yaxis={'categoryorder': 'array', 'categoryarray': my_categories },\n",
    "                  title='Count of Votes in YouGov Survey - Location Level',\n",
    "                  xaxis_title=\"Count of Votes\",\n",
    "                  yaxis_title=\"Vote\",\n",
    "                  template='ggplot2',\n",
    "                  autosize=False,\n",
    "                  width=800,\n",
    "                  height=500)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a8376951-26d6-401a-bae5-bf8f49f28ab9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.537701Z",
     "iopub.status.busy": "2026-10-01T18:22:10.537632Z",
     "iopub.status.idle": "2026-10-01T18:22:10.580445Z",
     "shell.execute_reply": "2026-10-01T18:22:10.580156Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"height:500px; width:1000px;\">            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js\"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
       "        <script charset=\"utf-8\" src=\"https://cdn.plot.ly/plotly-4.1.1.min.js\" integrity=\"sha256-O24V1F27f8pb0glCkelh3cVHLNiHAJ5gCaVtq2aNch8=\" crossorigin=\"anonymous\"></script>                <div id=\"9e15c057-a2db-42e4-951d-075c21f36766\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>            <script>                window.PLOTLYENV=window.PLOTLYENV || {};                                if (document.getElementById(\"9e15c057-a2db-42e4-951d-075c21f36766\")) {                    Plotly.newPlot(                        \"9e15c057-a2db-42e4-951d-075c21f36766\",                        [{\"hovertemplate\":\"location=London\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"London\",\"marker\":{\"color\":\"rgb(136, 204, 238)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"London\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"P24Y\"},\"xaxis\":\"x\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Rest of South\",\"marker\":{\"color\":\"rgb(204, 102, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Rest of South\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"8QABATEA\"},\"xaxis\":\"x2\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Midlands and Wales\",\"marker\":{\"color\":\"rgb(221, 204, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Midlands and Wales\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"kQCYADkA\"},\"xaxis\":\"x3\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"location=North\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"North\",\"marker\":{\"color\":\"rgb(17, 119, 51)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"North\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"sACwADAA\"},\"xaxis\":\"x4\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Scotland\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Scotland\",\"marker\":{\"color\":\"rgb(51, 34, 136)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Scotland\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"J1wK\"},\"xaxis\":\"x5\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y5\",\"type\":\"bar\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"rgb(51,51,51)\"},\"error_y\":{\"color\":\"rgb(51,51,51)\"},\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"baxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"contour\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergl\"}],\"scattermap\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattermap\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"rgb(237,237,237)\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"rgb(217,217,217)\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"colorscale\":{\"sequential\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"sequentialminus\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]]},\"colorway\":[\"#F8766D\",\"#A3A500\",\"#00BF7D\",\"#00B0F6\",\"#E76BF3\"],\"font\":{\"color\":\"rgb(51,51,51)\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"rgb(237,237,237)\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"rgb(237,237,237)\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"fillcolor\":\"black\",\"line\":{\"width\":0},\"opacity\":0.3},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"}}},\"xaxis\":{\"anchor\":\"y\",\"domain\":[0.0,0.18400000000000002],\"title\":{\"text\":\"count\"}},\"yaxis\":{\"anchor\":\"x\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"\"},\"categoryorder\":\"array\",\"categoryarray\":[\"Don’t know\",\"Right\",\"Wrong\"]},\"xaxis2\":{\"anchor\":\"y2\",\"domain\":[0.20400000000000001,0.388],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis2\":{\"anchor\":\"x2\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis3\":{\"anchor\":\"y3\",\"domain\":[0.40800000000000003,0.5920000000000001],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis3\":{\"anchor\":\"x3\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis4\":{\"anchor\":\"y4\",\"domain\":[0.6120000000000001,0.7960000000000002],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis4\":{\"anchor\":\"x4\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis5\":{\"anchor\":\"y5\",\"domain\":[0.8160000000000001,1.0],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis5\":{\"anchor\":\"x5\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"annotations\":[{\"font\":{},\"showarrow\":false,\"text\":\"London\",\"x\":0.09200000000000001,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Rest of South\",\"x\":0.29600000000000004,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Midlands and Wales\",\"x\":0.5,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"North\",\"x\":0.7040000000000002,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Scotland\",\"x\":0.908,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"}],\"legend\":{\"title\":{\"text\":\"Location\"},\"tracegroupgap\":0},\"margin\":{\"t\":60},\"barmode\":\"relative\",\"title\":{\"text\":\"Count of Votes in YouGov Survey - Location Level\"},\"autosize\":false,\"width\":1000,\"height\":500},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('9e15c057-a2db-42e4-951d-075c21f36766');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_categories = ['London','Rest of South','Midlands and Wales','North','Scotland']\n",
    "\n",
    "fig = px.bar(DF_plot,y='vote',x='count',\n",
    "             color='location',\n",
    "             facet_col='location',\n",
    "            color_discrete_sequence=px.colors.qualitative.Safe)\n",
    "\n",
    "fig.for_each_annotation(lambda a: a.update(text=a.text.split(\"=\")[1]))\n",
    "\n",
    "my_categories = ['Don’t know','Right','Wrong']\n",
    "fig.update_layout(yaxis={'categoryorder': 'array', 'categoryarray': my_categories},\n",
    "                  title='Count of Votes in YouGov Survey - Location Level',\n",
    "                  yaxis_title=\"\",\n",
    "                  template='ggplot2',\n",
    "                  legend_title='Location',\n",
    "                  autosize=False,\n",
    "                  width=1000,\n",
    "                  height=500)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbd4b745-ab8e-4ae9-bc60-a254f7c61b5c",
   "metadata": {},
   "source": [
    "**Q** Which of the plots do you think is better. What do you notice are the pluses and minuses of each figure?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9daf089a-be59-4ac0-a488-b43eb4c96e5b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "47c2337c-8687-49b2-bff2-eeca81e636ba",
   "metadata": {},
   "source": [
    "**Q** Is there any redundancy in the second graph? What is redundant?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a04c68d9-25da-420d-a516-28c11d3bac1f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a5911e76-a6d2-41bd-ac2c-00578a842c77",
   "metadata": {},
   "source": [
    "## Avoid Redundancy\n",
    "\n",
    "Here is the same graph again, but avoiding redundancy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "46b662b7-5532-4528-97d4-665dc6d30031",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.581698Z",
     "iopub.status.busy": "2026-10-01T18:22:10.581635Z",
     "iopub.status.idle": "2026-10-01T18:22:10.623591Z",
     "shell.execute_reply": "2026-10-01T18:22:10.623219Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"height:500px; width:1000px;\">            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js\"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
       "        <script charset=\"utf-8\" src=\"https://cdn.plot.ly/plotly-4.1.1.min.js\" integrity=\"sha256-O24V1F27f8pb0glCkelh3cVHLNiHAJ5gCaVtq2aNch8=\" crossorigin=\"anonymous\"></script>                <div id=\"233a94a2-d07f-46bf-8918-ab6650be3cb6\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>            <script>                window.PLOTLYENV=window.PLOTLYENV || {};                                if (document.getElementById(\"233a94a2-d07f-46bf-8918-ab6650be3cb6\")) {                    Plotly.newPlot(                        \"233a94a2-d07f-46bf-8918-ab6650be3cb6\",                        [{\"hovertemplate\":\"location=London\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"\",\"marker\":{\"color\":\"gray\",\"pattern\":{\"shape\":\"\"}},\"name\":\"\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"P24Y\"},\"xaxis\":\"x\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"\",\"marker\":{\"color\":\"gray\",\"pattern\":{\"shape\":\"\"}},\"name\":\"\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"8QABATEA\"},\"xaxis\":\"x2\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"\",\"marker\":{\"color\":\"gray\",\"pattern\":{\"shape\":\"\"}},\"name\":\"\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"kQCYADkA\"},\"xaxis\":\"x3\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"location=North\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"\",\"marker\":{\"color\":\"gray\",\"pattern\":{\"shape\":\"\"}},\"name\":\"\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"sACwADAA\"},\"xaxis\":\"x4\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Scotland\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"\",\"marker\":{\"color\":\"gray\",\"pattern\":{\"shape\":\"\"}},\"name\":\"\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"J1wK\"},\"xaxis\":\"x5\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y5\",\"type\":\"bar\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"rgb(51,51,51)\"},\"error_y\":{\"color\":\"rgb(51,51,51)\"},\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"baxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"contour\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergl\"}],\"scattermap\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattermap\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"rgb(237,237,237)\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"rgb(217,217,217)\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"colorscale\":{\"sequential\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"sequentialminus\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]]},\"colorway\":[\"#F8766D\",\"#A3A500\",\"#00BF7D\",\"#00B0F6\",\"#E76BF3\"],\"font\":{\"color\":\"rgb(51,51,51)\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"rgb(237,237,237)\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"rgb(237,237,237)\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"fillcolor\":\"black\",\"line\":{\"width\":0},\"opacity\":0.3},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"}}},\"xaxis\":{\"anchor\":\"y\",\"domain\":[0.0,0.18400000000000002],\"title\":{\"text\":\"count\"}},\"yaxis\":{\"anchor\":\"x\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"\"},\"categoryorder\":\"array\",\"categoryarray\":[\"Don’t know\",\"Right\",\"Wrong\"]},\"xaxis2\":{\"anchor\":\"y2\",\"domain\":[0.20400000000000001,0.388],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis2\":{\"anchor\":\"x2\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis3\":{\"anchor\":\"y3\",\"domain\":[0.40800000000000003,0.5920000000000001],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis3\":{\"anchor\":\"x3\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis4\":{\"anchor\":\"y4\",\"domain\":[0.6120000000000001,0.7960000000000002],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis4\":{\"anchor\":\"x4\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis5\":{\"anchor\":\"y5\",\"domain\":[0.8160000000000001,1.0],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis5\":{\"anchor\":\"x5\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"annotations\":[{\"font\":{},\"showarrow\":false,\"text\":\"London\",\"x\":0.09200000000000001,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Rest of South\",\"x\":0.29600000000000004,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Midlands and Wales\",\"x\":0.5,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"North\",\"x\":0.7040000000000002,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Scotland\",\"x\":0.908,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"}],\"legend\":{\"tracegroupgap\":0,\"title\":{\"text\":\"Location\"}},\"margin\":{\"t\":60},\"barmode\":\"relative\",\"title\":{\"text\":\"Count of Votes in YouGov Survey - Location Level\"},\"autosize\":false,\"width\":1000,\"height\":500},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('233a94a2-d07f-46bf-8918-ab6650be3cb6');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_categories = ['London','Rest of South','Midlands and Wales','North','Scotland']\n",
    "\n",
    "fig = px.bar(DF_plot,y='vote',x='count',\n",
    "             facet_col='location',\n",
    "            color_discrete_sequence=['gray'])\n",
    "\n",
    "fig.for_each_annotation(lambda a: a.update(text=a.text.split(\"=\")[1]))\n",
    "\n",
    "my_categories = ['Don’t know','Right','Wrong']\n",
    "fig.update_layout(yaxis={'categoryorder': 'array', 'categoryarray': my_categories},\n",
    "                  title='Count of Votes in YouGov Survey - Location Level',\n",
    "                  yaxis_title=\"\",\n",
    "                  template='ggplot2',\n",
    "                  legend_title='Location',\n",
    "                  autosize=False,\n",
    "                  width=1000,\n",
    "                  height=500)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ea5f76df-7067-475b-9cda-7c4a8eb2a387",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.624791Z",
     "iopub.status.busy": "2026-10-01T18:22:10.624727Z",
     "iopub.status.idle": "2026-10-01T18:22:10.665238Z",
     "shell.execute_reply": "2026-10-01T18:22:10.665054Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"height:500px; width:1000px;\">            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js\"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
       "        <script charset=\"utf-8\" src=\"https://cdn.plot.ly/plotly-4.1.1.min.js\" integrity=\"sha256-O24V1F27f8pb0glCkelh3cVHLNiHAJ5gCaVtq2aNch8=\" crossorigin=\"anonymous\"></script>                <div id=\"e827438b-a06a-4b6e-b93f-44cc19527a92\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>            <script>                window.PLOTLYENV=window.PLOTLYENV || {};                                if (document.getElementById(\"e827438b-a06a-4b6e-b93f-44cc19527a92\")) {                    Plotly.newPlot(                        \"e827438b-a06a-4b6e-b93f-44cc19527a92\",                        [{\"hovertemplate\":\"location=London\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"London\",\"marker\":{\"color\":\"rgb(136, 204, 238)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"London\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"P24Y\"},\"xaxis\":\"x\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Rest of South\",\"marker\":{\"color\":\"rgb(204, 102, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Rest of South\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"8QABATEA\"},\"xaxis\":\"x2\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Midlands and Wales\",\"marker\":{\"color\":\"rgb(221, 204, 119)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Midlands and Wales\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"kQCYADkA\"},\"xaxis\":\"x3\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"location=North\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"North\",\"marker\":{\"color\":\"rgb(17, 119, 51)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"North\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"sACwADAA\"},\"xaxis\":\"x4\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"location=Scotland\\u003cbr\\u003ecount=%{x}\\u003cbr\\u003evote=%{y}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Scotland\",\"marker\":{\"color\":\"rgb(51, 34, 136)\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Scotland\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"J1wK\"},\"xaxis\":\"x5\",\"y\":[\"Right\",\"Wrong\",\"Don’t know\"],\"yaxis\":\"y5\",\"type\":\"bar\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"rgb(51,51,51)\"},\"error_y\":{\"color\":\"rgb(51,51,51)\"},\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"baxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"contour\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergl\"}],\"scattermap\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattermap\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"rgb(237,237,237)\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"rgb(217,217,217)\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"colorscale\":{\"sequential\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"sequentialminus\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]]},\"colorway\":[\"#F8766D\",\"#A3A500\",\"#00BF7D\",\"#00B0F6\",\"#E76BF3\"],\"font\":{\"color\":\"rgb(51,51,51)\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"rgb(237,237,237)\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"rgb(237,237,237)\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"fillcolor\":\"black\",\"line\":{\"width\":0},\"opacity\":0.3},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"}}},\"xaxis\":{\"anchor\":\"y\",\"domain\":[0.0,0.18400000000000002],\"title\":{\"text\":\"count\"}},\"yaxis\":{\"anchor\":\"x\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"\"},\"categoryorder\":\"array\",\"categoryarray\":[\"Don’t know\",\"Right\",\"Wrong\"]},\"xaxis2\":{\"anchor\":\"y2\",\"domain\":[0.20400000000000001,0.388],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis2\":{\"anchor\":\"x2\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis3\":{\"anchor\":\"y3\",\"domain\":[0.40800000000000003,0.5920000000000001],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis3\":{\"anchor\":\"x3\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis4\":{\"anchor\":\"y4\",\"domain\":[0.6120000000000001,0.7960000000000002],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis4\":{\"anchor\":\"x4\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis5\":{\"anchor\":\"y5\",\"domain\":[0.8160000000000001,1.0],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis5\":{\"anchor\":\"x5\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"annotations\":[{\"font\":{},\"showarrow\":false,\"text\":\"London\",\"x\":0.09200000000000001,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Rest of South\",\"x\":0.29600000000000004,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Midlands and Wales\",\"x\":0.5,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"North\",\"x\":0.7040000000000002,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Scotland\",\"x\":0.908,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"}],\"legend\":{\"title\":{\"text\":\"Location\"},\"tracegroupgap\":0},\"margin\":{\"t\":60},\"barmode\":\"relative\",\"title\":{\"text\":\"In hindsight, do you think Britain was right\\u002fwrong to vote to leave EU?\"},\"autosize\":false,\"width\":1000,\"height\":500,\"showlegend\":false},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('e827438b-a06a-4b6e-b93f-44cc19527a92');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_categories = ['London','Rest of South','Midlands and Wales','North','Scotland']\n",
    "\n",
    "fig = px.bar(DF_plot,y='vote',x='count',\n",
    "             color='location',\n",
    "             facet_col='location',\n",
    "            color_discrete_sequence=px.colors.qualitative.Safe)\n",
    "\n",
    "fig.for_each_annotation(lambda a: a.update(text=a.text.split(\"=\")[1]))\n",
    "\n",
    "my_categories = ['Don’t know','Right','Wrong']\n",
    "fig.update_layout(yaxis={'categoryorder': 'array', 'categoryarray': my_categories},\n",
    "                  title='In hindsight, do you think Britain was right/wrong to vote to leave EU?',\n",
    "                  yaxis_title=\"\",\n",
    "                  template='ggplot2',\n",
    "                  legend_title='Location',\n",
    "                  autosize=False,\n",
    "                  width=1000,\n",
    "                  height=500,\n",
    "                 showlegend=False)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2a5e380-d1b0-450a-8be7-75d576aa332e",
   "metadata": {},
   "source": [
    "**Q** Which of these two plots do you like better and why?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a06ad66e-9d23-4fed-bc39-de068e401aee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "58e88488-42d2-4bd9-9f23-ccee3e9d355a",
   "metadata": {},
   "source": [
    "## Selecting meaningful colors.\n",
    "\n",
    "[colorbrewer.org](https://colorbrewer2.org/)\n",
    "\n",
    "Here is an example where colors were picked on the website above and the codes were put into the color_discrete_map."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e52c6be7-bb1a-4d0b-b6ac-6cd94e951cd3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-01T18:22:10.666403Z",
     "iopub.status.busy": "2026-10-01T18:22:10.666324Z",
     "iopub.status.idle": "2026-10-01T18:22:10.715340Z",
     "shell.execute_reply": "2026-10-01T18:22:10.715060Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"height:500px; width:1000px;\">            <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg.min.js\"></script><script>if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script>                <script>window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n",
       "        <script charset=\"utf-8\" src=\"https://cdn.plot.ly/plotly-4.1.1.min.js\" integrity=\"sha256-O24V1F27f8pb0glCkelh3cVHLNiHAJ5gCaVtq2aNch8=\" crossorigin=\"anonymous\"></script>                <div id=\"e03d7af8-4ff5-4c2f-8245-bb9679c19410\" class=\"plotly-graph-div\" style=\"height:100%; width:100%;\"></div>            <script>                window.PLOTLYENV=window.PLOTLYENV || {};                                if (document.getElementById(\"e03d7af8-4ff5-4c2f-8245-bb9679c19410\")) {                    Plotly.newPlot(                        \"e03d7af8-4ff5-4c2f-8245-bb9679c19410\",                        [{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=London\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"#91bfdb\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"Pw==\"},\"xaxis\":\"x\",\"y\":[\"Right\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"#91bfdb\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"8QA=\"},\"xaxis\":\"x2\",\"y\":[\"Right\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"#91bfdb\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"kQA=\"},\"xaxis\":\"x3\",\"y\":[\"Right\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=North\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"#91bfdb\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"sAA=\"},\"xaxis\":\"x4\",\"y\":[\"Right\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Scotland\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Right\",\"marker\":{\"color\":\"#91bfdb\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Right\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"Jw==\"},\"xaxis\":\"x5\",\"y\":[\"Right\"],\"yaxis\":\"y5\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=London\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"#fc8d59\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"bg==\"},\"xaxis\":\"x\",\"y\":[\"Wrong\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"#fc8d59\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"AQE=\"},\"xaxis\":\"x2\",\"y\":[\"Wrong\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"#fc8d59\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"mAA=\"},\"xaxis\":\"x3\",\"y\":[\"Wrong\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=North\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"#fc8d59\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i2\",\"bdata\":\"sAA=\"},\"xaxis\":\"x4\",\"y\":[\"Wrong\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Scotland\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Wrong\",\"marker\":{\"color\":\"#fc8d59\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Wrong\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"XA==\"},\"xaxis\":\"x5\",\"y\":[\"Wrong\"],\"yaxis\":\"y5\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=London\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"#ffffbf\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":true,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"GA==\"},\"xaxis\":\"x\",\"y\":[\"Don’t know\"],\"yaxis\":\"y\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Rest of South\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"#ffffbf\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"MQ==\"},\"xaxis\":\"x2\",\"y\":[\"Don’t know\"],\"yaxis\":\"y2\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Midlands and Wales\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"#ffffbf\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"OQ==\"},\"xaxis\":\"x3\",\"y\":[\"Don’t know\"],\"yaxis\":\"y3\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=North\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"#ffffbf\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"MA==\"},\"xaxis\":\"x4\",\"y\":[\"Don’t know\"],\"yaxis\":\"y4\",\"type\":\"bar\"},{\"hovertemplate\":\"vote=%{y}\\u003cbr\\u003elocation=Scotland\\u003cbr\\u003ecount=%{x}\\u003cextra\\u003e\\u003c\\u002fextra\\u003e\",\"legendgroup\":\"Don’t know\",\"marker\":{\"color\":\"#ffffbf\",\"pattern\":{\"shape\":\"\"}},\"name\":\"Don’t know\",\"orientation\":\"h\",\"showlegend\":false,\"textposition\":\"auto\",\"x\":{\"dtype\":\"i1\",\"bdata\":\"Cg==\"},\"xaxis\":\"x5\",\"y\":[\"Don’t know\"],\"yaxis\":\"y5\",\"type\":\"bar\"}],                        {\"template\":{\"data\":{\"barpolar\":[{\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"barpolar\"}],\"bar\":[{\"error_x\":{\"color\":\"rgb(51,51,51)\"},\"error_y\":{\"color\":\"rgb(51,51,51)\"},\"marker\":{\"line\":{\"color\":\"rgb(237,237,237)\",\"width\":0.5},\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"bar\"}],\"carpet\":[{\"aaxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"baxis\":{\"endlinecolor\":\"rgb(51,51,51)\",\"gridcolor\":\"white\",\"linecolor\":\"white\",\"minorgridcolor\":\"white\",\"startlinecolor\":\"rgb(51,51,51)\"},\"type\":\"carpet\"}],\"choropleth\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"choropleth\"}],\"contourcarpet\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"contourcarpet\"}],\"contour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"contour\"}],\"heatmap\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"heatmap\"}],\"histogram2dcontour\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2dcontour\"}],\"histogram2d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"histogram2d\"}],\"histogram\":[{\"marker\":{\"pattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2}},\"type\":\"histogram\"}],\"mesh3d\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"type\":\"mesh3d\"}],\"parcoords\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"parcoords\"}],\"pie\":[{\"automargin\":true,\"type\":\"pie\"}],\"scatter3d\":[{\"line\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatter3d\"}],\"scattercarpet\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattercarpet\"}],\"scattergeo\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergeo\"}],\"scattergl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattergl\"}],\"scattermap\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scattermap\"}],\"scatterpolargl\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolargl\"}],\"scatterpolar\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterpolar\"}],\"scatter\":[{\"fillpattern\":{\"fillmode\":\"overlay\",\"size\":10,\"solidity\":0.2},\"type\":\"scatter\"}],\"scatterternary\":[{\"marker\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"type\":\"scatterternary\"}],\"surface\":[{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"},\"colorscale\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"type\":\"surface\"}],\"table\":[{\"cells\":{\"fill\":{\"color\":\"rgb(237,237,237)\"},\"line\":{\"color\":\"white\"}},\"header\":{\"fill\":{\"color\":\"rgb(217,217,217)\"},\"line\":{\"color\":\"white\"}},\"type\":\"table\"}]},\"layout\":{\"annotationdefaults\":{\"arrowhead\":0,\"arrowwidth\":1},\"autotypenumbers\":\"strict\",\"coloraxis\":{\"colorbar\":{\"outlinewidth\":0,\"tickcolor\":\"rgb(237,237,237)\",\"ticklen\":6,\"ticks\":\"inside\"}},\"colorscale\":{\"sequential\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]],\"sequentialminus\":[[0,\"rgb(20,44,66)\"],[1,\"rgb(90,179,244)\"]]},\"colorway\":[\"#F8766D\",\"#A3A500\",\"#00BF7D\",\"#00B0F6\",\"#E76BF3\"],\"font\":{\"color\":\"rgb(51,51,51)\"},\"geo\":{\"bgcolor\":\"white\",\"lakecolor\":\"white\",\"landcolor\":\"rgb(237,237,237)\",\"showlakes\":true,\"showland\":true,\"subunitcolor\":\"white\"},\"hoverlabel\":{\"align\":\"left\"},\"hovermode\":\"closest\",\"paper_bgcolor\":\"white\",\"plot_bgcolor\":\"rgb(237,237,237)\",\"polar\":{\"angularaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"radialaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"scene\":{\"xaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"yaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"},\"zaxis\":{\"backgroundcolor\":\"rgb(237,237,237)\",\"gridcolor\":\"white\",\"gridwidth\":2,\"linecolor\":\"white\",\"showbackground\":true,\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"zerolinecolor\":\"white\"}},\"shapedefaults\":{\"fillcolor\":\"black\",\"line\":{\"width\":0},\"opacity\":0.3},\"ternary\":{\"aaxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"baxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"},\"bgcolor\":\"rgb(237,237,237)\",\"caxis\":{\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\"}},\"xaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"},\"yaxis\":{\"automargin\":true,\"gridcolor\":\"white\",\"linecolor\":\"white\",\"showgrid\":true,\"tickcolor\":\"rgb(51,51,51)\",\"ticks\":\"outside\",\"title\":{\"standoff\":15},\"zerolinecolor\":\"white\"}}},\"xaxis\":{\"anchor\":\"y\",\"domain\":[0.0,0.18400000000000002],\"title\":{\"text\":\"count\"}},\"yaxis\":{\"anchor\":\"x\",\"domain\":[0.0,1.0],\"title\":{\"text\":\"\"},\"categoryorder\":\"array\",\"categoryarray\":[\"Don’t know\",\"Right\",\"Wrong\"]},\"xaxis2\":{\"anchor\":\"y2\",\"domain\":[0.20400000000000001,0.388],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis2\":{\"anchor\":\"x2\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis3\":{\"anchor\":\"y3\",\"domain\":[0.40800000000000003,0.5920000000000001],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis3\":{\"anchor\":\"x3\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis4\":{\"anchor\":\"y4\",\"domain\":[0.6120000000000001,0.7960000000000002],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis4\":{\"anchor\":\"x4\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"xaxis5\":{\"anchor\":\"y5\",\"domain\":[0.8160000000000001,1.0],\"matches\":\"x\",\"title\":{\"text\":\"count\"}},\"yaxis5\":{\"anchor\":\"x5\",\"domain\":[0.0,1.0],\"matches\":\"y\",\"showticklabels\":false},\"annotations\":[{\"font\":{},\"showarrow\":false,\"text\":\"London\",\"x\":0.09200000000000001,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Rest of South\",\"x\":0.29600000000000004,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Midlands and Wales\",\"x\":0.5,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"North\",\"x\":0.7040000000000002,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"},{\"font\":{},\"showarrow\":false,\"text\":\"Scotland\",\"x\":0.908,\"xanchor\":\"center\",\"xref\":\"paper\",\"y\":1.0,\"yanchor\":\"bottom\",\"yref\":\"paper\"}],\"legend\":{\"title\":{\"text\":\"Location\"},\"tracegroupgap\":0},\"margin\":{\"t\":60},\"barmode\":\"relative\",\"title\":{\"text\":\"In hindsight, do you think Britain was right\\u002fwrong to vote to leave EU?\"},\"autosize\":false,\"width\":1000,\"height\":500,\"showlegend\":false},                        {\"responsive\": true}                    ).then(function(){\n",
       "                            \n",
       "var gd = document.getElementById('e03d7af8-4ff5-4c2f-8245-bb9679c19410');\n",
       "var x = new MutationObserver(function (mutations, observer) {{\n",
       "        var display = window.getComputedStyle(gd).display;\n",
       "        if (!display || display === 'none') {{\n",
       "            console.log([gd, 'removed!']);\n",
       "            Plotly.purge(gd);\n",
       "            observer.disconnect();\n",
       "        }}\n",
       "}});\n",
       "\n",
       "// Listen for the removal of the full notebook cells\n",
       "var notebookContainer = gd.closest('#notebook-container');\n",
       "if (notebookContainer) {{\n",
       "    x.observe(notebookContainer, {childList: true});\n",
       "}}\n",
       "\n",
       "// Listen for the clearing of the current output cell\n",
       "var outputEl = gd.closest('.output');\n",
       "if (outputEl) {{\n",
       "    x.observe(outputEl, {childList: true});\n",
       "}}\n",
       "\n",
       "                        })                };            </script>        </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "my_categories = ['London','Rest of South','Midlands and Wales','North','Scotland']\n",
    "\n",
    "fig = px.bar(DF_plot,y='vote',x='count',\n",
    "             color='vote',\n",
    "             facet_col='location',\n",
    "             color_discrete_map={'Right':'#91bfdb','Wrong':'#fc8d59',\"Don’t know\":'#ffffbf'})\n",
    "\n",
    "fig.for_each_annotation(lambda a: a.update(text=a.text.split(\"=\")[1]))\n",
    "\n",
    "my_categories = ['Don’t know','Right','Wrong']\n",
    "fig.update_layout(yaxis={'categoryorder': 'array', 'categoryarray': my_categories},\n",
    "                  title='In hindsight, do you think Britain was right/wrong to vote to leave EU?',\n",
    "                  yaxis_title=\"\",\n",
    "                  template='ggplot2',\n",
    "                  legend_title='Location',\n",
    "                  autosize=False,\n",
    "                  width=1000,\n",
    "                  height=500,\n",
    "                 showlegend=False)\n",
    "\n",
    "fig.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd3412fc-7c18-4621-82ef-f1ca218477dd",
   "metadata": {},
   "source": [
    "### Exercise 1 (Choose one!)\n",
    "\n",
    "#### Data Vis Principles:\n",
    "\n",
    "1. Order Matters\n",
    "2. Put long categories on the y-axis\n",
    "3. Pick a Purpose.\n",
    "4. Keep scales consistent\n",
    "5. Select meaningful colors\n",
    "6. Use meaningful and nonredundant labels.\n",
    "\n",
    "**Option 1.** \n",
    "\n",
    "Create your own plot of this data. Make it as nice as possible! Choose your own colors, themes, labels, ordering, etc. Decide if you prefer facets or colored bars. Make the labels as informative as possible. Try experimenting with things we haven't yet covered in class: look up how to add a caption or include textures in your plot.\n",
    "\n",
    "Talk about the positives and negatives of your graph. How does it meet, not meet, or exceed the data visualization principles above?\n",
    "\n",
    "**Option 2.**\n",
    "\n",
    "Using data of your choice, create a beautiful data visualization. Try experimenting with things we haven't yet covered in class: look up how to add a caption or include textures in your plot.\n",
    "\n",
    "Talk about the positives and negatives of your graph. How does it meet, not meet, or exceed the data visualization principles above?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bfc7d8b-3520-426f-a7d1-770493864331",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.13.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
