1

Is there a way to get matplotlib's quiver and streamplot functions to use the image coordinate convention (origin at the upper-left corner, +y points down), rather than the standard coordinate convention (origin at lower-left corner, +y points up)?

Context:

I'm using both quiver and streamplot to visualize optical flow fields between two images. You can see an example below.

Optical flow visualization using streamplot

Unfortunately, quiver and streamplot both expect the flow field to be defined in conventional coordinates, whereas my flow field is defined in image coordinates.

In the image above, I manually converted the flow to conventional coordinates before passing it to streamplot. The streamlines shows up correctly, but the axis labels are still in conventional coordinates. Notice how they disagree with the axis labels of the top two images.

I could manually convert axis labels as well, but is there some simpler way for me to just get streamplot and quiver to understand that the flow field is defined in the image coordinate convention, and should be displayed as such (origin at upper-left, +y points down)?

1 Answer 1

1

If you want to exactly mimic the display of the image axes (including the 0.5 offset and the inversion of the y axis), then you can should retrieve the x and y limits of one of the first two axes and then apply them to your streamplot axes

hax1 = plt.subplot(3, 1, 1)
hax1.imshow(image1)

hax2 = plt.subplot(3, 1, 2)
hax2.imshow(image2)

hax3 = plt.subplot(3, 1, 3)
hax3.streamplot(x, y, u, v)
hax3.set_xlim(hax1.get_xlim())
hax3.set_ylim(hax1.get_ylim())
Sign up to request clarification or add additional context in comments.

2 Comments

Actually, this doesn't just reverse the y axis label, but also flips the plot vertically (and shifts the origin to the upper-left). So now I don't have to convert the flow field from image coordinates to conventional coordinates before passing it to quiver/streamplot. Perfect!
(after your addition of set_xlim, set_ylim): Cool; I had been shifting the flow field's x & y coords by [.5, .5] manually, but I had forgotten that this wasn't reflected in the axes. Now I can pass in the flow coordinates as is, unshifted and unflipped, and have both the streamplot / quiver plot and the axes labels be consistent with imshow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.