我有一个窗口应用程序,当按下按钮时,该应用程序应该使用matplotlib触发3d散点图的动画。这是我的代码:
from __future__ import print_function
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QListWidget, QPushButton, QMainWindow
from PyQt5.QtCore import QObject
from matplotlib.animation import FuncAnimation
from matplotlib import pyplot as plt
import platform
import sys
import six
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
i = 1
def main():
global device_app, device_listbox, device_win_conf_button, device_win
device_app = QApplication(sys.argv) # Initialize QApp
device_win = QWidget()
device_win_layout = QVBoxLayout() # Layout
device_win_conf_button = QPushButton("Proceed")
device_app.aboutToQuit.connect(quit)
device_win_conf_button.clicked.connect(plotAnimator)
device_win_layout.addWidget(device_win_conf_button)
device_win.setLayout(device_win_layout) # Assign layout to window and resize
device_win.resize(500, 500)
device_win.show()
device_app.exec_() # Enter loop
def plotAnimator():
global sc_plot, x, y, z
fig = plt.figure() # Create matplotlib fig
ax = fig.add_subplot(111, projection='3d') # Add plot
ax.set_title("Example")
ax.set_xlim3d(-10, 10) # Set bounds
ax.set_ylim3d(-10, 10)
ax.set_zlim3d(-10, 10)
x = [0]
y = [0]
z = [0]
sc_plot = ax.scatter(x,y,z) # Start at 0, 0, 0
print(2)
animation = FuncAnimation(fig, updatePlot, frames=2, interval=100)
plt.show()
def updatePlot(frame):
global i
x.append(i)
y.append(i)
z.append(i)
sc_plot._offsets3d = (x, y, z)
i += 1
main()
因此,当我运行它并单击按钮时,散点图将显示为我已将其初始化为(0,0,0)的单点。我可以与剧情互动,旋转剧情并放大和缩小,但是没有动画。此外,当我单击“继续”按钮时,会将以下内容打印到控制台:
QCoreApplication::exec: The event loop is already running
。
我已经研究了此错误,据我了解,该错误来自尝试执行已经执行的QApplication
。我没有在代码中看到这种情况。我唯一能想到的是,也许matplotlib使用PyQT创建其窗口?即使是这种情况,我也很难考虑如何解决。
非常感谢您的帮助!