Hello, I would like to show the grid at a specific position using and imagesc plot.
The
grid on
is associated with thick ticks positions. I would like it to be shown at a specific position, for exeample thick tick places + offset. Rather than 1 2 3 .. it would be 1.5, 2.5 3.5 etc for example. I do not want to change the xtick axis as it is shown in the help here.
I tried the gridxy function, which has the option of the position, but it doesn't show the grid where it is not empty (I see it where I have empty data only).
Any idea ? I am using R2019a.
Cheers

 Réponse acceptée

Star Strider
Star Strider le 7 Sep 2020

0 votes

I am not certain what you want.
Try this:
x = 1:0.1:10; % Create Data
y = rand(size(x)); % Create Data
figure
imagesc(x, y, y)
grid
Ax = gca; % Axis Handle
xt = Ax.XTick; % Get Current ‘XTick’ vector
Ax.XTick = xt + 0.5; % Create New ‘XTick’ Vector
.

6 commentaires

MaHa
MaHa le 7 Sep 2020
Hi,
z=rand(3,3);
figure
imagesc(z)
colormap(magma)
h=colorbar;
ylabel(h,'Z values','fontsize',20)
xlabel('x')
ylabel('y')
xlim([0.5 max(size(z,2))+0.5])
ylim([0.5 max(size(z,1))+0.5])
I would like to apply the grid for 1.5, 2.5 etc, but keeping xticks of 1, 2, 3 etc. By doing it I would improve the visibility of the plot, when I have two squares with the same color, it is hard to differentiate them.
I am still not certain what you want, and there is no ‘magma’ colormap, at least in R2020a.
That aside, try this:
z=rand(3,3);
figure
imagesc(z)
colormap(hot)
h=colorbar;
ylabel(h,'Z values','fontsize',20)
xlabel('x')
ylabel('y')
xlim([0.5 max(size(z,2))+0.5])
ylim([0.5 max(size(z,1))+0.5])
Ax = gca; % Axis Handle
xt = Ax.XTick; % Get Current ‘XTick’ vector
Ax.XTick = min(xt):max(xt); % Create New ‘XTick’ Vector
.
MaHa
MaHa le 7 Sep 2020
Thanks for trying ! A picture is worth a thousand words, here :
What I get if I apply grid on :
Ugly, it doesn't help at all !
What I would like :
Better grid, help to distinguish different squares.
Try this:
z=rand(3,3);
figure
imagesc(z)
yl = ylim;
xl = xlim;
gl = 0.5:3.5; % Grid Lines Locations
glplot = gl(2:end-1);
hold on
plot([1;1]*glplot, yl(:)*ones(size(glplot)), '-k', 'LineWidth',2)
plot(xl(:)*ones(size(glplot)), [1;1]*glplot, '-k', 'LineWidth',2)
hold off
colormap(hot)
h=colorbar;
ylabel(h,'Z values','fontsize',20)
xlabel('x')
ylabel('y')
xlim([0.5 max(size(z,2))+0.5])
ylim([0.5 max(size(z,1))+0.5])
Ax = gca; % Axis Handle
xt = Ax.XTick; % Get Current ‘XTick’ vector
Ax.XTick = (min(xt):max(xt))+0.5; % Create New ‘XTick’ Vector
You will obviously have to change it if the size of the ‘z’ matrix changes. If it is not square, use different ‘gl’ and ‘glplot’ vectors for the x and y direction grids.
This appears to work for this (3 x 3) grid, and should get you started.
MaHa
MaHa le 8 Sep 2020
Thanks
Star Strider
Star Strider le 8 Sep 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by