I want to create a live-plot plot using matplotlib and pyqt5, but if I'm not mistaken, it gives an error because of the first parameter of the FuncAnimation module and I couldn't figure out exactly what to write here. I'm open to any suggestions about the code.
Codes are given below:
First Part:
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
class MplWidget(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.canvas = FigureCanvas(Figure())
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.setLayout(vertical_layout)
Second Part:
from PyQt5.QtWidgets import*
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
import numpy as np
import random
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
x_vals = []
y_vals = []
index = count()
class MatplotlibWidget(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("livegraph.ui",self)
self.setWindowTitle("PyQt5 Graph")
self.update_graph()
self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
def update_graph(self):
self.ani = FuncAnimation(self.MplWidget.canvas, self.animate, interval=1000)
self.show()
def animate(self,i):
data = pd.read_csv('data.csv')
x = data['x_value']
y1 = data['total_1']
y2 = data['total_2']
plt.cla()
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.plot(x, y1, label='Altitude')
self.MplWidget.canvas.axes.plot(x, y2, label='Velocity')
self.MplWidget.canvas.axes.set_title('Time-Velocity/Altitude')
self.MplWidget.canvas.axes.legend(loc='upper right')
app = QApplication([])
window = MatplotlibWidget()
window.show()
app.exec_()
Error part is given below
Traceback (most recent call last):
File "C:\Users\slalo\Desktop\livegraph\newmain.py", line 63, in <module>
window = MatplotlibWidget()
File "C:\Users\slalo\Desktop\livegraph\newmain.py", line 36, in __init__
self.update_graph()
File "C:\Users\slalo\Desktop\livegraph\newmain.py", line 42, in update_graph
self.ani = FuncAnimation(self.MplWidget.canvas, self.animate, interval=1000)
File "C:\Users\slalo\anaconda3\lib\site-packages\matplotlib\animation.py", line 1656, in __init__
TimedAnimation.__init__(self, fig, **kwargs)
File "C:\Users\slalo\anaconda3\lib\site-packages\matplotlib\animation.py", line 1414, in __init__
event_source = fig.canvas.new_timer(interval=self._interval)
AttributeError: 'FigureCanvasQTAgg' object has no attribute 'canvas'
The first parameter of FuncAnimation expects the Figure of the canvas, it does not expect the canvas. The solution is to change to:
self.ani = FuncAnimation(
self.MplWidget.canvas.figure, self.animate, interval=1000
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.