[英]PyVista: Why does the callback only draw the 3d entities and not the 2d one on the second time?
我有一個問題,希望你能幫助我。 我在下面放了一個小的 MCVE。
第一次點擊按鈕時,可以看到3d數據(add_mesh、add_points)和2d數據(chart.scatter)出現了。 如果再次單擊它,則僅顯示 3d 數據。
有人知道為什么會這樣嗎?
import sys
import os
os.environ["QT_API"] = "pyqt5"
from qtpy import QtWidgets
import numpy as np
from pyvista import Chart2D
import pyvista as pv
from pyvistaqt import QtInteractor, MainWindow
class MyMainWindow(MainWindow):
def __init__(self, parent=None, show=True):
QtWidgets.QMainWindow.__init__(self, parent)
self.frame = QtWidgets.QFrame()
vlayout = QtWidgets.QVBoxLayout()
self.plotter = QtInteractor(self.frame)
btn1 = QtWidgets.QPushButton("Button 1", self)
vlayout.addWidget(btn1)
btn1.clicked.connect(self.buttonClicked)
vlayout.addWidget(self.plotter)
self.frame.setLayout(vlayout)
self.setCentralWidget(self.frame)
self.show()
def buttonClicked(self):
self.plotter.clear()
data = np.random.standard_normal(size=(100, 3))
actor = self.plotter.add_points(data)
chart = Chart2D()
_x = np.random.standard_normal(100)
_y = np.random.standard_normal(100)
chart.scatter(_x, _y, color="tab:blue", style="d", label="Scores")
self.plotter.add_chart(chart)
self.plotter.add_mesh(pv.Sphere())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyMainWindow()
sys.exit(app.exec_())
我認為您發現了一個錯誤,感謝您提供的好例子! 這現在在 PyVista 版本 0.35.2 中得到修復,更新包應該可以正常工作。 以下答案的原始(修復前)版本。
圖表的情況是它們存儲在名為__charts
的私有屬性中,可通過繪圖儀的Renderer
上名為_charts
的私有屬性訪問。 即self.plotter.renderer._charts
會給你一個私有的pyvista.plotting.charts.Charts
對象。 這實際上是跟蹤通過plotter.add_chart()
添加的各個圖表。
但是調用plotter.clear()
並不會清除這個圖表列表。 我還不清楚這究竟是如何與重新繪制混淆的(我懷疑早期的圖表以某種方式阻止了新圖表的添加,但新圖表已經從繪圖儀中刪除了)。 但是在plotter.clear()
上清除圖表也是有意義的,如果我通過編輯 PyVista 的庫代碼來做到這一點,你的問題就會消失。
我已經在 PyVista 中打開了一個拉取請求來解決這個問題(或者了解我是否錯了這是一個錯誤)。 在發生這種情況並發布修復程序之前,您可以在回調中破解您自己的代碼中的等效步驟:
self.plotter.clear()
self.plotter.renderer._charts.deep_clean() # <- hacky workaround
不用說,這是一種臨時措施,並且在您自己的代碼中具有非常糟糕的形式。 一旦我們有一個修復了這個問題的新版本,你應該刪除這行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.