plot 2D array using imagesc ?

조회 수: 22 (최근 30일)
dipak sanap
dipak sanap 2016년 2월 11일
편집: Brattv 2016년 2월 11일
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

채택된 답변

Brattv
Brattv 2016년 2월 11일
편집: Brattv 2016년 2월 11일
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
dipak sanap
dipak sanap 2016년 2월 11일
Well thanks now I understand how imagesc works.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2016년 2월 11일
편집: Guillaume 2016년 2월 11일
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
dipak sanap
dipak sanap 2016년 2월 11일
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 2016년 2월 11일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by