gpt4 book ai didi

python - 将 3D Matplot 保存为交互式 HTML

转载 作者:行者123 更新时间:2023-12-05 04:25:11 26 4
gpt4 key购买 nike

我正在尝试将我的 3D 三曲面图保存为交互式 HTML 图,因此应该可以放大/缩小并更改视点。在 IDE 中, plotly 已经存在并且到目前为止有效,但我由于 ValueError,无法将其保存为 HTML 格式:“fig 参数必须是字典或图形。收到类型 的值:Figure(1600x900)”。我不明白为什么“ ”不是图形?

这是我的方法:https://plotly.com/python/interactive-html-export/我已经用 go.Figure() ( Export rotable 3D plots from Python to HTML ) 试过了,但它不适用于 trisurf。

有没有办法保留我的绘图设置(按原样使用 trisurf)并获得 HTML 中的交互式图形?

非常感谢您的回答

#Import libraries 
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np
from mpl_toolkits.mplot3d import axes3d, Axes3D
import pandas as pd
import plotly.express as px
import io
import plotly.io as pio
%matplotlib notebook


E = np.arange(225)
D = np.arange(225)
A = np.arange(225)

E = [10000.0, 10000.0, ...]
D = [500.0, 1000.0, ...]
A = [1.9495, 1.9644, ...]


#Create figure
fig = plt.figure(figsize =(16, 9))
ax = plt.axes(projection ='3d')

# Creating color map
my_cmap = plt.get_cmap('hot')

# Data for three-dimensional scattered points
zdata = A
xdata = D
ydata = E

# Creating plot
trisurf = ax.plot_trisurf(xdata, ydata, zdata,
cmap = my_cmap,
linewidth = 0.2,
antialiased = True,
edgecolor = 'grey')
fig.colorbar(trisurf, ax = ax, shrink = 0.5, aspect = 10)
ax.set_title('AIE_SIM0.003__lowE_10000_upE_460000_stepE_30000_lowD_500.0_upD_8000.0_stepD_500.0')

ax.set_xlabel('Damping Ns/m')
ax.set_ylabel('Stifness N/m')
ax.set_zlabel('Amplification')


A2 = np.arange(225)

A2.fill(20.757)


# Creating color map
my_cmap2 = plt.get_cmap('gray')

# Data for three-dimensional scattered points
zdata2 = A2
xdata = D
ydata = E

# Creating plot
trisurf2 = ax.plot_trisurf(xdata, ydata, zdata2,
cmap = my_cmap2,
linewidth = 0.2,
antialiased = False,
edgecolor = 'none', alpha = 0.2)
fig.colorbar(trisurf2, ax = ax, shrink = 0.5, aspect = 10)
print(type(fig))
#fig.write_html("file.html")
plotly.io.to_html(fig=fig)

fig.savefig('3D_Plot_PNG_lowE_10000_upE_460000_stepE_30000_lowD_500.0_upD_8000.0_stepD_500.0.png')
fig.show()



------------------------------------------------------------------------------------------
Figure 1

printed: <class 'matplotlib.figure.Figure'>


ValueError:
The fig parameter must be a dict or Figure.
Received value of type <class 'matplotlib.figure.Figure'>: Figure(1600x900)

最佳答案

据我所知,Matplotlib 无法生成 3D html 图。

另外,你上面的尝试是错误的。该错误消息告诉您 Plotly 的 to_html 仅适用于 Plotly 的 Figure。所以混合使用 Plotly 和 Matplotlib 是行不通的。您需要创建一个 Plotly 图。

此外,我不认为 Plotly 公开了类似于 Matplotlib 的 plot_trisurf 的内容。但是,它公开了 go.Mesh,使我们能够获得相同的结果。

配方:

  • 生成您的数值数据。
  • 创建三角测量。我们将在这部分使用 Matplotlib 的 Triangulation 类。
  • 创建 Plotly 图并添加表面。
  • 将图形导出为 html。

在这里,我将发布一个示例来指导您:

import numpy as np
import matplotlib.tri as mtri
import plotly.graph_objects as go

### DATA GENERATION
# Make parameter spaces radii and angles.
n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += np.pi/n_angles

# Map radius, angle pairs to x, y, z points.
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
z = (np.cos(radii)*np.cos(3*angles)).flatten()

### TRIANGULATION
# Create the Triangulation; no triangles so Delaunay triangulation created.
triang = mtri.Triangulation(x, y)

# Mask off unwanted triangles.
xmid = x[triang.triangles].mean(axis=1)
ymid = y[triang.triangles].mean(axis=1)
mask = xmid**2 + ymid**2 < min_radius**2
triangles = triang.triangles[~mask]

### PLOT
fig = go.Figure(data=[
# go.Mesh allows to provide the triangulation
go.Mesh3d(
x=x, y=y, z=z,
colorbar_title='z',
colorscale="aggrnyl",
# Intensity of each vertex, which will be interpolated and color-coded
intensity =z,
# i, j and k give the vertices of triangles
i = triangles[:, 0],
j = triangles[:, 1],
k = triangles[:, 2],
showscale=True
)
])

fig.show()

### EXPORT TO HTML
# Please, execute `help(fig.write_html)` to learn about all the
# available keyword arguments to control the output
fig.write_html("test.html", include_plotlyjs=True, full_html=True)

关于python - 将 3D Matplot 保存为交互式 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73327484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com