County level choropleth in python

im literally tying to recreate this exact example but in python and without using ff.create_choropleth,

the goal is to use this file to plot based on california county FIPS codes: https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv

i want to map the counties in california and i dont want to use ff.create_choropleth instead i wanna return data and layout

has anyone done this before??
thank you

Hi @blksheephw,

Could you explain why you don’t want to use create_choropleth? This function returns a figure object (doesn’t plot it) so you could still get the data and layout values from it.

import plotly.figure_factory as ff
fig = ff.create_choropleth(...)
the_layout = fig.layout
the_data = fig.data

If that doesn’t handle your usecase, your best bet is probably to start with a copy of the figure factory code (See https://github.com/plotly/plotly.py/blob/master/plotly/figure_factory/_county_choropleth.py) and work on making changes from there.

Hope that helps, and good luck!
-Jon

hi @jmmease, thanks for the reply!! i initially didnt want to use create_choropleth because I couldnt find any way to edit the layout but i think i should be able to do it now i think?? i want to change the width/height of the map so i’ll give it a shot … do u happen to know what those parameters are?? i couldnt put them directly in the ff.create_choropleth function as arguments (no error but nothing changed)

for context: i am hoping to return data and layout and use them in a callback to pass tofigure property of a dcc.Graph element

thanks again

Hi @blksheephw,

Here’s how you can modify the width/height

import plotly.figure_factory as ff
fig = ff.create_choropleth(...)
fig.layout.width = 1000
fig.layout.height = 800

Hope that helps!
-Jon

I just starting running into the problem with ff.create_choropleth(… failing with the infinite ‘shell is not a linear ring’ error.

I have searched all around stackoverflow and the community here and I cannot find a solution. I have installed geopandas and pyshp and shapely.

Is there a way to tell if I have good installs for all three of these libraries?

I am just trying to get the geopandas , pyshp and shapely must be installed for this figure factory , right.

the figure_factory has scant documentation on what are the dependencies when using one of the methods. I had to comb through .py source up on github to glean anything.

This is the help(plotly.figure_factory) call result:
Help on package plotly.figure_factory in plotly:

NAME
plotly.figure_factory

PACKAGE CONTENTS
_2d_density
_annotated_heatmap
_bullet
_candlestick
_county_choropleth
_dendrogram
_distplot
_facet_grid
_gantt
_ohlc
_quiver
_scatterplot
_streamline
_table
_ternary_contour
_trisurf
_violin
utils

DATA
absolute_import = _Feature((2, 5, 0, ‘alpha’, 1), (3, 0, 0, ‘alpha’, 0…

Shouldn’t 'create_choropleth() also show up in this list?

Its in the .py file on github - https://github.com/plotly/plotly.py/blob/master/plotly/figure_factory/_county_choropleth.py

    def create_choropleth(fips, values, scope=['usa'], binning_endpoints=None,
                      colorscale=None, order=None, simplify_county=0.02,
                      simplify_state=0.02, asp=None, show_hover=True,
                      show_state_data=True, state_outline=None,
                      county_outline=None, centroid_marker=None,
                      round_legend_values=False, exponent_format=False,
                      legend_title='', **layout_options):

Hi @thoughtsociety,

Is there a way to tell if I have good installs for all three of these libraries?

Do any of the examples at Usa county choropleth maps in Python work for you? If you can reproduce your error on one of these examples then I’d recommend opening a bug report at Issues · plotly/plotly.py · GitHub with the example and info on your python version, and the versions of geopandas , pyshp, and shapely that you have installed.

Shouldn’t 'create_choropleth() also show up in this list?

I don’t know the history of why, but the internal module is named _county_choropleth instead of _choropleth.

-Jon

The installation of geopandas, pyshp and shapely in itself was nettlesome.
I am using Jupyter notebooks and my environment is fully Anaconda on OSX.

I shouldn’t have to use _county_choropleth in the code?
The code is straight from your demo example.

Here are the versions:

geopandas v. 0.4.1
shapely v. 1.6.4
pyshp v. 2.0.1

Everything blows up when it hits:

fig = ff.create_choropleth(fips=fips, values=values)

Thanks

I shouldn’t have to use _county_choropleth in the code?

correct.

The code is straight from your demo example.

As a next step, could you pick out one example and open a bug report at GitHub - plotly/plotly.py: The interactive graphing library for Python This project now includes Plotly Express!? Copy and paste a full example into the report so that we can all be sure we’re working with the exact same code. Add the library versions there, and include the full stack trace of the error. Thanks!