我无法在美国各州的等值线情节中获得自定义色阶以在Plotly中显示。 z
值表示每个州的值除以所有州的平均值。图片链接如下。
具体来说,我希望色阶到span
z
的整个范围,但是感知中点(RdBu刻度上的黄色)始终位于1(数据的平均值)。我希望捕获所有动态范围,同时在人口较多(例如约1个)状态集中保持更密集的颜色信息。
我的代码如下所示(尝试忽略上下文位):
def plot_states(sms_range, var='normalized Basic vs DOT', norm=False, timeupper = '2018-10-24',
timelower = '2010-01-01',
title = 'Test Figure (Hover for breakdown)'):
df = sms_range[sms_range.index.get_level_values(1) <=
pd.to_datetime(timeupper)].sort_index()
df = df[df.index.get_level_values(1) >= pd.to_datetime(timelower)].sort_index()
dfmean = df.mean(level=0)
if norm:
totalmean = df.median()
plot_z = dfmean[var]/(totalmean[var])
else:
plot_z = dfmean[var].astype(float)
dfmean['code'] = stateabbreviations[:,1]
minimum = plot_z.min()
maximum = plot_z.max()
onepointlow = ((1. - minimum) / (maximum - minimum))*0.9
onepointhigh = ((1. - minimum) / (maximum - minimum))*1.1
colorscale = [[0.0, 'rgb(49,54,149)'], # Blue
[onepointlow/2., 'rgb(116,173,209)'],
[onepointlow, 'rgb(224,243,248)'], # Yellow
[onepointhigh, 'rgb(254,224,144)'], # Yellow
[(onepointhigh+1.)/2., 'rgb(244,109,67)'],
[1.0, 'rgb(165,0,38)']], # Red
print(colorscale)
dfmean['text'] = dfmean['code']
data = [dict(
type='choropleth',
colorscale=colorscale,
autocolorscale=False,
locations=dfmean['code'],
z=plot_z,
locationmode='USA-states',
text=dfmean['text'],
marker=dict(
line=dict(
color='rgb(255,255,255)',
width=2
)),
colorbar=dict(
title=var)
)]
print(data)
layout = dict(
title=title,
geo=dict(
scope='usa',
projection=dict(type='albers usa'),
showlakes=True,
lakecolor='rgb(255, 255, 255)'),
)
fig = dict(data=data, layout=layout)
plot(fig, filename='d3-cloropleth-map')
这里的关键是这个函数的一次运行,
colorscale = ([[0.0, 'rgb(49,54,149)'], [0.0400, 'rgb(116,173,209)'], [0.0801, 'rgb(224,243,248)'], [0.0979, 'rgb(254,224,144)'], [0.5489, 'rgb(244,109,67)'], [1.0, 'rgb(165,0,38)']],)
基本上,色阶是默认的标准&#39; RdBu&#34; colorscale。这很奇怪,因为通过调用树传递的Plotly HTML包含我自定义的colorcale不变 - 它就像在渲染时被忽略一样。
具体来说,plotly / offline.py中的plotly_html包含:
<div id="54d14a55-4470-4e77-908a-ed1aad3efd68" style="height: 100%; width: 100%;" class="plotly-graph-div"></div><script type="text/javascript">window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL="https://plot.ly";Plotly.newPlot("54d14a55-4470-4e77-908a-ed1aad3efd68", [{"type": "choropleth", "autocolorscale": false, "text": ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"], "colorbar": {"title": "Drugs/ Alcohol\u2217_norm"}, "locationmode": "USA-states", "z": [0.9574031223360088, 4.663833598074362, 0.7470654042509098, 0.45790667712096444, 0.7139322298296459, 1.215065339824984, 0.9474219012849346, 0.7353880993782959, 2.810812736720189, 0.6462863936749693, 0.9986899212581901, 6.548052464708055, 1.0090737091057922, 0.49457016318780755, 0.695353446901323, 0.9419458477900825, 0.8293205959460278, 0.7351136201837116, 1.6921332609878887, 1.9066962190394505, 0.8032201503740584, 1.572393730339865, 1.0845158832537063, 0.9784209575362792, 0.9652909590698142, 0.6196209448011111, 1.4865809187853425, 0.6422023875155145, 1.0212118831303278, 1.7328143913690517, 0.7946163933668561, 0.716044064254416, 1.0120916518088283, 0.9151593810825868, 2.046814218440964, 1.1652449211545537, 0.8550939805838285, 1.497308945837465, 0.6778640418859166, 0.9013539653339065, 1.6812599294250667, 1.1817300995782374, 1.0516933417009253, 0.720853315460788, 1.1375338872234784, 1.5900074550270746, 1.1047246521920622, 1.1058951057861155, 1.0357326707814427, 1.208933943847189, 1.3533084199230396], "colorscale": [[[0.0, "rgb(49,54,149)"], [0.04005519798766846, "rgb(116,173,209)"], [0.08011039597533692, "rgb(224,243,248)"], [0.09791270619207847, "rgb(254,224,144)"], [0.5489563530960392, "rgb(244,109,67)"], [1.0, "rgb(165,0,38)"]]], "locations": ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"], "marker": {"line": {"width": 2, "color": "rgb(255,255,255)"}}}], {"title": "D/A Compliance, normalized 2017", "geo": {"showlakes": true, "scope": "usa", "lakecolor": "rgb(255, 255, 255)", "projection": {"type": "albers usa"}}}, {"linkText": "Export to plot.ly", "showLink": true})</script>
正如您所看到的,定义了正确的色阶。
这是我的代码的明显问题吗?或者渲染器中有什么东西绕过我的自定义色阶 - 例如预期的行为?
感谢您的帮助!