EL_DON
EL_DON

Reputation: 1516

How can I control the alignment of y axes in multiple subplots with pyqtgraph when the tick labels have different lengths?

I am using pyqtgraph to make a plot with a few subplots and shared X axes. However, when the tick labels have different lengths, the Y axes do not line up with each other. How can I make the plots line up properly, despite differences in the size of tick labels or axis labels?

from pyqtgraph import QtGui, QtCore
import pyqtgraph as pg
import numpy as np

# Make up some data ----------
x = np.linspace(0, 80, 81)
y1 = np.sqrt(x)
y2 = 10*x**2

# Set up the plot ----------
# Switch to using white background and black foreground
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
app = QtGui.QApplication([])
pw = pg.GraphicsWindow(title="plot test")
gv = pw
layout_ = pg.GraphicsLayout()
gv.setCentralItem(layout_)
gv.show()
layout = layout_.addLayout(colspan=1)
layout.setContentsMargins(10, 10, 10, 10)
# Top row: title
layout.addLabel('title', colspan=2)
# Next row: main plot with y axis label
layout.nextRow()
layout.addLabel('y1', angle=-90, rowspan=1)
p1 = layout.addPlot()
# Next row: secondary plot
layout.nextRow()
layout.addLabel('y2', angle=-90, rowspan=1)
p2 = layout.addPlot()
# Bottom row: X axis label
layout.nextRow()
layout.addLabel("x", col=1, colspan=1)
# Share X axes
p2.setXLink(p1)

# Add data to plot ---------
p1.plot(x, y1, pen=pg.mkPen((255, 0, 0, 255), width=3))
p2.plot(x, y2, pen=pg.mkPen((0, 0, 255, 255), width=3))

# Start Qt event loop to keep the window open
if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        app.exec_()  # Start QApplication event loop ***

Output from example code

Upvotes: 3

Views: 1283

Answers (2)

Agus Abdullah
Agus Abdullah

Reputation: 31

from pyqtgraph import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import sys
# Make up some data ----------
x = np.linspace(0, 80, 81)
y1 = np.sqrt(x)
y2 = 10*x**2

# Set up the plot ----------
# Switch to using white background and black foreground
# pg.setConfigOption('background', 'w')
# pg.setConfigOption('foreground', 'k')

app = QtGui.QApplication([])
pw = pg.GraphicsWindow(title="plot test")
gv = pw
layout_ = pg.GraphicsLayout()
gv.setCentralItem(layout_)
gv.show()
layout = layout_.addLayout(colspan=1)
layout.setContentsMargins(10, 10, 10, 10)
# Top row: title
layout.addLabel('title', colspan=2)
# Next row: main plot with y axis label
layout.nextRow()
layout.addLabel('y1', angle=-90, rowspan=1)
p1 = layout.addPlot()
# Next row: secondary plot
layout.nextRow()
layout.addLabel('y2', angle=-90, rowspan=1)
p2 = layout.addPlot()
# Bottom row: X axis label
layout.nextRow()
layout.addLabel("x", col=1, colspan=1)
# Share X axes
p2.setXLink(p1)


p1.getAxis('left').setWidth(int(100))
p2.getAxis('left').setWidth(int(100))

# Add data to plot ---------
p1.plot(x, y1, pen=pg.mkPen((255, 0, 0, 255), width=3))
p2.plot(x, y2, pen=pg.mkPen((0, 0, 255, 255), width=3))

# Start Qt event loop to keep the window open
if __name__ == '__main__':
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        app.exec_()  # Start QApplication event loop ***

Upvotes: 2

Just link the 'left' axis to a view that is registered. Something like:

Widths = [i.width() for i in xAxis]
maximum=Widths.index(max(Widths))
viewBox[maximum].register('largest')
for i in listPlots:
    i.getAxis('left').linkToView(viewBox[maximum])

where listPlots is the list of plotDataItems

Upvotes: 1

Related Questions