Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib imshow: Data rotated?

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

like image 247
Tengis Avatar asked Jan 14 '13 14:01

Tengis


People also ask

How do you rotate an image on Imshow?

J = imrotate( I , angle ) rotates image I by angle degrees in a counterclockwise direction around its center point. To rotate the image clockwise, specify a negative value for angle . imrotate makes the output image J large enough to contain the entire rotated image.

Does Imshow normalize image?

By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

How do I rotate a figure in Matplotlib?

MatPlotLib with PythonAdd a mutable 2D affine transformation, "t". Add a rotation (in degrees) to this transform in place. Add a transform from the source (curved) coordinate to target (rectilinear) coordinate. Add a floating axes "h" with the current figure with GridHelperCurveLinear() instance.

What is extent in Imshow?

The extent keyword arguments controls the bounding box in data coordinates that the image will fill specified as (left, right, bottom, top) in data coordinates, the origin keyword argument controls how the image fills that bounding box, and the orientation in the final rendered image is also affected by the axes limits ...


3 Answers

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"

like image 53
Thorsten Kranz Avatar answered Oct 22 '22 09:10

Thorsten Kranz


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.

like image 22
Bernhard Avatar answered Oct 22 '22 10:10

Bernhard


Consider to use pcolormesh or contourf if you want to plot data of the form f(X, Y) = Z. imshow simply plots data Z, scaling and resampling has do be done manually.

like image 40
lumbric Avatar answered Oct 22 '22 08:10

lumbric