Trying to understand how imagesc works compared to scatter.
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have gridded data of mean tropopause temperature along with its longitude-latitude coordinates. I have done a scatter plot with this data so I know what it's supposed to look like. I recently saw a video about Matlab's Climate Toolbox, and in it, they used the function imagescn. I wanted to try out imagesc first to understand it a bit better but found that the output that I got had my data rotated 90 degrees compared to when I used a scatter plot. The grid of z-values were rotated but that x and y-axis stayed where it should be. Attached is an example of the data that I'm using.
To explain, I started off with an array of coordinates with attached mean temperature in no particular order. I then organize the temperature data in a grid with the coordinates as you would in an (x,y) graph, from bottom left to right, you go up one row, left to right, until you end up at top right. (Temperature is in Kelvin, also ignore the NaNs)
load('imagesc_confusion.mat')
meantemp
Because of the way MATLAB displays matrices, the longitude increases from top to bottom, and latitude increases from left to right. So the "origin" meantemp(1,1) would be at the top left. So if this were a map, you would be tilting your head to the right. The data can't fit in this post, but the temperature gets warmer as you get nearer the right most edge (meaning it gets warmer at the higher latitudes).
meantemp(:)
When you lay out the 2D matrix down in order, it starts going through the 'longitude' values first before going up a latitude (when looking at the data above, it goes to the next column to the right). Because of this, I would do the scatter plot like this:
scatter(xlon,ylat,50,meantemp(:),'filled'); colorbar;
...so that it would be displayed correctly. I then tried this out with imagesc:
imagesc(114:145,4:28,meantemp); axis xy; colorbar;
The warmer temps seem to be at the right edge instead of the top. I tried using some smaller data to see what would happen.
x = [100 110 120 100 110 120 100 110 120];
y = [3 3 3 4 4 4 5 5 5];
tempgrid = [0 20 70;5 30 100; 10 50 200]
scatter(x,y,50,tempgrid(:),'filled');
Okay, the 'warmer' temperature seems to be correctly rotated and is on the top right.
I tried it with imagesc:
imagesc(100:10:120,3:5,tempgrid);
axis xy;
The top row on the scatter plot seems to be on the right and sideways on the figure above. Is this just the nature of how imagesc displays data? Is there something wrong with my data? Have I missed something? Am I somehow misunderstanding how to use imagesc? How would I go about putting the "origin" on the bottom left? Switching the x and y values around sort of solves it, but it would still be sideways.
Anyway, some help would be appreciated, thanks!
imagesc(4:28,114:145,meantemp)
0 Kommentare
Akzeptierte Antwort
Voss
am 27 Aug. 2024
load('imagesc_confusion.mat')
Longitude (xlon) varies first in your data:
[xlon ylat meantemp(:)]
which means as you go down the first column of meantemp, longitude is varying and latitude is constant. Each column of meantemp corresponds to a single latitude value (and each row corresponds to a longitude).
But you want each column of pixels in your image to represent a single longitude value (and each row to represent a latitude), which means as you go down the first column of the image, latitude varies and longitude is constant.
Therefore, transpose meantemp when creating the image:
imagesc(xlon([1 end]),ylat([1 end]),meantemp.'); axis xy; colorbar;
That image matches the scatter plot:
scatter(xlon,ylat,50,meantemp(:),'filled'); colorbar;
[Also, note that the x and y arguments given to imagesc, when they are vectors, are treated as the corners of the image, and only the first and last elements of the vectors given are used. That's why I only passed the first and last elements of xlon and ylat in this case.]
2 Kommentare
Voss
am 30 Aug. 2024
You're welcome!
I also meant to point out that, unlike creating an image from a matrix, scatter doesn't care about the order of the data. You can randomly reorder the x, y, c, (as long as they are all reordered the same), and you'll still get the same result:
load('imagesc_confusion.mat')
x = xlon;
y = ylat;
c = meantemp(:);
[x,y,c]
% original
figure(); scatter(x,y,50,c,'filled'); colorbar;
% random reordering
idx = randperm(numel(meantemp));
x = x(idx);
y = y(idx);
c = c(idx);
[x,y,c]
figure(); scatter(x,y,50,c,'filled'); colorbar;
This is because scatter merely places a marker at each point you specify. The order of the points is of no consequence - hence the name "scatter".
Weitere Antworten (1)
Image Analyst
am 27 Aug. 2024
Scatter is an x-y plot where the origin is at the lower left, by convention. And imagesc plots an image (or matrix) where the origin is at the upper left, by convention. To reverse the y direction you can use either
axis xy % Put origin at the lower left.
or
axis ij % Put origin at the upper left.
until it looks like you want. Using axis ij is the default for images, imagesc, imshow, etc. and axis xy is the default for plots, plot(), scatter(), etc. You can change it with the axis command if you want.
Also, scatter takes (x,y) coordinates that do not necessarily lie on a perfect grid, while images must have pixel intensities on a perfect grid.
Plus scatter() puts down a marker (which you can optionally change the size and color of) while imagesc() puts down a dot (single pixel on your screen) where the intensity of the dot is the value of the matrix. They're different and I hope you understand why.
Siehe auch
Kategorien
Mehr zu Scatter Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!