top of page
Search

Highlighting Click Data on Plotly Choropleth Map

  • Writer: Ivan Lai
    Ivan Lai
  • Oct 15, 2020
  • 1 min read


In making the UK Houseprice Plotly App, one challenge was to high light the clicked part of the choropleth map. Such functionality is not available at the time of writing, so here is a way to get round it.


I will modify the election-choropleth example in the plotly document for illustrations. The complete code is available here as a Google Colab notebook.


The basic idea is to add a second layer with only the selected districts in the corresponding geojson, and keeping track of the districts that has been clicked or unclicked.


GeoJSON is an open standard geospatial data interchange format. For our geojson file, we want to select the geojson['features'] element that contains the selected districts. To speed up the lookup process, we will first get a district_lookup dictionary:



Then we write a function to get the geojson file just for the selected districts:



Given a list of selections, now we can use the function get_highlights() in the second layer of the figure for the highlighting effect, where we increased the opacity for the selection from 0.5 to 1.0. We could also highlight the clicked districts in other ways just by specifying the desired format for the second layer.



Now we are ready to run the app. The dash_core_components.graph component has a property "clickData" - when there is a click on the map, the event will trigger the callback function and update the set "selections" and the figure.




Hope you find this little trick helpful, and good luck with your project!


 
 
 

2 Comments


giecphangqua.n.h.g.h.u.n.g
3 days ago

Mình mua vé số chủ yếu cho vui thôi, kiểu có cái để hóng kết quả mỗi ngày chứ không đặt nặng chuyện phải trúng. Trước đây nghe người ta bàn về “cầu” này “cầu” kia mình cũng nửa tin nửa ngờ, nhưng theo dõi một thời gian thì đúng là đôi lúc thấy vài dạng số lặp lại khiến mình chú ý. Mỗi lần đọc mấy bài dự đoán, mình hay ghi lại vài dòng rồi vài hôm sau mở ra so lại, để xem có phải mình tự tưởng tượng ra quy luật không. Dạo gần đây thỉnh thoảng mình ghé https://soicauxsmb.io/so-dep-hom-nay.html cho có thêm chuyện nói với bạn bè. Có hôm trúng chút thì vui, còn trật…

Like

nolafo.wle156+abc123
Aug 07

Mình thỉnh thoảng cũng xem soi cầu cho vui thôi, kiểu lấy làm tài liệu tham khảo để tự tập nhìn dàn số và đối chiếu lại cảm giác của mình. Hồi trước hay nghe người quen mách nước rồi ghi bừa, trượt cái là khó chịu cả ngày, nên giờ mình ưu tiên đọc mấy bài có lập luận rõ ràng hơn; đúng sai gì thì ít nhất cũng học được cách họ suy nghĩ. Có dạo lướt thấy https://soicauxsmb.pro/ nhắc chuyện chọn lọc theo chu kỳ, mình thử làm theo hướng “gạn bớt” chứ không đặt niềm tin tuyệt đối, thấy tâm lý đỡ bị kéo theo cảm xúc hơn. Với mình mấy thứ này chỉ nên coi…

Like
bottom of page