如何使用Plotly Python组合热图和轮廓图

问题描述 投票:1回答:1

我有两个不同的图,一个热图和轮廓图,我想将轮廓放在热图的顶部。每个图的z值对应于不同的数量。

import plotly.graph_objects as go

fig1=go.Figure(go.Heatmap(
    x=data.X,
    y=data.Y,
    z=data0.Z1, zsmooth='best',
    colorscale='Viridis'
), layout=layout)

fig2=go.Figure(go.Contour(
    x=data.X,
    y=data.Y,
    z=round(data.Z2,3),
    zmin=0,
    zmax=0.3,
    contours_coloring='lines',
    line_width=2,
    colorscale=[[0,'blue'],[0.5,'black'],[1,'red']],
    contours=dict(
        start=0, end=0.01, size=1)
), layout=layout)

[在Altair中,我只能使用fig1 + fig2,但这不适用于Plotly。不幸的是,Altair没有任何轮廓功能。如果Plotly无法做到这一点,可以使用matplotlib来实现吗?

编辑:这是一个数据示例

ls = [[-0.1,-3.0,-0.5,0.0],[-0.1,-2.0,-0.25,0.2],[-0.1,-1.0,-0.15,0.25],[-0.1,0.0,0,0.3],[-0.1,1.0,0.1,0.25],[-0.1,2.0,0.25,0.2],[-0.1,3.0,0.5,0.0],[0.0,-3.0,-0.5,0.0],[0.0,-2.0,-0.25,0.1],[0.0,-1.0,-0.15,0.11],[0.0,0.0,0.0,0.1],[0.0,1.0,0.1,0.0],[0.0,2.0,0.25,0.0],[0.0,3.0,0.5,0.0],[0.1,-3.0,-0.5,0.0],[0.1,-2.0,-0.5,0.12],[0.1,-1.0,-0.15,0.12],[0.1,0.0,0.0,0.13],[0.1,1.0,0.1,0.12],[0.1,2.0,0.5,0.12],[0.1,3.0,0.5,0.0]]

data = pd.DataFrame(ls, columns=['X', 'Y', 'Z1', 'Z2'])
python plotly heatmap contour
1个回答
0
投票

给出您的示例,将轮廓放在所需的热图上方:

fig1.add_trace(fig2.data[0])
© www.soinside.com 2019 - 2024. All rights reserved.