Search code examples
pythonplotlygeojsonchoroplethplotly-express

Plotly choropleth not displaying regions properly


I am trying to use plotly to display a chropleth map of Spain.

This is the code of my attempt:

import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import numpy as np
import geopandas as gpd

geo_df = gpd.read_file("cc_aa.geojson") # Read geojson file with geopandas
geo_df=geo_df[geo_df["acom_code"]!="20"] # Filter region that is not needed

region_names =["MELILLA", "MADRID", "CATALUNA", "CEUTA ", "ANDALUCIA", "ISLAS BALEARES",
                      "ISLAS CANARIAS", "EXTREMADURA", "REGION DE MURCIA", "COMUNIDAD VALENCIANA",
                      "LA RIOJA", "CASTILLA Y LEON", "ARAGON", "GALICIA", "COMUNIDAD FORAL DE NAVARRA",
                      "PRINCIPADO DE ASTURIAS", "CASTILLA-LA MANCHA", "CANTABRIA",
                      "PAIS VASCO"]
geo_df["acom_name"]= region_names # rename region names
df = pd.DataFrame([(r,) for r in region_names], columns = ["acom_name"]) # Create a pandas dataframe with the regions
np.random.seed(1)                                   
df["random"] = np.random.rand(19) # Add a random column which will be used in the chropleth map to set the color
print(len(df)) #19
geo_df = geo_df.merge(df, on="acom_name").set_index("acom_name") # merge the geopandas df with the pandas df
print(len(geo_df)) # 19
fig = px.choropleth(geo_df,
                   geojson=geo_df.geometry,color = "random",
                   locations=geo_df.index)
fig.update_geos(fitbounds="geojson", visible=True)
fig.write_html("testing_map.html")

This is what the figure looks like:enter image description here Only one region (PAIS VASCO) is correctly plotted, which is the last row of the dataframe coincidentally. I can hover over the other regions and I can see the region name and their values though.

The geojson data should be correct, as I can make the plot with geopandas directly with no problems. This is the code:

  fig, ax = plt.subplots(1, 1)
  geo_df.plot(column='random',ax=ax, legend=True)

And the resulting figure: enter image description here

The geojson was downloaded from opendatasoft.


Solution

  • It would seem there was something wrong with the geojson I was using before (I did not think that to be the case because plotting with geopandas worked correctly).

    This is how the plot with the dataset downloaded from here looks like:

    enter image description here