Why does imagesc show different outputs for the same input matrix?

52 views (last 30 days)
ppmap_debug was visualized using imagesc as follows:
% figure 1
figure;
imagesc('CData',ppmap_debug,'XData',xBydVec,'YData',yBydVec_debug)
axis tight equal
ppmap_debug and yBydVec_debug are formed as follows:
ppmap_debug = [pp_Region2;pp_Region3];
yBydVec_debug = [yBydVec_Region2 yBydVec_Region3];
pp_Region2 and pp_Region3 were also visualized separately as follows:
% figure 2
figure;
imagesc('CData',pp_Region2,'XData',xBydVec,'YData',yBydVec_Region2);
hold on
imagesc('CData',pp_Region3,'XData',xBydVec,'YData',yBydVec_Region3);
axis tight equal
figure 1 and figure 2 do not match as shown below. Why? Shouldn't they be the same?
pp_Region2, pp_Region3, yBydVec_Region2, yBydVec_Region3, and xBydVec have been provided to run the above code snippets.
  2 Comments
Stephen23
Stephen23 on 24 Nov 2024 at 17:58
"Why? Shouldn't they be the same?"
Nothing in your question explains why different data should be visualised the same.
M2 = load('pp_Region2.mat').pp_Region2
M2 = 500×1000
0.7954 0.7954 0.7951 0.7947 0.7941 0.7934 0.7925 0.7915 0.7903 0.7889 0.7874 0.7857 0.7839 0.7819 0.7797 0.7774 0.7750 0.7724 0.7696 0.7667 0.7637 0.7605 0.7572 0.7537 0.7501 0.7463 0.7425 0.7385 0.7343 0.7301 0.7952 0.7951 0.7948 0.7944 0.7939 0.7931 0.7923 0.7912 0.7900 0.7886 0.7871 0.7854 0.7836 0.7816 0.7795 0.7772 0.7747 0.7721 0.7694 0.7665 0.7634 0.7603 0.7569 0.7535 0.7499 0.7462 0.7423 0.7383 0.7342 0.7299 0.7949 0.7948 0.7946 0.7942 0.7936 0.7929 0.7920 0.7909 0.7897 0.7884 0.7868 0.7852 0.7833 0.7813 0.7792 0.7769 0.7745 0.7719 0.7691 0.7663 0.7632 0.7600 0.7567 0.7533 0.7497 0.7460 0.7421 0.7381 0.7340 0.7298 0.7946 0.7945 0.7943 0.7939 0.7933 0.7926 0.7917 0.7907 0.7895 0.7881 0.7866 0.7849 0.7831 0.7811 0.7790 0.7767 0.7742 0.7716 0.7689 0.7660 0.7630 0.7598 0.7565 0.7531 0.7495 0.7458 0.7419 0.7380 0.7338 0.7296 0.7943 0.7942 0.7940 0.7936 0.7930 0.7923 0.7914 0.7904 0.7892 0.7878 0.7863 0.7846 0.7828 0.7808 0.7787 0.7764 0.7740 0.7714 0.7687 0.7658 0.7628 0.7596 0.7563 0.7529 0.7493 0.7456 0.7417 0.7378 0.7337 0.7295 0.7940 0.7940 0.7937 0.7933 0.7928 0.7920 0.7912 0.7901 0.7889 0.7876 0.7860 0.7844 0.7826 0.7806 0.7785 0.7762 0.7737 0.7712 0.7684 0.7656 0.7626 0.7594 0.7561 0.7527 0.7491 0.7454 0.7416 0.7376 0.7335 0.7293 0.7938 0.7937 0.7934 0.7930 0.7925 0.7918 0.7909 0.7898 0.7886 0.7873 0.7858 0.7841 0.7823 0.7803 0.7782 0.7759 0.7735 0.7709 0.7682 0.7653 0.7623 0.7592 0.7559 0.7525 0.7489 0.7452 0.7414 0.7374 0.7333 0.7291 0.7935 0.7934 0.7932 0.7928 0.7922 0.7915 0.7906 0.7896 0.7884 0.7870 0.7855 0.7839 0.7820 0.7801 0.7780 0.7757 0.7733 0.7707 0.7680 0.7651 0.7621 0.7590 0.7557 0.7523 0.7487 0.7450 0.7412 0.7373 0.7332 0.7290 0.7932 0.7931 0.7929 0.7925 0.7919 0.7912 0.7903 0.7893 0.7881 0.7868 0.7853 0.7836 0.7818 0.7798 0.7777 0.7754 0.7730 0.7705 0.7677 0.7649 0.7619 0.7588 0.7555 0.7521 0.7485 0.7448 0.7410 0.7371 0.7330 0.7288 0.7929 0.7928 0.7926 0.7922 0.7916 0.7909 0.7901 0.7890 0.7878 0.7865 0.7850 0.7833 0.7815 0.7796 0.7775 0.7752 0.7728 0.7702 0.7675 0.7647 0.7617 0.7585 0.7553 0.7519 0.7483 0.7446 0.7408 0.7369 0.7328 0.7287
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M3 = load('pp_Region3.mat').pp_Region3
M3 = 500×1000
0.6651 0.6650 0.6649 0.6647 0.6645 0.6642 0.6638 0.6633 0.6628 0.6622 0.6616 0.6608 0.6600 0.6592 0.6582 0.6572 0.6561 0.6550 0.6538 0.6525 0.6512 0.6498 0.6483 0.6468 0.6452 0.6435 0.6418 0.6400 0.6381 0.6362 0.6649 0.6649 0.6648 0.6646 0.6644 0.6641 0.6637 0.6632 0.6627 0.6621 0.6614 0.6607 0.6599 0.6591 0.6581 0.6571 0.6560 0.6549 0.6537 0.6524 0.6511 0.6497 0.6482 0.6467 0.6451 0.6434 0.6417 0.6399 0.6381 0.6362 0.6648 0.6648 0.6647 0.6645 0.6642 0.6639 0.6636 0.6631 0.6626 0.6620 0.6613 0.6606 0.6598 0.6589 0.6580 0.6570 0.6560 0.6548 0.6536 0.6524 0.6510 0.6496 0.6482 0.6466 0.6450 0.6434 0.6417 0.6399 0.6381 0.6362 0.6647 0.6646 0.6645 0.6644 0.6641 0.6638 0.6634 0.6630 0.6625 0.6619 0.6612 0.6605 0.6597 0.6588 0.6579 0.6569 0.6559 0.6547 0.6535 0.6523 0.6509 0.6496 0.6481 0.6466 0.6450 0.6433 0.6416 0.6399 0.6380 0.6361 0.6645 0.6645 0.6644 0.6642 0.6640 0.6637 0.6633 0.6629 0.6623 0.6618 0.6611 0.6604 0.6596 0.6587 0.6578 0.6568 0.6558 0.6546 0.6534 0.6522 0.6509 0.6495 0.6480 0.6465 0.6449 0.6433 0.6416 0.6398 0.6380 0.6361 0.6644 0.6644 0.6643 0.6641 0.6639 0.6636 0.6632 0.6627 0.6622 0.6616 0.6610 0.6603 0.6595 0.6586 0.6577 0.6567 0.6557 0.6545 0.6534 0.6521 0.6508 0.6494 0.6480 0.6464 0.6449 0.6432 0.6415 0.6398 0.6379 0.6361 0.6643 0.6643 0.6642 0.6640 0.6637 0.6634 0.6631 0.6626 0.6621 0.6615 0.6609 0.6602 0.6594 0.6585 0.6576 0.6566 0.6556 0.6544 0.6533 0.6520 0.6507 0.6493 0.6479 0.6464 0.6448 0.6432 0.6415 0.6397 0.6379 0.6360 0.6642 0.6641 0.6640 0.6639 0.6636 0.6633 0.6629 0.6625 0.6620 0.6614 0.6608 0.6600 0.6593 0.6584 0.6575 0.6565 0.6555 0.6543 0.6532 0.6519 0.6506 0.6492 0.6478 0.6463 0.6447 0.6431 0.6414 0.6397 0.6379 0.6360 0.6640 0.6640 0.6639 0.6637 0.6635 0.6632 0.6628 0.6624 0.6619 0.6613 0.6606 0.6599 0.6591 0.6583 0.6574 0.6564 0.6554 0.6543 0.6531 0.6518 0.6505 0.6492 0.6477 0.6462 0.6447 0.6431 0.6414 0.6396 0.6378 0.6360 0.6639 0.6639 0.6638 0.6636 0.6634 0.6631 0.6627 0.6622 0.6617 0.6612 0.6605 0.6598 0.6590 0.6582 0.6573 0.6563 0.6553 0.6542 0.6530 0.6517 0.6504 0.6491 0.6477 0.6462 0.6446 0.6430 0.6413 0.6396 0.6378 0.6359
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
surf(M3-M2)
Sumit
Sumit on 24 Nov 2024 at 18:48
Edited: Sumit on 2 Dec 2024 at 8:59
Sorry. I have not been clear.
pp_Region2 and pp_Region 3 are different matrices - these are 2 different parts of a single image. yBydVec_Region2 and yBydVec_Region3 are the corresponding y-coordinate vectors for these 2 images. pp_Region2 and pp_Region3 are plotted in figure 2 using 2 imagesc calls and corresponding y-coordinate vectors. In figure 1, the matrices and y-coordinate vectors are combined and plotted using a single imagesc call. I was expecting figure 1 and 2 be the same becasue (a) for figure 1, we have a combined matrix, and (b) for figure 2, we have the same matrix but split into 2 halves. Question: Why do figures 1 and 2 not look the same?
Note: these matrices share the same x-coordinate vector, xBydVec.

Sign in to comment.

Answers (2)

DGM
DGM on 2 Dec 2024 at 13:39
Edited: DGM on 2 Dec 2024 at 13:46
The problem is the ydata spacing is not uniform between the two sets. Tools like image(), imagesc(), and imshow() don't use all the xdata and ydata you give it. They only look at the extrema of the xdata,ydata, and then they just uniformly distribute everything in between. That behavior assumes that the samples are uniformly distributed over the interval. In this case, they aren't.
load yBydVec_Region2.mat
load yBydVec_Region3.mat
yBydVec_debug = [yBydVec_Region2 yBydVec_Region3];
plot(yBydVec_debug)
In the more general case of arbitrarily-distributed xdata,ydata, it's probably just more convenient to use pcolor() instead. There are differences, but the main point here is that it actually plots the zdata at the specified xdata and ydata instead of just redistributing it like imagesc().
load pp_Region2.mat
load pp_Region3.mat
load xBydVec.mat
load yBydVec_Region2.mat
load yBydVec_Region3.mat
ppmap_debug = [pp_Region2; pp_Region3];
yBydVec_debug = [yBydVec_Region2 yBydVec_Region3];
% figure 1
figure;
pcolor(xBydVec,yBydVec_debug,ppmap_debug)
shading flat
axis tight equal
% figure 2
figure;
pcolor(xBydVec,yBydVec_Region2,pp_Region2)
hold on
pcolor(xBydVec,yBydVec_Region3,pp_Region3)
shading flat
axis tight equal
As to what you could do to make it work with imagesc(), that would be to do it how you were doing it in two separate calls. That could work for this simple case where a single axis is piecewise-linear. Trying to merge the data and plot it at once is what's messing things up.

Divyajyoti Nayak
Divyajyoti Nayak on 24 Nov 2024 at 19:06
Hi @Sumit,
From what I can tell, the reason the two figures are slightly different is because for figure 1 the colors are being scaled off the maximum and minimum values of 'ppmap_debug'. While for the second figure, it is formed by the stitching together of two plots whose colors are scaled by their individual maximum and minimum values of 'CData'.
  2 Comments
Sumit
Sumit on 25 Nov 2024 at 17:30
Edited: Sumit on 25 Nov 2024 at 21:46
Thanks Divyajyoti. I repeated the steps mentioned in my question using 2 small (2 x 2) matrices, as shown below and generated figures 3 and 4.
aa = [1 2;3 4];
bb = [5 6;7 8];
combinedAABB = [aa;bb];
xCord = [1 2]
yCord_aa = [1 2]
yCord_bb = [3 4]
yCord_vec = [1 2 3 4]
%figure 3
figure;
imagesc('CData',combinedAABB,'XData',xCord,'YData',yCord_vec)
%figure 4
figure;
imagesc('CData',aa,'XData',xCord,'YData',yCord_aa);
hold on
imagesc('CData',bb,'XData',xCord,'YData',yCord_bb);
Here, figure 3 and 4 look the same unlike figure 1 and 2 in the question.
If they were being scaled off the maximum and minimum values, they shouldn't look the same.
DGM
DGM on 2 Dec 2024 at 13:50
I admit, this was my first guess. The white page background made it hard to notice the "crease" in the image.

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!