In MATLAB, is there a way to set the GRID at a spacing different from the ticks on the axes?

553 vues (au cours des 30 derniers jours)
In MATLAB, is there a way to set the GRID at a spacing different from the ticks on the axes?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 27 Juin 2009
Currently the ticks and grid line spacing are associated and so the only way to change grid spacing is to change the tick spacing. Here are some examples:
figure
plot(1:100)
set(gca,'xtick',[0:13:100])
set(gca,'ytick',linspace(0,100,13))
% The following code changes the minor grid
% spacing by adjusting the tick spacing:
figure
plot(1:100);
grid on
grid minor
set(gca,'xtick',[0:50:100])
set(gca,'ytick',[0:50:100])

Plus de réponses (1)

Udo Ruprecht
Udo Ruprecht le 6 Fév 2015
I have another idea
x=[20:0.1:80];
y=sin(x);
plot(x,y,'r','Linewidth',2)
ylim([0 4]);
xlim([0 100]);
% gridlines ---------------------------
hold on
g_y=[0:0.1:4]; % user defined grid Y [start:spaces:end]
g_x=[0:2:100]; % user defined grid X [start:spaces:end]
for i=1:length(g_x)
plot([g_x(i) g_x(i)],[g_y(1) g_y(end)],'k:') %y grid lines
hold on
end
for i=1:length(g_y)
plot([g_x(1) g_x(end)],[g_y(i) g_y(i)],'k:') %x grid lines
hold on
end
print(1,'-dpng','-r300','K1') %save plot as png (looks better)
  1 commentaire
Hugh
Hugh le 6 Août 2015
I voted for this answer because I also tend to build my own grid from lines. I use it to get the grid on top of an "area" plot style and to emphasize the baseline (typically zero value) with a thicker line.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by