我需要一种使用数百万个数据点制作 3 维曲面图的方法,因此我开始检查 pyvista,它应该可以很好地做到这一点。但是,pyvista 对我来说有点难以掌握。
我有 x,y,z 数据,其中 x 是时间,y 是不同的测量值,z 是这些测量值的值。我想要的只是让 pyvista 向我展示包含这些信息的曲面图。
例如,如果我在 matplotlib 或其他带有曲面图的库中使用此数组:
X = np.array([1,2,3,4,5,6,7,8,9])
Y = np.array([1,2,3,4,5,6,7,8,9])
X, Y = np.meshgrid(X, Y)
Z = X*Y
我得到这个输出:
但是,如果我在任何 pyvista 图上使用相同的数据,我会得到如下信息:
import sys
# Setting the Qt bindings for QtPy
import os
os.environ["QT_API"] = "pyqt5"
from qtpy import QtWidgets
from qtpy.QtWidgets import QMainWindow
import numpy as np
import pyvista as pv
from pyvistaqt import QtInteractor
import pandas as pd
class MainWindow(QMainWindow):
def __init__(self, parent=None, show=True):
QtWidgets.QMainWindow.__init__(self, parent)
# create the frame
self.frame = QtWidgets.QFrame()
vlayout = QtWidgets.QVBoxLayout()
# add the pyvista interactor object
self.plotter = QtInteractor(self.frame)
vlayout.addWidget(self.plotter.interactor)
self.frame.setLayout(vlayout)
self.setCentralWidget(self.frame)
# simple menu to demo functions
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
exitButton = QtWidgets.QAction('Exit', self)
exitButton.setShortcut('Ctrl+Q')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
# allow adding a sphere
meshMenu = mainMenu.addMenu('Mesh')
self.add_sphere_action = QtWidgets.QAction('Add Sphere', self)
self.add_sphere_action.triggered.connect(self.add_sphere)
meshMenu.addAction(self.add_sphere_action)
x = np.array([9,8,7,6,5,4,3,2,1])
y = np.array([9,8,7,6,5,4,3,2,1])
x, y = np.meshgrid(x, y)
z = x*y
# z[z < -10] = np.nan # get rid of missing data. pyvista needs you to do this
i_res = 2 # display every nth point
j_res = 2 # display every nth point
self.grid = pv.StructuredGrid(x[::i_res, ::j_res], y[::i_res, ::j_res], z[::i_res, ::j_res])
self.z = z
self.x = x
self.y = y
self.plotter.add_mesh(self.grid, scalars=self.grid.points[:, 2], lighting=True, specular=0.5, smooth_shading=True,
show_scalar_bar=True)
if show:
self.show()
def add_sphere(self): #changing resolution, not adding a sphere
i_res = 5 # display every nth point
j_res = 5 # display every nth point
self.grid = pv.StructuredGrid(self.x[::i_res, ::j_res], self.y[::i_res, ::j_res], self.z[::i_res, ::j_res])
self.plotter.update()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
import pyvista as pv
import numpy as np
# Define a simple Gaussian surface
x = np.array([1,2,3,4,5,6,7,8,9])
y = np.array([1,2,3,4,5,6,7,8,9])
x, y = np.meshgrid(x, y)
z = x*y
# Get the points as a 2D NumPy array (N by 3)
points = np.c_[x.reshape(-1), y.reshape(-1), z.reshape(-1)]
points[0:5, :]
# simply pass the numpy points to the PolyData constructor
cloud = pv.PolyData(points)
cloud.plot(point_size=15)
我设法使用这段代码获得了不同的“东西”:
import pandas as pd
import pyvista as pv
import numpy as np
# Load Excel sheet using Pandas
# Note - you may need to `pip install xlrd`
# x = np.array([1,2,3,4,5,6,7,8,9])
# y = np.array([1,2,3,4,5,6,7,8,9])
x = np.array([[1],[2],[3],[4],[5],[6],[7],[8],[9]])
y = np.array([[1],[2],[3],[4],[5],[6],[7],[8],[9]])
# # x, y = np.meshgrid(x, y)
z = x*y
coords = np.hstack((x,y,z))
# Make the structured surface manually
structured = pv.StructuredGrid()
# Set coordinates
structured.points = coords
# Set the dimensions of the structured grid
structured.dimensions = [1, 1, 9]
# Apply an Elevation filter
elevation = structured.elevation()
elevation.plot(show_edges=True, show_grid=True, notebook=False)
但它只提供一串数据。我无法让其他任何东西正常工作。
有谁知道为什么 x,y,z 数据在 pyvista 中做奇怪的事情,以及我如何才能提供一个正常的表面图?将不胜感激,因为我很难过。