user308485
user308485

Reputation: 634

Why is my y-axis inverted when using meshgrid?

I'm trying to run the following:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
x = np.linspace(-15, 15, 10)
y = np.linspace(-15, 15, 10)
X, Y = np.meshgrid(x, y)
Z = Y;
# Z = X;
cmap = mpl.colors.ListedColormap(['r', 'b'])
bounds = [-300, 0, 300]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
plt.figure();
plt.xlabel('x');
plt.ylabel('y');
im = plt.imshow(Z,cmap= cmap, norm = norm)
plt.show();

If I try to do Z = X, it works fine. But if I do Z = Y, the y-axis is inverted, i.e. red (negative) at the top, and blue (positive) at the bottom. Why is this happening?

Upvotes: 1

Views: 1375

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

Inverted compared to what?

The plot you have is working fine in both cases, as expected. The value of Z[0,0], which is -15 in this case, is plotted in red at the coordinate 0,0.

If you want the y axis to start at the bottom instead of the top, use the origin="lower" keyword argument to imshow.

Upvotes: 3

Related Questions