Hi,
You must be a physicist. :)
If you have a copy of the pyqtgraph module under your project source directory (which is recommended), then easiest is to just modify the source. In pyqtgraph/graphicsItems/AxisItem.py modify the labelString method like:
def labelString(self):
if self.labelUnits == '':
if not self.autoSIPrefix or self.autoSIPrefixScale == 1.0:
units = ''
else:
units = asUnicode('/x%g') % (1.0/self.autoSIPrefixScale)
else:
#print repr(self.labelUnitPrefix), repr(self.labelUnits)
units = asUnicode('/%s%s') % (asUnicode(self.labelUnitPrefix), asUnicode(self.labelUnits))
s = asUnicode('%s %s') % (asUnicode(self.labelText), asUnicode(units))
style = ';'.join(['%s: %s' % (k, self.labelStyle[k]) for k in self.labelStyle])
return asUnicode("<span style='%s'>%s</span>") % (style, asUnicode(s))
Alternatively, don't use the automatic units/scaling and manually label the axes.
A more complicated but "correct" way if you don't want to modify the original source is to make your own class extending AxisItem and override the labelString method as above. When creating a
plotItem, you can pass in instances of your custom AxisItem in the constructor.
Finally, a horrible and ugly way is to live-patch the method in the existing AxisItems for your plots. For example:
#!/usr/bin/env python3
from PyQt5 import QtWidgets
import pyqtgraph as pg
class TestPlot(pg.GraphicsLayoutWidget):
def __init__(self):
super().__init__()
self.plot = self.addPlot()
self.plot.setLabels(left=("Left axis", "V"), bottom=("Bottom axis", "s"))
self.plot.getAxis("left").enableAutoSIPrefix(True)
# Crude patch of the AxisItem label formatting method
import types
def labelSIString(self):
if self.labelUnits == '':
if not self.autoSIPrefix or self.autoSIPrefixScale == 1.0:
units = ''
else:
units = str('/x%g') % (1.0/self.autoSIPrefixScale)
else:
units = str('/%s%s') % (str(self.labelUnitPrefix), str(self.labelUnits))
s = str('%s %s') % (str(self.labelText), str(units))
style = ';'.join(['%s: %s' % (k, self.labelStyle[k]) for k in self.labelStyle])
return str("<span style='%s'>%s</span>") % (style, str(s))
# Once you've defined the new method, it's just one line per axis you want to patch
self.plot.getAxis("left").labelString = types.MethodType(labelSIString, self.plot.getAxis("left"))
self.plot.getAxis("bottom").labelString = types.MethodType(labelSIString, self.plot.getAxis("bottom"))
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
mainwindow = TestPlot()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Hopefully one of those solutions will help.
Patrick