我正在try 将我的3D三面图保存为交互式的HTML图形,因此应该可以放大/缩小和更改视点.在IDE中,到目前为止,该图已经存在并且工作正常,但我 由于ValueError,无法将其保存为HTML格式: "Figure参数必须是字典或数字. 收到的值类型为<class‘matplotlib.figure.Figure’>:Figure(1600x900)". 我不明白为什么"<class‘matplotlib.figure.figure’>"不是一个数字?

这就是我的做法:https://plotly.com/python/interactive-html-export/ 我已经用Gotry 过了.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绘图.

此外,你上面的try 是错误的.该错误消息告诉您,普洛特利的to_html只适用于普洛特利的Figure.因此,将Ploly和Matplotlib混合在一起是行不通的.您需要创建一个Ploly图形.

此外,我不认为普洛特利expose 了与Matplotlib的plot_trisurf类似的东西.然而,它expose 了go.Mesh,使我们能够达到同样的结果.

食谱如下:

  • 生成您的数字数据.
  • 创建三角剖分.对于这一部分,我们将使用Matplotlib的Triangulation类.
  • 创建Ploly地物并添加曲面.
  • 将图形导出为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相关问答推荐

(Python/Pandas)基于列中非缺失值的子集DataFrame

Django抛出重复的键值违反唯一约束错误

具有不同坐标的tkinter canvs.cocords()和canvs.moveto()

在使用ROLING()获得最大值时,是否可以排除每个窗口中的前n个值?

组颠倒大Pandas 数据帧

Fake pathlib.使用pyfakefs的类变量中的路径'

排除NRRD文件中的多切片卷加载问题

将标量值作为输入并输出矩阵的函数的积分

提取子数组,然后在Python中将它们连接起来

对齐多个叠置多面Seborn CAT图

函数以表格和图形的形式输出

PySpark:使用重置对窗口进行计数

使用Numpy进行重写For循环矢量化

每像素级图像处理的毕达式优化

为列PANAS中的每个字符串值创建累计和列

如何允许使用点输入进行键验证

获取Python中的层次 struct 数据

如何动态修改气流任务decorator 属性?

如何使用Pandas在Excel中有条件地设置单个单元格的格式?

Python字符串包含不能与方括号一起使用