Changing the axes labels to match another coordinate system

52 views
Skip to first unread message

Alan Wang

unread,
Jul 12, 2022, 6:26:44 PM7/12/22
to pyqtgraph
Hi all,
I'm making a graph that displays an image with an ImageItem and GraphicsLayoutWidget, but I'd like to label the image with axes on a different system of coordinates. When I create a graph, the axes go from 0 to 4096 (the size of the image), but I'd like the axes to be labeled from -1024 to 1024, essentially remapping the coordinates to a different scale. Is there a way to do this? 

Thank you

Patrick

unread,
Jul 12, 2022, 10:17:46 PM7/12/22
to pyqtgraph
Hi Alan,

The new, accepted way to do this is by applying a QTransform to the ImageItem. Something like this:

# Axis labels or limits for the x and y pixels
# If pixel values, they need to be uniform intervals
x = np.array([-1024, 1024])
y = np.array([-1024, 1024])

# Scale in units per pixel
x_scale = (x[-1] - x[0])/(x.shape[0]-1) if x.shape[0] > 1 else 1.0
y_scale = (y[-1] - y[0])/(y.shape[0]-1) if y.shape[0] > 1 else 1.0

# Create QTransform and apply to ImageItem
# Adjust so values are centred on pixels
tr = QtGui.QTransform()
tr.translate(x[0] - x_scale/2, y[0] - y_scale/2)
tr.scale(x_scale, y_scale)
image.setTransform(tr)

It's just calculating the translation and pixel scale required to map the pixel coordinates to the desired axis coordinates.

Patrick

Ognyan Moore

unread,
Jul 13, 2022, 1:17:47 AM7/13/22
to pyqt...@googlegroups.com
In addition to what Patrick said; you can also call setRect on the image, to place it where you want

image.setRect(pg.Qt.QtCore.QRectF(x0, y0, width, height))

Keep in mind that x0 and y0 represent the top-left corner of the rectangle.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/85ab2e7c-5418-4946-9110-800983b05387n%40googlegroups.com.

Alan Wang

unread,
Jul 18, 2022, 5:28:41 PM7/18/22
to pyqtgraph
Thanks for the help. Both ways worked exactly how I needed them to, but I also want to be able to set a sinusoidal and logarithmic transformation on the axes. Is this possible without using a matrix?

Ognyan Moore

unread,
Jul 18, 2022, 6:53:19 PM7/18/22
to pyqt...@googlegroups.com
Non-linear transformations are not well supported in pyqtgraph.  There is a log mode; feel free to experiment with it, but there are a variety of issues with it, but it seems to work for some folks.

We are having discussions to support arbitrary transformations (see PR 2326), if this is a feature that would be useful to you, or think might impact you, we would love your input on it.

Reply all
Reply to author
Forward
0 new messages