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!