Swap x-axis and y-axis in pyqtgraph

1,464 views
Skip to first unread message

m has

unread,
Jul 24, 2017, 12:31:19 PM7/24/17
to pyqtgraph
Is there any way to swap x-axis and y-axis in plot. I need a vertical x-axis and horizontal y-axis. I can not simply plot(y,x) as I need to set alot of other properties as well. I have been searching for two days but no avail. I rotated the ViewBox, but it changes the x-axis and y-axis labels.

Thank you.

Patrick Kreitzberg

unread,
Jul 24, 2017, 5:37:54 PM7/24/17
to pyqtgraph
Try getting the axis item of the viewbox then changing the label.  The ... is because you may be using different plot items.
ax = ...getViewBox().getAxis('left')
ax.setLabel('y')

ay = ...getViewBox().getAxis('bottom')
ay.setLabel('x')

I think that should switch the labels if that is all you want to do.  If you rotate the viewbox then you can change the labels this way.  If this doesn't solve your problem then please add more specifics about what exactly you want.  I am not sure if you want the data to look like (x,y) and just change the labels or if you want the data to look like (y,x) with the left axis labled x and the right labeled y.

Muhammad Haseeb

unread,
Jul 25, 2017, 5:45:30 AM7/25/17
to pyqt...@googlegroups.com
Hello Patrick,
Thank a lot for your reply. 
Let 't' represent time,  's' represent sin. Both are list, or numpy array of 1 dimension,

If I use plot(x,y), the sin wave is plotted such that x-axis(horizontal) is time, y-axis(vertical) is sin wave.

I want to use same "plot(x,y)" such that, vertical axis is time and horizontal axis is sin wave.
Now the simpler way to go is to use "plot(y,x)". But it create confusion in code, for example, to actually set XLimits, I have to use values of YLimits.

It would be great if there is method to simply interchange the x-axis to y-axis. 

##             +ve Y
## ^ Default axis
## |
## |
## |
## -------> +ve X
##
##
##              +ve X
## ^ Required axis
## |
## |
## |
## +ve Y <-------

Proposed Method:
1) Interchange X-axis to Y-axis, then Invert Y-axis
2) Rotate the plot in place by -90 degrees.(making sure all text, tick labels of axis doesn't get rotated.

Thank you.


--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/LPpYZrDvZpA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/ac73b0cc-7d01-42c1-b9f4-8c69e50f57c2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Patrick Kreitzberg

unread,
Jul 25, 2017, 2:22:06 PM7/25/17
to pyqtgraph
Is the following what you want? It has the x axis is on the left side, labeled as the x axis and increases as you go up.  The y axis is on the bottom, is labeled the y axis and increases to the left.

Done by setting the plotwidget to have invertX=true which is actually a property of the PlotItem created by the plotwidget. Then the graph is rotated 90 degrees and the PlotItem axis's are labeled as you want.

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
win = pg.PlotWidget()
x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)
l = pg.PlotDataItem(x=x,y=y)
win.addItem(l)

win.getPlotItem().invertX(True)

cw = QtGui.QWidget()            # GENERIC WIDGET AS CENTRAL WIDGET (inside main window)
lay = QtGui.QGridLayout()
cw.setLayout(lay)
lay.addWidget(win)

l.rotate(90)
ax = win.getPlotItem().getAxis('left')
ax.setLabel('Y')
ax = win.getPlotItem().getAxis('bottom')
ax.setLabel('X')

mw.setCentralWidget(cw)
mw.show()
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


On Monday, July 24, 2017 at 10:31:19 AM UTC-6, m has wrote:

Patrick Kreitzberg

unread,
Jul 25, 2017, 6:35:40 PM7/25/17
to pyqtgraph
My description was off, the x axis is on the left but is labeled 'Y'.  The y axis is on the bottom labeled as 'X'.

The graph is rotated 90 degrees and the bottom axis is inverted.

Muhammad Haseeb

unread,
Jul 25, 2017, 6:49:57 PM7/25/17
to pyqt...@googlegroups.com
Dear Patrik,
Thank you again for your detailed reply. Indeed the coordinates sytem appers to be rotated. Now if I try to set min, max limits on new X-axis(vertical), unfortunately it sets the limit to old axis(horizontal).

Best Regards,
 Haseeb

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/LPpYZrDvZpA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+unsubscribe@googlegroups.com.
Message has been deleted
Message has been deleted
Message has been deleted

Patrick Kreitzberg

unread,
Jul 26, 2017, 1:41:18 PM7/26/17
to pyqtgraph
The axis is linked to the viewbox so I am not sure if what you want can be done or not.   Rotating the viewbox causes a mess. This is probably an issue you will just have to work around by setting the axis range to variables that are named according to how you want and updating those, or whatever solution you want but I don't think pyqtgraph can do this natively.

If it helps, the viewbox function .childrenBounds() returns the xrange, yrange as you want them so that xrange is the range on the left axis and yrange is the range on the bottom axis.  If you can get the graph to update so that the viewbox left axis is the same as the child left axis then what you want to do will be done.  Not sure how to do that.  Maybe Ill look when I am not at work.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

m has

unread,
Jul 27, 2017, 7:02:24 AM7/27/17
to pyqtgraph
Dear Patrik, 
Thanks again for your valuable input. I also realized that probably it is not simple enought to just change axis in pyqtgraph. Normally you have plots with x-axis(horizontal) for time and y-axis(vertical) for some pysical quantity.

But in case of ADAS Autosar, the axis are different. Here x-axis(vertical) represent the distance along drive path for car and y-axis(horizontal) represents the lateral distance. So thats why i need a axis orientation like this to plot objects around car field of view.

I will look into it again, and if I come up with some solution, I will post it here.

Has
Reply all
Reply to author
Forward
0 new messages