Matplotlib图形小部件未在PyQt5 Python中更新

Matplotlib图形小部件未在PyQt5 Python中更新,python,matplotlib,graph,Python,Matplotlib,Graph,我正在从事pyqt5项目。我正在使用qt设计器设计UI。我已经创建了一个窗口,并添加了一个小部件来显示图形。我还可以在UI的其他部分创建或删除库。有一个按钮,按下时显示图形 我为MatplotlibWidget创建了以下类 class MatplotlibWidget(QWidget): def __init__(self, parent=None): super(MatplotlibWidget, self).__init__(parent) self.

我正在从事
pyqt5
项目。我正在使用
qt设计器设计UI
。我已经创建了一个窗口,并添加了一个小部件来显示图形。我还可以在UI的其他部分创建或删除库。有一个按钮,按下时显示图形

我为MatplotlibWidget创建了以下类

class MatplotlibWidget(QWidget):
    def __init__(self, parent=None):
        super(MatplotlibWidget, self).__init__(parent)
        self.figure = Figure()
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.axis = self.figure.add_subplot(111)

        self.layoutvertical = QVBoxLayout(self)
        self.layoutvertical.addWidget(self.canvas)
下面是我如何使用它:

self.matplotlibwidget = MatplotlibWidget()
self.layoutvertical = QVBoxLayout(self.home_ui.total_persons_per_gallery_widget)
self.layoutvertical.addWidget(self.matplotlibwidget)
self.matplotlibwidget.axis.clear()

l = []
gallery_name = get_gallery_names()
for name in gallery_name:
    list_of_subjects = get_subject_id(name)
    print(len(list_of_subjects['subject_id'][0]))
    l.append(len(list_of_subjects['subject_id'][0]))

objects = tuple(gallery_name)
y_pos = np.arange(len(objects))
performance = l
bar_width = []
for i in range(len(gallery_name)):
    bar_width.append(0.1)

self.matplotlibwidget.axis.bar(y_pos, performance, align='center', alpha=0.5, width=bar_width)
self.matplotlibwidget.axis.set_xticks(y_pos)
self.matplotlibwidget.axis.set_xticklabels(objects)
self.matplotlibwidget.axis.set_ylabel('Persons')
self.matplotlibwidget.axis.set_title('Total no of persons per gallery')

self.matplotlibwidget.canvas.draw()
在上面的代码中,我得到创建的库列表,然后得到库中的人数。下面是它的外观:

如果我创建了另一个图库,然后单击按钮显示更新后的图形,它只显示两个图库,而不显示创建的第三个图库。如果我关闭应用程序并再次重新启动,它会在图中显示3rg库

我也尝试过调试,看起来代码运行良好,它获得了第三个库,但由于某些原因它没有显示出来。它仅在应用程序关闭并重新启动后显示。我们需要做些什么来刷新matplotlib。请帮忙。谢谢