【发布时间】:2020-05-31 13:55:05
【问题描述】:
我想生成 Z 值之间的梯度热图并使用 Plotly 显示。
我遇到的问题是能够有效地掩盖在没有数据点的“凹”区域中插值形成的不需要的数据。
【问题讨论】:
标签: python numpy matplotlib scipy plotly
我想生成 Z 值之间的梯度热图并使用 Plotly 显示。
我遇到的问题是能够有效地掩盖在没有数据点的“凹”区域中插值形成的不需要的数据。
【问题讨论】:
标签: python numpy matplotlib scipy plotly
一种解决方案(可能不是最优雅的)是找到点的边界(凹壳),然后将此边界之外的任何内容设置为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()
更多评论:
【讨论】:
grid_z 结果来改进。