Trying to understand how imagesc works compared to scatter.

11 Ansichten (letzte 30 Tage)
Ryan Eugenio
Ryan Eugenio am 27 Aug. 2024
Kommentiert: Voss am 30 Aug. 2024
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
meantemp = 32x25
187.1023 186.0357 NaN 186.7121 189.5764 NaN NaN 187.7755 NaN NaN 187.8682 NaN NaN 187.8468 NaN 190.5751 191.2325 186.6971 191.2550 191.1144 NaN NaN 195.2639 198.4585 NaN NaN NaN NaN NaN NaN NaN NaN NaN 184.5038 188.4985 185.4796 187.3182 NaN 188.7641 187.4788 189.5509 186.6644 188.9223 187.2073 187.9126 NaN 191.3150 193.6064 196.6508 NaN 190.3012 NaN 189.8166 NaN 186.0039 185.4499 NaN NaN 189.0813 NaN NaN 188.3569 NaN 188.4332 190.7123 188.1415 190.7428 NaN 190.2569 192.6492 192.2567 189.9670 192.6407 195.5686 NaN 185.3917 185.1080 187.1583 185.5245 188.1421 NaN NaN 187.1831 185.8774 189.0513 184.9662 187.4364 188.1925 188.2912 189.6705 188.6802 188.9572 NaN 194.2109 191.3852 190.6202 NaN 197.2982 NaN NaN NaN NaN NaN NaN 186.0977 NaN NaN 186.4786 186.1045 NaN NaN NaN 189.8378 188.7481 NaN 186.0372 189.8646 NaN 189.6533 191.2494 191.0871 194.0438 193.8742 195.1335 NaN 187.1623 NaN NaN NaN 185.8767 NaN NaN NaN 188.7393 185.7082 188.2986 188.4371 NaN NaN NaN 186.5061 188.3968 NaN 189.4588 189.3158 192.5182 195.6481 NaN 196.0929 NaN NaN NaN NaN NaN 185.3353 NaN NaN NaN 184.4874 187.7337 187.0527 187.1956 187.6000 186.9031 188.7024 187.2118 NaN 189.9402 NaN 191.2000 192.1758 195.2609 NaN 196.6787 NaN 181.5051 182.9686 185.6889 186.5361 185.3924 NaN 184.8080 185.0929 186.8034 NaN 185.2615 NaN NaN NaN 189.7435 187.0576 NaN 190.5458 190.4124 189.1134 188.6690 191.4891 NaN NaN NaN 189.0035 NaN 188.3849 NaN 185.6898 185.2612 189.3346 NaN NaN 183.5456 184.2918 186.4169 189.2023 NaN 189.1633 185.0669 188.6795 NaN 189.8301 190.0784 195.8996 NaN 192.3665 197.0935 NaN NaN 184.9058 NaN NaN NaN 184.2746 NaN 184.1591 NaN 185.9680 NaN NaN NaN 189.3288 187.7720 190.4746 189.3607 NaN 189.5035 191.7706 NaN NaN 193.2623 193.1618 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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(:)
ans = 800x1
187.1023 NaN 190.3012 185.3917 NaN 187.1623 NaN 181.5051 189.0035 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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]
tempgrid = 3x3
0 20 70 5 30 100 10 50 200
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)

Akzeptierte Antwort

Voss
Voss am 27 Aug. 2024
load('imagesc_confusion.mat')
Longitude (xlon) varies first in your data:
[xlon ylat meantemp(:)]
ans = 800x3
114.0000 4.0000 187.1023 115.0000 4.0000 NaN 116.0000 4.0000 190.3012 117.0000 4.0000 185.3917 118.0000 4.0000 NaN 119.0000 4.0000 187.1623 120.0000 4.0000 NaN 121.0000 4.0000 181.5051 122.0000 4.0000 189.0035 123.0000 4.0000 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
Ryan Eugenio
Ryan Eugenio am 30 Aug. 2024
Thank you for the answer! (and also for the imagesc vector tip)
Voss
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]
ans = 800x3
114.0000 4.0000 187.1023 115.0000 4.0000 NaN 116.0000 4.0000 190.3012 117.0000 4.0000 185.3917 118.0000 4.0000 NaN 119.0000 4.0000 187.1623 120.0000 4.0000 NaN 121.0000 4.0000 181.5051 122.0000 4.0000 189.0035 123.0000 4.0000 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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]
ans = 800x3
124.0000 23.0000 190.0097 140.0000 17.0000 NaN 130.0000 26.0000 190.7519 137.0000 28.0000 NaN 119.0000 23.0000 189.3158 122.0000 4.0000 189.0035 116.0000 10.0000 NaN 140.0000 14.0000 188.4230 120.0000 19.0000 187.2118 114.0000 12.0000 NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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".

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
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.
  1 Kommentar
Ryan Eugenio
Ryan Eugenio am 30 Aug. 2024
"And imagesc plots an image (or matrix) where the origin is at the upper left, by convention."
Guess that explains it. Thanks!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by