@@ -478,8 +478,8 @@ def create_choropleth(
478478 :param **layout_options: a **kwargs argument for all layout parameters
479479
480480
481- Example 1: Florida::
482-
481+ Example 1: Florida::
482+
483483 import plotly.plotly as py
484484 import plotly.figure_factory as ff
485485
@@ -506,106 +506,99 @@ def create_choropleth(
506506 exponent_format=True,
507507 )
508508
509- Example 2: New England
510- ```
511- import plotly.plotly as py
512- import plotly.figure_factory as ff
509+ Example 2: New England::
513510
514- import pandas as pd
511+ import plotly.figure_factory as ff
515512
516- NE_states = ['Connecticut', 'Maine', 'Massachusetts',
517- 'New Hampshire', 'Rhode Island']
518- df_sample = pd.read_csv(
519- 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
520- )
521- df_sample_r = df_sample[df_sample['STNAME'].isin(NE_states)]
522- colorscale = ['rgb(68.0, 1.0, 84.0)',
523- 'rgb(66.0, 64.0, 134.0)',
524- 'rgb(38.0, 130.0, 142.0)',
525- 'rgb(63.0, 188.0, 115.0)',
526- 'rgb(216.0, 226.0, 25.0)']
527-
528- values = df_sample_r['TOT_POP'].tolist()
529- fips = df_sample_r['FIPS'].tolist()
530- fig = ff.create_choropleth(
531- fips=fips, values=values, scope=NE_states, show_state_data=True
532- )
533- py.iplot(fig, filename='choropleth_new_england')
534- ```
513+ import pandas as pd
535514
536- Example 3: California and Surrounding States
537- ```
538- import plotly.plotly as py
539- import plotly.figure_factory as ff
515+ NE_states = ['Connecticut', 'Maine', 'Massachusetts',
516+ 'New Hampshire', 'Rhode Island']
517+ df_sample = pd.read_csv(
518+ 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
519+ )
520+ df_sample_r = df_sample[df_sample['STNAME'].isin(NE_states)]
521+ colorscale = ['rgb(68.0, 1.0, 84.0)',
522+ 'rgb(66.0, 64.0, 134.0)',
523+ 'rgb(38.0, 130.0, 142.0)',
524+ 'rgb(63.0, 188.0, 115.0)',
525+ 'rgb(216.0, 226.0, 25.0)']
540526
541- import pandas as pd
527+ values = df_sample_r['TOT_POP'].tolist()
528+ fips = df_sample_r['FIPS'].tolist()
529+ fig = ff.create_choropleth(
530+ fips=fips, values=values, scope=NE_states, show_state_data=True
531+ )
532+ fig.show()
542533
543- df_sample = pd.read_csv(
544- 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
545- )
546- df_sample_r = df_sample[df_sample['STNAME'] == 'California']
547-
548- values = df_sample_r['TOT_POP'].tolist()
549- fips = df_sample_r['FIPS'].tolist()
550-
551- colorscale = [
552- 'rgb(193, 193, 193)',
553- 'rgb(239,239,239)',
554- 'rgb(195, 196, 222)',
555- 'rgb(144,148,194)',
556- 'rgb(101,104,168)',
557- 'rgb(65, 53, 132)'
558- ]
534+ Example 3: California and Surrounding States::
559535
560- fig = ff.create_choropleth(
561- fips=fips, values=values, colorscale=colorscale,
562- scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],
563- binning_endpoints=[14348, 63983, 134827, 426762, 2081313],
564- county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
565- legend_title='California Counties',
566- title='California and Nearby States'
567- )
568- py.iplot(fig, filename='choropleth_california_and_surr_states_outlines')
569- ```
536+ import plotly.figure_factory as ff
570537
571- Example 4: USA
572- ```
573- import plotly.plotly as py
574- import plotly.figure_factory as ff
538+ import pandas as pd
575539
576- import numpy as np
577- import pandas as pd
540+ df_sample = pd.read_csv(
541+ 'https://raw.githubusercontent.com/plotly/datasets/master/minoritymajority.csv'
542+ )
543+ df_sample_r = df_sample[df_sample['STNAME'] == 'California']
578544
579- df_sample = pd.read_csv(
580- 'https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv'
581- )
582- df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(
583- lambda x: str(x).zfill(2)
584- )
585- df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(
586- lambda x: str(x).zfill(3)
587- )
588- df_sample['FIPS'] = (
589- df_sample['State FIPS Code'] + df_sample['County FIPS Code']
590- )
545+ values = df_sample_r['TOT_POP'].tolist()
546+ fips = df_sample_r['FIPS'].tolist()
591547
592- binning_endpoints = list(np.linspace(1, 12, len(colorscale) - 1))
593- colorscale = ["#f7fbff", "#ebf3fb", "#deebf7", "#d2e3f3", "#c6dbef",
594- "#b3d2e9", "#9ecae1", "#85bcdb", "#6baed6", "#57a0ce",
595- "#4292c6", "#3082be", "#2171b5", "#1361a9", "#08519c",
596- "#0b4083","#08306b"]
597- fips = df_sample['FIPS']
598- values = df_sample['Unemployment Rate (%)']
599- fig = ff.create_choropleth(
600- fips=fips, values=values, scope=['usa'],
601- binning_endpoints=binning_endpoints, colorscale=colorscale,
602- show_hover=True, centroid_marker={'opacity': 0},
603- asp=2.9, title='USA by Unemployment %',
604- legend_title='Unemployment %'
605- )
548+ colorscale = [
549+ 'rgb(193, 193, 193)',
550+ 'rgb(239,239,239)',
551+ 'rgb(195, 196, 222)',
552+ 'rgb(144,148,194)',
553+ 'rgb(101,104,168)',
554+ 'rgb(65, 53, 132)'
555+ ]
556+
557+ fig = ff.create_choropleth(
558+ fips=fips, values=values, colorscale=colorscale,
559+ scope=['CA', 'AZ', 'Nevada', 'Oregon', ' Idaho'],
560+ binning_endpoints=[14348, 63983, 134827, 426762, 2081313],
561+ county_outline={'color': 'rgb(255,255,255)', 'width': 0.5},
562+ legend_title='California Counties',
563+ title='California and Nearby States'
564+ )
565+ fig.show()
566+
567+ Example 4: USA::
568+
569+ import plotly.figure_factory as ff
606570
607- py.iplot(fig, filename='choropleth_full_usa')
608- ```
571+ import numpy as np
572+ import pandas as pd
573+
574+ df_sample = pd.read_csv(
575+ 'https://raw.githubusercontent.com/plotly/datasets/master/laucnty16.csv'
576+ )
577+ df_sample['State FIPS Code'] = df_sample['State FIPS Code'].apply(
578+ lambda x: str(x).zfill(2)
579+ )
580+ df_sample['County FIPS Code'] = df_sample['County FIPS Code'].apply(
581+ lambda x: str(x).zfill(3)
582+ )
583+ df_sample['FIPS'] = (
584+ df_sample['State FIPS Code'] + df_sample['County FIPS Code']
585+ )
586+
587+ binning_endpoints = list(np.linspace(1, 12, len(colorscale) - 1))
588+ colorscale = ["#f7fbff", "#ebf3fb", "#deebf7", "#d2e3f3", "#c6dbef",
589+ "#b3d2e9", "#9ecae1", "#85bcdb", "#6baed6", "#57a0ce",
590+ "#4292c6", "#3082be", "#2171b5", "#1361a9", "#08519c",
591+ "#0b4083","#08306b"]
592+ fips = df_sample['FIPS']
593+ values = df_sample['Unemployment Rate (%)']
594+ fig = ff.create_choropleth(
595+ fips=fips, values=values, scope=['usa'],
596+ binning_endpoints=binning_endpoints, colorscale=colorscale,
597+ show_hover=True, centroid_marker={'opacity': 0},
598+ asp=2.9, title='USA by Unemployment %',
599+ legend_title='Unemployment %'
600+ )
601+ fig.show()
609602 """
610603 # ensure optional modules imported
611604 if not _plotly_geo :
0 commit comments