如何使用Plotly Python组合热图和轮廓图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Plotly Python组合热图和轮廓图相关的知识,希望对你有一定的参考价值。
我有两个不同的图,一个热图和轮廓图,我想将轮廓放在热图的顶部。每个图的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'])
答案
给出您的示例,将轮廓放在所需的热图上方:
fig1.add_trace(fig2.data[0])
以上是关于如何使用Plotly Python组合热图和轮廓图的主要内容,如果未能解决你的问题,请参考以下文章