I am creating a pyqt5 application where I need to show the matplotlib graph. I am following this video from youtube. In this video, he first creates a window in qt designer and adds a widget which is used to show graph. Below is the code:
He has created this class for matplotlib widget:
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)
and then this is how it is being used:
self.matplotlibwidget = MatplotlibWidget()
self.layoutvertical = QVBoxLayout(self.home_ui.total_persons_per_gallery_widget)
self.layoutvertical.addWidget(self.matplotlibwidget)
self.matplotlibwidget.axis.clear()
x = np.random.random(10)
y = np.random.random(10)
txts = ["P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"]
self.matplotlibwidget.axis.scatter(x, y)
for index, txt in enumerate(txts):
self.matplotlibwidget.axis.annotate(txt, (x[index], y[index]))
self.matplotlibwidget.canvas.draw()
Above code is working fine and showing below graph
In above coe in class MatplotlibWidget(QWidget), I am not able to understand this line self.axis = self.figure.add_subplot(111).
I want to create a bar graph for that I updated the code to below:
self.matplotlibwidget = MatplotlibWidget()
self.layoutvertical = QVBoxLayout(self.home_ui.total_persons_per_gallery_widget)
self.layoutvertical.addWidget(self.matplotlibwidget)
objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(objects))
performance = [10, 8, 6, 4, 2, 1]
self.matplotlibwidget.bar(y_pos, performance, align='center', alpha=0.5)
self.matplotlibwidget.xticks(y_pos, objects)
self.matplotlibwidget.ylabel('Usage')
self.matplotlibwidget.title('Programming language usage')
self.matplotlibwidget.canvas.draw()
and it gave me an error that matplotlibwidget doesnt not have bar. Can anyone tell me how can I update the code to show bar graph or any other graph. Please help. Thanks
