How to plot a bar chart with Qt Designer graphics view

72 views
Skip to first unread message

Matthew Martin

unread,
Sep 14, 2019, 5:04:02 PM9/14/19
to pyqtgraph
Hello,

What is the way I can plot a bar chart in a graphics view created with Qt Designer?

Following an example I found, I tried the following without success.  The variable gv_spec is a graphics view item promoted to a PlotWidget in Designer.

curve2 = self.gv_spec.plot()
y1 = np.linspace(0, np.floor(x_time[-1]/fs), data2.shape[0])
bg1 = pg.BarGraphItem(x=data2, height=y1, width=0.6, brush='r')
curve2.addItem(bg1)

This yields the error: AttributeError: 'PlotDataItem' object has no attribute 'addItem'

My goal is simply to change from a line graph to a bar chart. This line that I was using works fine in displaying the line graph.

curve2 = self.gv_spec.plot(data2, clickable=True,
fillBrush=pg.mkBrush(0, 255, 0, 100), fillLevel=0)

How can I convert this line graph to a bar chart?





Patrick

unread,
Sep 15, 2019, 8:22:35 PM9/15/19
to pyqtgraph
Hi,

The plot() method from PlotWidget is just a convenience method to generate a PlotDataItem (which is the default scatter/line plot) and add it to the PlotItem contained within. Here you are manually creating a plot type, which then needs to be added to the scene manually (in the same way you could also construct the PlotDataItem manually and add it). So try self.gv_spec.addItem(bg1) and that should do what you want.

Patrick

Jim Crowell

unread,
Sep 18, 2019, 9:54:20 AM9/18/19
to pyqtgraph
I worked out a method that didn't require promoting, which I found very fiddly; you just add a QWidget in designer and pass to this function:


def add_plot(widget):
   
"""Add a plot item (that will behave nicely) to a basic QWidget (e.g.
    generated by QTDesigner).

    widget: QWidget

    returns None
    """


    layout
= QtGui.QHBoxLayout()
    widget
.setLayout(layout)
    layout
.setSpacing(0)

    widget
.plotView = pg.GraphicsView()
    layout
.addWidget(widget.plotView)

    widget
.plotItem = pg.PlotItem()
    widget
.plotItem.resize = widget.resize
    widget
.plotView.setCentralItem(widget.plotItem)



Reply all
Reply to author
Forward
0 new messages