Plotly Heatmap - Python - 添加颜色条

时间:2017-08-01 09:21:56

标签: python plotly

我有一个从数据框生成的热图。不幸的是,它没有颜色条。我试过添加一个,但它没有显示。有什么想法吗?

这是代码:

zp = df_hml.values.tolist()
zp.reverse()
z = zp
z_text = np.around(z) 

x = [threeYr,twoYr,oneYr,Yr]
y=['March', 'February', 'January', 'December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April']


layout = go.Layout(
    autosize=False,
    width=700,
    height=450,
    margin=go.Margin(
        l=150,
        r=160,
        b=50,
        t=100,
        pad=3
    ),
)
colorscale = [[0, '#454D59'],[0.5, '#FFFFFF'], [1, '#F1C40F']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text,colorscale=colorscale,  hoverinfo='z')

# Make text size smaller
for i in range(len(fig.layout.annotations)):
        fig.layout.annotations[i].font.size = 9

fig.layout.title = '<b>Research - HEATMAP</b><br>Applications'
fig.layout.titlefont.size = 14

fig.layout.width = 700
fig.layout.height = 550 
plotly.offline.iplot(fig, config={"displayModeBar": False}, show_link=False, filename='annotated_heatmap_numpy')

产生这个:

enter image description here

1 个答案:

答案 0 :(得分:2)

create_annotated_heatmap返回的对象实际上只是一个JSON。您可以使用print(fig)检查所有参数。只需设置fig['data'][0]['showscale'] = True,就会出现比例尺。

此外,您应将colorscale设置为

colorscale = [[0, '#454D59'],
              [0.33, '#454D59'],
              [0.33, '#FFFFFF'],
              [0.66, '#FFFFFF'],
              [0.66, '#F1C40F'], 
              [1, '#F1C40F']]

 [[lower bound, color1], [upper bound, color1],
  [lower bound, color2], [upper bound, color2],

对于离散颜色条,两种不同颜色的上限和下限应该相同。