如何使用Matplotlib获得更快的三维曲面打印更新?

2024-05-03 17:03:55 发布

您现在位置:Python中文网/ 问答频道 /正文

想象一个矩形板,有8个表面安装的传感器,在弯曲过程中在z方向上连续传输变形值。我正在尝试使用Matplotlib在三维曲面/线框图中获得更高的刷新率。大约30帧每秒就足够满足我的需要了。你知道吗

到目前为止,我已经使用matplotlib的3D线框图和Scipy的RBF插值函数来可视化变形,并获得一条平滑的曲线。matplotlib的刷新率本来就很低,正如前面几个答案中提到的那样。你知道吗

我将如何实现这个3D情节布点?在这种情况下,bliting是否足以满足更高的刷新率,或者PyQtGraph是必要的?你知道吗

代码:

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import scipy.interpolate
#from mpl_toolkits.mplot3d import Axes3D

# The first Four x and y values are the corners of the plate
# The next Eight x and y values correspond to the location of the sensors on the plate
x_coords = np.array([0, 500, 0, 500, 70, 70, 160, 250, 250, 340, 430, 430]);
y_coords = np.array([0, 0, 500, 500, 70, 340, 250, 160, 340, 250, 70, 430]);

# Just an example array of deformation values on the plate in the Z-direction. 
# The first Four z values are Zero because the corners of the plate are fixed during testing
z_deformation = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                          [0, 0, 0, 0,-5, -5, -8, -15, -15, -8, -5, -5], 
                          [0, 0, 0, 0,-5, -5, -9, -16, -16, -9, -5, -5],
                          [0, 0, 0, 0,-6, -6, -10, -18, -18, -10, -6, -6],
                          [0, 0, 0, 0,-7, -7, -12, -20, -20, -12, -7, -7],
                          [0, 0, 0, 0,-8, -8, -15, -22, -22, -15, -8, -8]]);

# the Plate is 500x500 mm. Create a meshgrid of this size with 100 data points in x and y directions
B1,B2 = np.meshgrid(np.linspace(0, 500, 100), np.linspace(0, 500, 100), indexing='xy')

# Initialize plotting
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d') 

for k in range(len(z_deformation)):

    # RBF Interpolation to smoothen the 8 sensor values 
    spline = sp.interpolate.Rbf(x_coords, y_coords, z_deformation[k,:], function='thin_plate', smooth=0, episilon=0)
    Z = spline(B1,B2)

    # Plotting the wireframe, labelling and setting axes limits
    ax.plot_wireframe(B1, B2, Z)
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Deformation')
    ax.set_zlim(-50, 50) 

    plt.draw()
    plt.pause(0.5)

    # Clear axes at the end of every iteration
    ax.cla()

Tags: andoftheimportmatplotlibnppltcoords