编辑plotly.figure_factory.create_choropleth的图例标签

时间:2018-08-08 01:09:52

标签: python-3.x plotly legend labels

我使用plotly.figure_factory.create_choropleth创建了以下图:

enter image description here

我要为最后两个元素更改图例的标签,如下所示:

  • “ <-1” ==>“缺少”
  • “-1-0” ==> 0

如果可能的话,我也希望更改悬停值,如上所述。

有没有办法做到这一点?我在下面包括了我的代码以供参考。

任何帮助将不胜感激。

# read in data
merged_data = pd.read_csv('merged_data.csv')

# pad fips_padded_zeros columns with zeros in front
merged_data['fips_padded_zeros'] = merged_data.fips_code.apply(lambda x: str(x).zfill(5))

# colors to use for plotly plot
colorscale = ["#000000", "#FFFFFF","#F6F1F1","#EEE4E4","#E6D6D6","#DEC9C9","#D6BBBB","#CEAEAE",
              "#C6A1A1","#BE9393","#B68686","#AE7878","#A66B6B","#9E5D5D","#965050",
              "#8E4343","#863535","#7E2828", "#761A1A", "#6E0D0D", "#660000"]

# color gradient endpts
endpts = [[-1], [0]]
endpts.append(list(np.linspace(1, 90, len(colorscale) - 3)))
endpts = [endpt for elem in endpts for endpt in elem] # flatten list of lists

fips_padded_zeros = merged_data['fips_padded_zeros'].tolist()
values = merged_data['crude_opioid_mortality_rate'].tolist()

# plotly plot
fig = ff.create_choropleth(
    fips=fips_padded_zeros, values=values,
    binning_endpoints=endpts,
    colorscale=colorscale,
    show_state_data=True,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, title='USA by Opioid Overdose Mortality Rate, 2016',
    legend_title='Mortality Rate (per 100,000)', scope = ['usa']
)
py.iplot(fig, filename='choropleth_full_usa')

0 个答案:

没有答案