Nocxs
Nocxs

Reputation: 23

PyQtGraph: How to name Bars in BarGraphItem

I need to be able to create a graph in PyQtGraph that either displays strings on the x line like this: enter image description here

Or inside of the bar itself like this:

enter image description here

These are my values:

y = [5.509, 5.509, 5.414, 5.414, 5.414, 5.289, 5.289, 5.289, 5.289, 5.289, 5.289, 5.289, 5.174, 5.174]
x = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']

This is an example of what it could look like. "Note that this does not work"

# importing QtGui to use QIcon
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication
 
# importing pyqtgraph as pg
import pyqtgraph as pg
 
# importing QtCore and QtGui from the pyqtgraph module
from pyqtgraph.Qt import QtCore

 
# creating a pyqtgraph plot window
window = pg.plot()
 
# setting window geometry
window.setGeometry(100, 100, 600, 500)
 
# title for the plot window
title = "Test"
 
# setting window title to plot window
window.setWindowTitle(title)

y = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
x = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']

bargraph = pg.BarGraphItem(x=x, height=y, width=0.5)
window.addItem(bargraph)

# main method
if __name__ == '__main__':
     
    # importing system
    import sys
     
    # Start Qt event loop unless running in interactive mode or using
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()

I need all the help I can get, since I haven't been able to find out anything myself.

Upvotes: 0

Views: 1844

Answers (1)

bfris
bfris

Reputation: 5815

This was a little more complicated than I thought. There are no examples for tick labels when you run the examples from the command line:

python -m pyqtgraph.examples

This question and the documentation for AxisItem provided the clues for how to set tick labels.

To make a proper bar chart, you have to pass in x values. And then you pass in replacement tick labels. For x values I just went from 1 to the number of labels: list(range(1, len(xlab)+1)). To get a reference to the x-axis, you use window.getAxis('bottom'). And finally, from the docs "The format for ticks looks like:"

[
    [ (majorTickValue1, majorTickString1), (majorTickValue2, majorTickString2), ... ],
    [ (minorTickValue1, minorTickString1), (minorTickValue2, minorTickString2), ... ],
    ...
]

Putting it all together we get:

# importing QtGui to use QIcon
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication

# importing pyqtgraph as pg
import pyqtgraph as pg

# importing QtCore and QtGui from the pyqtgraph module
from pyqtgraph.Qt import QtCore


# creating a pyqtgraph plot window
window = pg.plot()

# setting window geometry
window.setGeometry(100, 100, 600, 500)

# title for the plot window
title = "Test"

# setting window title to plot window
window.setWindowTitle(title)

y = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
xlab = ['RUS', 'VET', 'OCO', 'MSC', 'MAZ', 'VER', 'HAM', 'BOT', 'GAS', 'STR', 'SAI', 'RAI', 'NOR', 'PER']
xval = list(range(1,len(xlab)+1))
print(xval)

ticks=[]
for i, item in enumerate(xlab):
    ticks.append( (xval[i], item) )
ticks = [ticks]

bargraph = pg.BarGraphItem(x=xval, height=y, width=0.5)
window.addItem(bargraph)
ax = window.getAxis('bottom')
ax.setTicks(ticks)

# main method
if __name__ == '__main__':

    # importing system
    import sys

    # Start Qt event loop unless running in interactive mode or using
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QApplication.instance().exec_()

and the plot is

enter image description here

It's worth pointing out that this exercise is significantly easier in Matplotlib. According to this example the equivalent plotting command in Matplotlib is a one-liner

win.bar(xlab, y)

Upvotes: 2

Related Questions