0

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

enter image description here

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

2
  • 1
    Does this answer your question? Embed a matplotlib plot in a pyqt5 gui Commented Aug 31, 2020 at 14:06
  • @SuryaveerSingh Linked question kind of answered but I am still not able to understand. Can you please explain Commented Sep 1, 2020 at 5:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.