我想生成Z值之间的梯度的热图,并使用Plotly显示。
我遇到的问题是能够有效地掩盖在没有数据点的“凹形”区域中插值形成的有害数据。
答案 0 :(得分:1)
一种解决方法(也许不是最优雅的方法)是找到点(凹壳)的边界,然后将边界之外的任何内容设置为nan
。
要找到边界,可以使用alphashape并确定grid_z
点是否在边界内(或上),可以使用shapely。
下面是一个在第一个情节剧情开始之前拾取的示例:
from shapely.geometry import Polygon, Point
import alphashape
mpoints = [Point(X, Y) for X, Y in zip(x, y)]
alpha=.125
hull = alphashape.alphashape(mpoints, alpha)
poly = Polygon(hull)
grid_gz = grid_z
gx = np.arange(min(x), max(x),1)
gy = np.arange(min(y), max(y),1)
for i, gxi in enumerate(gx):
for j, gyi in enumerate(gy):
if not np.isnan(grid_gz[j,i]): #UPDATE: no need to test pts that are already NaN
p1 = Point(gxi, gyi)
test = poly.contains(p1) | poly.touches(p1)
if test==False:
grid_gz[j,i]=np.nan
fig = go.Figure()
fig.add_trace(
go.Heatmap(z=grid_gz,x0=min(x),y0=min(y),showscale=True, zsmooth='best',
connectgaps=False, colorscale='Hot'
))
fig.add_trace(
go.Scatter(
x=x,
y=y,mode="markers",marker_size=2,marker_color="black",
))
fig.update_layout(
width = 1200,
height = 1200,
title = "Gradient Heatmap Plot",
yaxis = dict(
scaleanchor = "x",
scaleratio = 1,
))
fig.show()
更多评论: