Displaying data geographic data using Plotly

Local Authority dwelling stock - UK 2021

Introduction

At work I was recently tasked with visualising recycling statistics from all of the local authorities in the UK. Using an Interactive Chloropleth map was a nice solution and was well recieved. Here I am plotting data from the ONS to show the total number of properties owned by each authority.
Dwelling stock data
Map file

        
            import pandas as pd
            import geojson
            import plotly.express as px
            import numpy as np
            
            # load housing data
            housing_stats = pd.read_csv("LAHS_21-22_Full_Data.csv")

            # only select columns we are interested in
            housing_stats = housing_stats[['LAD21CD', 'LAD21NM', 'RGN21CD', 'RGN21NM', 'Year','a1a']]

            # read our map file
            with open("LAD_(Dec_2021)_GB_BFC.json") as f:
                boundary_data = geojson.load(f)


            
            # produce interacttive map
            fig = px.choropleth_mapbox(housing_stats,
                                        geojson=boundary_data, 
                                        locations='LAD21NM', 
                                        color=np.log(housing_stats['a1a']),
                                        featureidkey="properties.LAD21NM",
                                        color_continuous_scale="Viridis",
                                        mapbox_style="carto-positron",
                                        zoom=4,
                                        center={"lat": 55, "lon": 0},
                                        opacity=0.5,
                                        labels={"LA":"Dewlling stock"}
                                        )
            fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
            fig.update_layout(coloraxis_colorbar=dict(tickprefix='1.e'))
            fig.layout.coloraxis.colorbar.title ="log(count of dwelling stock)"
            fig.show()
        
        

The key to making this work well is by resahping the map file so the boundaries are not so well defined. Here I have reduced the filesize from 208 MB to around 6 MB which means it is much faster to load. To achieve this I used Mapshaper which is a free tool for achieving this.


Find me on ...