【问题标题】:The axes of the plot won't show when start FuncAnimation启动 FuncAnimation 时,绘图的轴不会显示
【发布时间】:2021-01-23 17:27:28
【问题描述】:

我对 FuncAnimation 有疑问。我的问题是动画有效,但情节的轴不再显示。

如果我将代码中的动画部分注释掉,它将显示带有轴的绘图。

    self.line, = self.ax.plot(self.state_x[0], self.state_y[0], '#000000', linewidth=4)
    self.anim = FuncAnimation(self.fig, self.update, frames=4, interval=700, blit=True, repeat=True)

def update(self, i):
    self.line.set_data([self.state_x[i], self.state_y[i]])
    return self.line,

所以我不知道为什么会发生这种情况以及如何解决它。

下面的完整代码。

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QWidget, QApplication, QRadioButton, QLabel
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QRect, Qt
from matplotlib.animation import FuncAnimation
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import sys


class Carnot(FigureCanvas):
    def __init__(self, parent):
        plt.ion()
        self.fig = Figure(figsize=(5, 5), dpi=100)
        self.ax = self.fig.add_subplot(111)
        super().__init__(self.fig)
        self.setParent(parent)
        self.resize(800, 800)

        self.state_x = np.array([(1, 2), (2, 2), (2, 1), (1, 1)])
        self.state_y = np.array([(2, 2), (2, 1), (1, 1), (1, 2)])

        self.setup()

    def setup(self):
        state12 = self.ax.plot(self.state_x[0], self.state_y[0], 'r', linewidth=3)
        state23 = self.ax.plot(self.state_x[1], self.state_y[1], 'b', linewidth=3)
        state34 = self.ax.plot(self.state_x[2], self.state_y[2], 'g', linewidth=3)
        state41 = self.ax.plot(self.state_x[3], self.state_y[3], 'y', linewidth=3)

        self.ax.set(xlabel="specific entropy s", ylabel="temperature T",
                    title="Carnot cycle")

        self.ax.set_xticks([])
        self.ax.set_yticks([])

        self.ax.legend(("1 → 2 isothermal expansion", "2 → 3 isentropic expansion", "3 → 4 isothermal compression",
                        "4 → 1 isentropic compression"))

        self.line, = self.ax.plot(self.state_x[0], self.state_y[0], '#000000', linewidth=4)
        self.anim = FuncAnimation(self.fig, self.update, frames=4, interval=700, blit=True, repeat=True)

    def update(self, i):
        self.line.set_data([self.state_x[i], self.state_y[i]])
        return self.line,


class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle("Name")
        self.resize(1110, 800)

        self.label_topic = QLabel("Carnot", self)
        self.label_topic.setGeometry(QRect(10, 10, 291, 41))
        self.label_topic.setFont(QFont("Arial", 12, QFont.Bold))
        self.label_topic.setAlignment(Qt.AlignLeft)

        self.radioButton1 = QRadioButton(self)  # Carnot
        self.radioButton1.setGeometry(QRect(20, 110, 91, 30))
        self.radioButton1.setFont(QFont("Arial", 12))
        self.radioButton1.setText("Carnot")
        self.radioButton1.clicked.connect(self.ShowCarnot)

    def ShowCarnot(self):
        carnot_process = Carnot(self)
        carnot_process.move(310, 0)
        carnot_process.show()


App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec())

【问题讨论】:

    标签: python matplotlib pyqt5 python-3.8 matplotlib-animation


    【解决方案1】:

    不要创建已使用名称的函数,在您的情况下,FigureCanvas 是一个继承自 QWidget 的类,因此也具有 update() 方法。但是用整数参数覆盖会导致当 maplotlib 在内部调用它时会失败,从而导致某个部分不被绘制。解决方案是将名称更改为更具描述性的名称:

        # ...
        self.anim = FuncAnimation(
            self.fig,
            self.update_animation,
            frames=4,
            interval=700,
            blit=True,
            repeat=True,
        )
    
    def update_animation(self, i):
        self.line.set_data([self.state_x[i], self.state_y[i]])
        return (self.line,)

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 2021-11-15
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多