plot 2D array using imagesc ?
12 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Brattv
on 11 Feb 2016
Edited: Brattv
on 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
More Answers (1)
Guillaume
on 11 Feb 2016
Edited: Guillaume
on 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 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!