自定义色阶在绘图(Python)中恢复为默认值

2024-07-08 15:21:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难得到一个定制的色阶来显示在美国各州的choropleth图中。z值表示每个状态的值除以所有状态的平均值。下面是一个链接的图像。在

具体地说,我希望色阶spanz的全部范围,但是感知中点(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)']],)

然而,结果如下: Choropleth plot

基本上,颜色比例是标准的。这很奇怪,因为通过调用树传递的绘图HTML包含我未更改的自定义色阶-就好像在呈现时忽略了它一样。在

特别是plotly中的html/脱机.py包含: <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>

如你所见,它定义了正确的色阶。在

这是我的代码明显的问题吗?或者渲染器中有什么东西绕过了我的自定义色阶-例如预期的行为?在

谢谢你的帮助!在


Tags: textdfdataindexplottitlevartype

热门问题