Thanks to visit codestin.com
Credit goes to github.com

Skip to content

quiver does not consider direction of an axis #3709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
espdev opened this issue Oct 23, 2014 · 8 comments
Closed

quiver does not consider direction of an axis #3709

espdev opened this issue Oct 23, 2014 · 8 comments
Assignees
Milestone

Comments

@espdev
Copy link

espdev commented Oct 23, 2014

I'm trying to plot imshow (it inverts Y-axis) and quiver on the same axes and I see the incorrect direction of vectors for Y-axis. In MATLAB the analogous code working correctly.

My code for example:

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

h = 100
w = 100

# test image (white square)
image = np.zeros((h, w))
image[20:79, 20:79] = 1

# compute X, Y gradients
gx = ndimage.convolve1d(image, [1, 0, -1], axis=1)
gy = ndimage.convolve1d(image, [1, 0, -1], axis=0)

# compute magnitude
mg = np.hypot(gx, gy)

# grid for quiver
x, y = np.meshgrid(np.arange(0, w), np.arange(0, h))

fig, ax = plt.subplots(2, 1)
ax1, ax2 = ax

ax1.set_title('Gradient mag image and Quiver')
ax1.imshow(mg, cmap=plt.cm.gray)
ax1.quiver(x, y, gx, gy, color='g')

ax2.set_title('Gradient Quiver')
ax2.set_xlim([0, w])
ax2.set_ylim([0, h])
ax2.set_aspect('equal')
ax2.quiver(x, y, gx, gy, color='g')

plt.show()

image

@tacaswell tacaswell added this to the v1.4.x milestone Oct 23, 2014
@tacaswell
Copy link
Member

I am confused, the arrows do seem to flip direction as desired....

@efiring
Copy link
Member

efiring commented Oct 23, 2014

The point is that the arrows are supposed to be showing the gradient, which should be inward regardless of whether any axis is flipped. So something is wrong here, but I don't know where the error is coming in.

@espdev
Copy link
Author

espdev commented Oct 23, 2014

At the second plot the arrows are in the right direction (they are directed inside towards the gradient), but at the first plot (with an image) vertical arrows are reversely directed.

If we look at the similar example in MATLAB, we will see that at the first plot arrows are directed inside. Quiver in MATLAB consides flipped Y-axis.

@tacaswell
Copy link
Member

ah, yes, I understand there needs to be an even number of flips

@efiring
Copy link
Member

efiring commented Oct 23, 2014

I see what is going on; it can be reproduced using our quiver demo. The U and V (vector components) are interpreted in a standard coordinate system regardless of the orientation of the coordinate system for X and Y (the locations). It's surprising that no one tripped over this and brought it up earlier; I can see that it is not the behavior one would expect or want in many cases--although there are cases (including one relevant to me) where it is the preferred behavior. At the moment I am thinking that we will want to add a kwarg to control this behavior--deciding whether the U and V coordinate orientations track X and Y. We have to be careful to handle the angles option consistently.

@soupault
Copy link
Contributor

Seems like a conflict between two concepts:

  1. Or we plot vector pair as a function (x axis is horizontal -inf(left):+inf(right));
  2. Either we plot them as an image (x axis is vertical +inf(bottom):-inf(top)).

I can imagine two various cases using quiver:

  • In image proccessing as mentioned by OP;
  • In pure math analysis.

So, it's should definitely be an option how to orient axis, like efiring said.

@tacaswell
Copy link
Member

@efiring punting from 1.4.3 as I don't see this getting done in the next week and it has been like this apparently forever.

@efiring
Copy link
Member

efiring commented Jan 22, 2015

@tacaswell, the fix is pretty simple, and I have included a test. I have not yet made a changelog or api changes entry; I want to see whether people are comfortable with the kwarg name and values, or have a better idea.
This could be categorized as a "missing feature" rather than a "bug". If it is more a bug than a missing feature, then the default should be changed to the newly added behavior; instead, in the present version of the PR, I kept the present behavior as the default. At the moment, I'm having a hard time remembering why I thought the present behavior might be preferred for some applications. Ah, yes, it would be when plotting velocity arrows in an x-z plane with depth positive down but vertical velocity positive up. That's pretty obscure, though, and easily handled by explicitly inverting the sign of the vertical velocity in the call to quiver.

tacaswell added a commit that referenced this issue Jan 25, 2015
BUG :  fixes for quiver
 - always recalculate verts 
 - improve docstring; 

closes #3709 closes #3817
tacaswell added a commit that referenced this issue Jan 25, 2015
BUG :  fixes for quiver
 - always recalculate verts
 - improve docstring;

closes #3709 closes #3817
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants