如何用Julia的PlotlyJS绘制带指定坐标的3D曲面图?
用PlotlyJS绘制自定义坐标的$f(x,y)$函数
针对你需要绘制以[x, y, f(x,y)]向量形式给出的函数需求,这里提供两种简洁实现方案:
方案1:直接绘制3D散点图(展示所有指定坐标点)
适合需要呈现每个自定义坐标对应函数值的场景,代码如下:
using PlotlyJS # 替换为你的[x, y, f(x,y)]数据 n_points = 100 x = randn(n_points) # 自定义x坐标集合 y = randn(n_points) # 自定义y坐标集合 z = x.^2 + y.^2 # 对应的f(x,y)值 trace = scatter3d( x=x, y=y, z=z, mode="markers", marker=attr(size=5, color=z, colorscale="Viridis") ) layout = Layout(title="自定义坐标的3D散点图", width=700, height=600) plot(trace, layout)
方案2:插值生成规则网格后绘制曲面图(展示连续曲面)
如果需要呈现连续的曲面效果,可先将离散的自定义坐标插值为规则网格,再用surface绘制:
using PlotlyJS, Interpolations # 替换为你的[x, y, f(x,y)]数据 x = rand(100) * 10 .- 5 # 自定义x范围[-5,5]的离散点 y = rand(100) * 10 .- 5 # 自定义y范围[-5,5]的离散点 z = sin.(sqrt.(x.^2 + y.^2)) # 对应f(x,y)值 # 创建规则网格的坐标轴 x_grid = range(minimum(x), maximum(x), length=50) y_grid = range(minimum(y), maximum(y), length=50) # 插值生成网格对应的z值 interp = interpolate((x, y), z, Gridded(Linear())) z_grid = [interp(xi, yi) for yi in y_grid, xi in x_grid] # 绘制曲面图 trace = surface( x=x_grid, y=y_grid, z=z_grid, colorscale="Viridis" ) layout = Layout(title="插值后的3D曲面图", width=700, height=600) plot(trace, layout)
说明
- 散点图方案无需额外依赖,直接映射原始坐标数据;
- 曲面图方案需要
Interpolations包做插值,若未安装可先运行using Pkg; Pkg.add("Interpolations")。
内容的提问来源于stack exchange,提问作者Alphaharrius
相关产品推荐
相关产品推荐

