0

I'm learning Python applied to data science and I'm trying to create an interactive map of my city, Madrid (Spain), showing the last election results. All I'm doing is in this GitHub link:

https://github.com/BernardoJoseLlamasVerna/Madrid_Elections_2021

You will see draft operations and looking for the best way to represent my data on a map:

https://github.com/BernardoJoseLlamasVerna/Madrid_Elections_2021/blob/main/Madrid_Elections_2021.ipynb

I'm following an example found on Internet about Wisconsin elections and fixes perfectly with what I would like to do with my data. I have downloaded it and stored on my repo to show you what I'm trying:

https://github.com/BernardoJoseLlamasVerna/Madrid_Elections_2021/blob/main/maps.ipynb

I've tried to do the same with my data, but nothing appears (even errors). The code is as follows:

from bokeh.io import output_notebook
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import output_notebook, show, output_file
from bokeh.plotting import figure
from bokeh.models import GeoJSONDataSource, LinearColorMapper, ColorBar, HoverTool
from bokeh.palettes import brewer
output_notebook()
import json
# res_w_states["clinton_share"] = res_w_states["clinton"] / res_w_states["total"]

#Convert data to geojson for bokeh
wi_geojson=GeoJSONDataSource(geojson=data.to_json())

color_mapper = LinearColorMapper(palette = brewer['RdBu'][10], low = 0, high = 1)
color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,
                     border_line_color=None,location = (0,0), orientation = 'horizontal')
hover = HoverTool(tooltips = [ ('Municipio','@Municipio'),('P.P.', '@P.P.'),
                               ('P.S.O.E.','@P.S.O.E.'),
                               ('Votos Totales','@Votos Totales')])
p = figure(title="Elecciones Madrid 2021", tools=[hover])
p.patches("xs","ys",source=wi_geojson,
          fill_color = {'field' :'P.P.', 'transform' : color_mapper})
p.add_layout(color_bar, 'below')
show(p)

I've been analysing wi_geojson comparing between mine and what I copied and they seem to follow the same structure.

**QUESTION: ** anyone could give me a hint about what is wrong with my code, data, etc?

Thank you for your help.

P.D.: if anyone could also post a link with better interactive mapping, I would be so glad.

3
  • If you got no error messags I could be possible that you code is working but the output is brocken. To make sure that bokeh works correctly copy one of these examples into you notebook. They should work. If not make sure you have the latest version of bokeh. If you get any errors, please update you post with the traceback.
    – mosc9575
    Commented Jun 19, 2021 at 8:32
  • I used the code you are using with bokeh 2.3.2 and my own geosource. It work well. So the problem is not in this part.
    – mosc9575
    Commented Jun 19, 2021 at 9:10
  • Thank you for answering my question :) I saw I have the following version "BokehJS 1.3.4 successfully loaded.". I'm trying to update my Bokeh version and see if it works(I don't know why my first attempts didn't succeed). I'll be writing back soon. Thank you again. Commented Jun 21, 2021 at 13:11

1 Answer 1

0

Thanks to mosc9575 I could manage a solution...

The problem was the Bokeh version (1.3.4); once updated (2.3.2) I could figure out my map. Now I have to fix the municipalities names XDD.

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.