Search code examples
user-interfacelabelpyqtgraphpyside6

Add individual Axis Labels to PyQT chart in Python?


I'm in the final portion of my project and we just need to make a graph showing the number of shows and movies with their rating trend. I have 4 different bar graph items and I was wondering if there's a way to add labels to each individual bar? For example here's one of the bars

y4 = [fourth]
x4 = [4.33]
bg4 = pg.BarGraphItem(x=x4, height=y4, width=0.2, brush='red', setLabel="Shows Down")
bg4.setX(0)
bg4.setY(0)

enter image description here


Solution

  • You could use a group of TextItem and set the bar as their parent:

    text = pg.TextItem('Some bar', angle=90, color='#ffff00')
    # reparent the item and make it a child of the bar
    text.setParentItem(bg4)
    # set the position to the x of the bar
    text.setX(x4[0])
    # use the x as left of the text and the y as vertical middle (referenced
    # to the text orientation)
    text.setAnchor(QPointF(0, .5))
    

    Note that you don't need to create individual bars as long as they are part of the same group and share the same colors: just add the x and height as lists of their positions and values.