Reputation: 785
Is it possible in imagesc
to specify the x-axis a column vector such that the ticks and data points(pixels) are placed on the corresponding points? As far as I understood from the manual you only specify the corners of the image and this is not a problem as long as your data is evenly spaced like 100:100:1000
.
In my case the x-axis consists of 21 elements which are evenly spaced like 1200:50:1700
, whereas the y-axis is the vertical concatenation of two evenly spaced column vectors 200:50:450
and 500:25:725
. My aim is to have the data points(pixels) at the correct locations, but it seems impossible to do so. Is there a workaround?
Upvotes: 1
Views: 1883
Reputation: 10450
One option is to make Y
also evenly spaced, and than use repelem
:
X = 1200:50:1700;
Y = [0 50 100 200:50:450 500:25:725 800];
% set the spacing factor:
spacing = round(diff(Y)/min(diff(Y)));
The spacing vector looks like:
spacing =
Columns 1 through 14
2 2 4 2 2 2 2 2 2 1 1 1 1 1
Columns 15 through 19
1 1 1 1 3
and defines the distance between elements as multiplications of the smallest distance between elements in the vector.
We then define our new 'Y' for the image, so it will be evenly spaced, with the smallest space between elements in the vector.
% Define the new Y:
Y_spaced = Y(1):min(diff(Y)):Y(end); % = 0:25:800
% some arbitrary data:
data = rand(numel(Y),numel(X));
We use the spacing vector as input for repelem
to duplicate each row in data
as much as needed:
% spacing the data:
data_spaced = repelem(data,spacing([1 1:end]),1,1);
And we can plot it using imagesc
(either in the matrix ij
orientation, or in cartesian xy
orientation):
imagesc(X,Y_spaced,data_spaced)
axis xy
The result:
Upvotes: 3
Reputation: 1711
Use contourf or interpolate the data to a grid that fits all data points using e.g. interp2 or plot using contourf
Initialize data
X=[1200:50:1700]
Y=[200:50:450 500:25:725]
V = peaks(max(size(X,2),size(Y,2)))(1:size(Y,2),1:size(X,2));
Plot using contourf
contourf(X,Y,V);
view([0 -90]);
Interpolate and plot
Xq=linspace(min(X),max(X),100);
Yq=linspace(min(Y),max(Y),100)';
Vq = interp2(X,Y,V,Xq,Yq,'nearest');
imagesc(Vq)
If you want to have your pixels exactly at the correct location, you need to use a grid that has points exactly at all the point of your Y vector of course, so you need to adjust the "100"s accordingly to your screen and data resolution.
The alternative is to use pcolor
pcolor(X,Y,V)
view([0 -90]);
(Be careful with all the up-down flipping between image data and real data)
Upvotes: 1
Reputation: 315
Try replace imagesc
with surf
command:
surf(1200:50:1700, [200:50:450 500:25:725], rand(16,11), 'EdgeColor','none');
view([0 90]);
Note: with this way you loose last column and last row compare to imagesc
, but you can just duplicated these data before displaying.
Upvotes: 2