更新 matplotlib 上的绘图
update plots on matplotlib
我正在尝试更新 matplotlib 图上的绘图
但是当我尝试使用
时,应用程序崩溃并停止工作
self.plot.remove()
这是我在 youtube 教程中找到的完整代码,它可以正常使用条形图,但绘图有问题
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QHBoxLayout, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Set Matplotlib Chart Value with QLineEdit Widget')
self.window_width, self.window_height = 1200, 800
self.setMinimumSize(self.window_width, self.window_height)
layout = QVBoxLayout()
self.setLayout(layout)
self.input = QLineEdit()
self.input.textChanged.connect(self.update_chart)
layout.addWidget(self.input)
self.canvas = FigureCanvas(plt.Figure(figsize=(15, 6)))
# set figure background color
self.canvas.figure.patch.set_facecolor('#303030')
layout.addWidget(self.canvas)
self.insert_ax()
def insert_ax(self):
font = {
'weight': 'normal',
'size': 16
}
matplotlib.rc('font', **font)
self.ax = self.canvas.figure.subplots()
self.ax.set_ylim([0, 100])
self.ax.set_xlim([0, 1])
self.plot = None
def update_chart(self):
value = self.input.text()
try:
value = int(value)
except ValueError:
value = 0
print(value)
x_position = [0.5]
if self.plot:
self.plot.remove()
self.plot = self.ax.plot(x_position, value, c='r', marker='o', linestyle="solid",linewidth=10)
self.canvas.draw()
# self.canvas.flush_events()
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor.
# QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app = QApplication(sys.argv)
app.setStyleSheet('''
QWidget {
font-size: 30px;
}
''')
myApp = MyApp()
myApp.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
替换为:
self.plot.remove()
有了这个:
self.plot.clear()
之后它应该可以工作了。
我正在尝试更新 matplotlib 图上的绘图 但是当我尝试使用
时,应用程序崩溃并停止工作self.plot.remove()
这是我在 youtube 教程中找到的完整代码,它可以正常使用条形图,但绘图有问题
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QHBoxLayout, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Set Matplotlib Chart Value with QLineEdit Widget')
self.window_width, self.window_height = 1200, 800
self.setMinimumSize(self.window_width, self.window_height)
layout = QVBoxLayout()
self.setLayout(layout)
self.input = QLineEdit()
self.input.textChanged.connect(self.update_chart)
layout.addWidget(self.input)
self.canvas = FigureCanvas(plt.Figure(figsize=(15, 6)))
# set figure background color
self.canvas.figure.patch.set_facecolor('#303030')
layout.addWidget(self.canvas)
self.insert_ax()
def insert_ax(self):
font = {
'weight': 'normal',
'size': 16
}
matplotlib.rc('font', **font)
self.ax = self.canvas.figure.subplots()
self.ax.set_ylim([0, 100])
self.ax.set_xlim([0, 1])
self.plot = None
def update_chart(self):
value = self.input.text()
try:
value = int(value)
except ValueError:
value = 0
print(value)
x_position = [0.5]
if self.plot:
self.plot.remove()
self.plot = self.ax.plot(x_position, value, c='r', marker='o', linestyle="solid",linewidth=10)
self.canvas.draw()
# self.canvas.flush_events()
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor.
# QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app = QApplication(sys.argv)
app.setStyleSheet('''
QWidget {
font-size: 30px;
}
''')
myApp = MyApp()
myApp.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
替换为:
self.plot.remove()
有了这个:
self.plot.clear()
之后它应该可以工作了。