Search code examples
pythonmatplotlibplotimshow

Quiver plot changes when plotted on top of intensity


So I'm trying to plot a transverse vector field on top of an intensity plot. If I just plot the vector field using the following code (with the intensity plot commented out)

import matplotlib.pyplot as plt
import numpy as np
from numpy import ma

X = np.loadtxt("X.csv", delimiter=",\t", skiprows=1)
Y = np.loadtxt("Y.csv", delimiter=",\t", skiprows=1)
Z = np.loadtxt("Z.csv", delimiter=",\t", skiprows=1)

U = X
V = Y

#plt.imshow(Z)
plt.quiver(U, V)
plt.show()

Everything works fine as seen in this plot.

Correct Field

Correct Field

However, if I run it and include the plt.imshow(Z) line, then it completely changes the vector field though it plots the intensity correctly.

Incorrect Field

Incorrect Vector Field

As you can see, the two fields are different. Any help appreciated, thanks!


Solution

  • The two fields appear to be different because imshow modifies the default axis properties and reverses the direction of the y axis so instead of (0,0) being in the lower left it is in the upper left.

    To correct this, you can use the origin parameter to imshow

    plt.imshow(Z, origin='lower')
    

    The other change is that imshow sets the data aspect ratio to be equal.