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
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:
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
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.
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).
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.
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 ...
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"
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With