简体   繁体   中英

How to mark an area in plotly 3D surface plot?

I use plotly to create a 3D elevation profile from xyz data which works pretty well with the following code:

import plotly.graph_objects as go
import pandas as pd
import numpy as np

# Read data
contour_data = pd.read_csv(r"C:\Elevation.xyz", delimiter=' ', names=["x","y","z"])
print(contour_data.head())

# Create 2D grids for X,Y and Z
Z = contour_data.pivot_table(index='x', columns='y', values='z').T.values
X_unique = np.sort(contour_data.x.unique())
Y_unique = np.sort(contour_data.y.unique())
X, Y = np.meshgrid(X_unique, Y_unique)

# Generate 3D plot
fig = go.Figure(data=[go.Surface(z=Z,x=X_unique,y=Y_unique)])
fig.update_layout(title='Elevation', autosize=True, margin=dict(l=65, r=50, b=65, t=90))
fig.update_layout(scene=dict(aspectratio=dict(x=2, y=2, z=0.4)))
fig.show(renderer="browser")

3D 高程图

Now I want to mark an area on this surface as in this example . Alternatively just the border of this area would be nice.

Is there a way to mark this area by just providing some x,y coordinates?

Thank you @vestland for suggesting a solution. Starting from your approach I have implemented a solution that is working with non-rectangle areas by just defining a polygon with x,y coordinates.

带有标记多边形区域的高程数据

import matplotlib.pyplot as plt
from matplotlib import rcParams
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from shapely.geometry import Point, Polygon

# Read data
contour_data = pd.read_csv(r"C:\Elevation.xyz", delimiter=' ', names=["x","y","z"])

# Create 2D grids for X,Y and Z
# https://alex.miller.im/posts/contour-plots-in-python-matplotlib-x-y-z/
Z = contour_data.pivot_table(index='x', columns='y', values='z').T
X_unique = np.sort(contour_data.x.unique())
Y_unique = np.sort(contour_data.y.unique())
X, Y = np.meshgrid(X_unique, Y_unique)

# Generate 3D plot
# https://www.geodose.com/2019/09/3d-terrain-modelling-in-python.html
# https://plotly.com/python/3d-surface-plots/ 
fig = go.Figure(data=go.Surface(z=Z,x=X_unique,y=Y_unique))
fig.update_layout(scene = dict(
        xaxis = dict(title='x Longitude',dtick=0.005),
        yaxis = dict(title='y Latitude',dtick=0.005),
        zaxis = dict(title='z Elevation',range=[100, 400])))
fig.update_layout(title='Elevation',autosize=True, margin=dict(l=65, r=50, b=65, t=90))
fig.update_layout(scene=dict(aspectratio=dict(x=2, y=2, z=0.3)))

# Create a Polygon
coords = [(9.185, 51.39), (9.175, 51.39), (9.175, 51.4), (9.2, 51.395)]
poly = Polygon(coords)

marked_area=Z.copy()
i=0
for x in X_unique:
    j=0
    for z in Z.iloc[i]:
        if (Point(x,Y_unique[j]).within(poly)):
            marked_area.iloc[i,j]=z+0.1
        else:
            marked_area.iloc[i,j]=0
        j=j+1
    i=i+1
    
fig.add_trace(go.Surface(z=marked_area,x=X_unique,y=Y_unique,
                         colorscale = ['rgba(0,0,250,1)', 'rgba(0,0,250,1)'],
                         colorbar = None,showlegend=False))

fig.show(renderer="browser")

Since you haven't provided a sample of your data, I'm basing an initial suggestion on the example from Topographical 3D Surface Plot . This might need some additional tweaking, but you can use fig.add_trace(go.Scatter3D) to highlight a subset of your coordinates like this:

在此处输入图像描述

Let me know how this works out for you and we can take a closer look at the details.

Complete code:

import plotly.graph_objects as go

import pandas as pd

# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')

fig = go.Figure(data=[go.Surface(z=z_data.values)])

fig.update_layout(title='Mt Bruno Elevation', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))
df2 = z_data.iloc[8:15, 7:21]
fig.add_trace(go.Surface(x = df2.columns, y = df2.index, z=df2.values,
                         colorscale = ['rgba(250,0,0,0.8)', 'rgba(250,0,0,0.8)'],
                         colorbar = None))

fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM