plot 2D array using imagesc ?

17 Ansichten (letzte 30 Tage)
dipak sanap
dipak sanap am 11 Feb. 2016
Bearbeitet: Brattv am 11 Feb. 2016
Lets say, I have array x and y.
I make 2D grid with 16 bins and count number of points in each bin.
Counts of each bin are stored in matrix z.
I want to plot bitmap with x,y values and z which will color the bins according to count.
However, I always get wrong y axis scale, any explanation would be helpful.
x = [2,8,4,4.2,7.3,7.5,7.1]';
y = [2,8,6,6.7,2.1,2.9,2.5]';
xy = [x,y];
z = [0 0 0 1;0 2 0 0;0 0 0 0;1 0 0 3];
imagesc(x,y,z)
colorbar

Akzeptierte Antwort

Brattv
Brattv am 11 Feb. 2016
Bearbeitet: Brattv am 11 Feb. 2016
So i assume you have a scatter looking like this which you want to put in 16 bins.
The doc on the imagesc function tells you that
imagesc(x,y,C)
C is an image and x and y is the bounds of the x- and y axis. The x and y vectors you have defined is the ones you are counting. You also need to remember that image coordinates is not the same as the plot function coordinates. Image coordinates like imagesc uses [x,y] = [height,width] and starts in the upper left corner. Try changing the bounds like this
z = [0 0 0 1;0 2 0 0;0 0 0 0;1 0 0 3];
imageX = [1:4]
imageY = [1:4]
imagesc(imageX,imageY,z)
colorbar
and you will get
If you only want integers on the axis, use the following code
z = [0 0 0 1;0 2 0 0;0 0 0 0;1 0 0 3];
imageX = [1:4]
imageY = [1:4]
imagesc(imageX,imageY,z)
set(gca,'ytick',0:4)
set(gca,'xtick',0:4)
colorbar
Hope this solves your problem
  1 Kommentar
dipak sanap
dipak sanap am 11 Feb. 2016
Well thanks now I understand how imagesc works.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 11 Feb. 2016
Bearbeitet: Guillaume am 11 Feb. 2016
The x and y arguments of imagesc only specify the coordinates of two corners of the image, not the intermediate tick marks. x and y are supposed to be scalar or vectors with two elements.
Admittedly, imagesc should give an error when you supply with vectors with more than two elements for x and y (bug to raise with matlab) but instead it simply uses the first and last element of the arrays, so your call is equivalent to:
imagesc([x(1) x(end)], [y(1) y(end)], z)
What I don't understand is why you have 7 elements in your x and y array and only have a 4x4 z matrix. Therefore, I'm not what output you were hoping for.
Also, it's unusual to have your x and y unordered.
  2 Kommentare
dipak sanap
dipak sanap am 11 Feb. 2016
I am simply trying to plot 2D histogram.
I first divide x,y scatter data in desirable grid and store counts for each bin in z matrix.
In mentioned question, I created 4 by 4 grid
(I have written my own code to get z with desirable bin size)
just to understand how imagesc works.
Do you know any other way to plot this with correct x and y axis.
dipak sanap
dipak sanap am 11 Feb. 2016
If I do something like this imagesc([min(x) max(x)],[min(y) max(y)],z)
I am getting right answer, but I am not sure if this is right way now

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