✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

How to create Congressional Districts in Plotly


I’d like to create a choropleth of New York’s 27 congressional districts. I assumed that this was the same thing as the county choropleth which follows the FIPs numbers, but that is not the case given that there are 62 FIps numbers for New York and only 27 Congressional districts. Does this mean I would have to specify the borders myself? Has anyone done something like this where I could take a look at an example? Thank you!

Hi @thedatasleuth,

The plotly.py ships with the geometry info for all of the US countires, which is the basis for the country choropleth. Supporting congressional districts would require another set of geometry. So there’s no built-in way to do this at this point.


Thanks for this - is there a way to import the congressional geometry?

Hi @thedatasleuth,

There’s nothing built-in right now. It would take a bit of work, but if you’re interested I’d recommend studying the implementation of the county_choropleth figure factory (https://github.com/plotly/plotly.py/blob/master/plotly/figure_factory/_county_choropleth.py) to see how this approach works.

You’re also welcome to open a feature request at https://github.com/plotly/plotly.py/issues. We don’t have any plans to work on this in the near future, but it’s helpful to have a record of what people are interested in doing to inform our future priorities.


1 Like

Can Plotly make it an official Hacktoberfest item? I’d love to see this developed and I might even try to do it myself.

Hi @thedatasleuth, as far as I understand people can contribute to any GitHub repo as part of Hacktoberfest. From https://hacktoberfest.digitalocean.com/

Was there anything you had in mind for making this “an official Hacktoberfest item”?

In any case, the next step would be to open a feature request issue at https://github.com/plotly/plotly.py/issues that describes the use case. Also, include any information you’ve already found regarding where the get the congressional district geometry, and any thoughts you have on the implementation/API. This way we can talk through the high level approach and API before you, or someone else, gets too far into coding it.



Ok, done! Looking forward to next steps!

Cross reference: https://github.com/plotly/plotly.py/issues/1215

1 Like

Hi, I opened this simmilar feature request not that long ago. Could my request tag along on this as well?

Hi @mbkupfer, sure. Feel free to add your references regarding the metro areas to the plotly.py issue above. At this point the county map is only available in plotly.py (not plotly.js) so it would probably be easier to add in to plotly.py than plotly.js at the moment.

And feel free to chime in with your thoughts on the same questions I raised to @thedatasleuth. In particular, what the API could look like and what kind of unique identifiers should be used to specify values for each metro areas (Is there something like FIPS codes for these?)


Hi @jmmease, that sounds good. I didn’t realize that countymaps was only available to plotly.py, but that now makes sense given the extra dependencies. I’m working in dash though so an only python solution would still be fine.

In particular, what the API could look like

Ideally, this would have the same name as the _county_choropleth.create_choropleth() function.

To allow something like this though, fig = ff.create_choropleth(area_codes=area_codes, values=values, ... )
or in @thedatasleuth’s case, fig = ff.create_choropleth(congressional_codes=congressional_codes, values=values, ... ) would require some restructuring to the current code.

To keep backwards compatibility, I’d probably create multiple create_choropleth() functions with some logic that decides whether we create a county, congressional, metro, or whatever type of choropleth figure. How does that sound?

and what kind of unique identifiers should be used to specify values for each metro areas (Is there something like FIPS codes for these?)

Yes, areas do have unique identifiers. Here is a link to the area definition file that is provided by the US Bureau of Labor Statistics: https://www.bls.gov/oes/current/area_definitions_m2017.xlsx Codes are in column D ‘MSA code (including MSA divisions)’

I’ll continue this on the PR thread as well. Really looking forward to getting involved with this!

1 Like