Matplotlib imshow: Data rotated?


Question

I was trying to plot some data with scatter. My code is

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy.interpolate import griddata
data = np.loadtxt('file1.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]
plt.scatter(x, y, c=z, s=100, cmap=mpl.cm.spectral)
cbar=plt.colorbar()
s=18
plt.ylabel(r"$a_v$", size=s)
plt.xlabel(r"$a_{\rm min}$", size=s)
plt.xlim([x.min(),x.max()])
plt.ylim([y.min(),y.max()])
plt.show()

The result is enter image description here

Now I came on the idea to try imshow with the some data, soince I didn't like the circles of scatter. So I tried this

from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
data = np.loadtxt('file1.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

N = 30j
extent = (min(x), max(x), min(y), max(y))

xs,ys = np.mgrid[extent[0]:extent[1]:N, extent[2]:extent[3]:N]

resampled = griddata(x, y, z, xs, ys)

plt.imshow(resampled.T, extent=extent)
s=18
plt.ylabel(r"$a_v$", size=s)
plt.xlabel(r"$a_{\rm min}$", size=s)
plt.xlim([x.min(),x.max()])
plt.ylim([y.min(),y.max()])
cbar=plt.colorbar()
plt.show()

With this result: enter image description here

My problem is obviosly why imshow() does invert the data? What happens here exactly?

PS: Here are the data, in case someone would like to play with them

1
20
1/14/2013 2:17:37 PM

Accepted Answer

Look at the keyword arguments of imshow. There is origin. The default is "upper", but you want "lower".

The default makes sense for plotting images, that usually start at the top-left corner. For most matrix-plotting, you'll want origin="lower"

35
1/14/2013 2:22:25 PM

It's not inverted, just flipped. The origin for imshow default to the upper left rather than the lower left. imshow has a parameter to specify the origin, it's named origin. Alternatively you can set the default in your matplotlib.conf.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon