[英]Create a 3D surface plot in Plotly
I want to create a 3D surface plot in Plotly by reading the data from an external file.我想通过从外部文件读取数据在 Plotly 中创建一个 3D 曲面图。 Following is the code I am using:以下是我正在使用的代码:
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
data = np.genfromtxt('values.dat', dtype=float)
# The shape of X, Y and Z is (10,1)
X = data[:,0:1]
Y = data[:,1:2]
Z = data[:,2:3]
fig = go.Surface(x=X, y=Y, z=Z, name='Surface plot', colorscale=px.colors.sequential.Plotly3)
plot(fig)
The above code does not produce any surface plot.上面的代码不会产生任何曲面图。 What changes has to be made to create a surface plot?必须进行哪些更改才能创建曲面图?
From plotly figure reference:从情节图参考:
The data the describes the coordinates of the surface is set in z
.描述表面坐标的数据设置在z
。 Data in z
should be a 2D list. z
数据应该是一个二维列表。 Coordinates in x
and y
can either be 1D lists or {2D arrays} x
和y
坐标可以是一维列表或 {2D 数组}
I have an example data set.我有一个示例数据集。 It contains three columns (x,y,z).它包含三列 (x,y,z)。
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.interpolate import griddata
df = pd.read_csv('./test_data.csv')
x = np.array(df.lon)
y = np.array(df.lat)
z = np.array(df.value)
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X,Y = np.meshgrid(xi,yi)
Z = griddata((x,y),z,(X,Y), method='cubic')
fig = go.Figure(go.Surface(x=xi,y=yi,z=Z))
fig.show()
Reference Page: https://plotly.com/python/reference/surface/参考页面: https : //plotly.com/python/reference/surface/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.