plot 2D array using imagesc ?
조회 수: 12 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!