XGrid and XTick with different space

35 vues (au cours des 30 derniers jours)
Qun Wu
Qun Wu le 12 Juil 2017
Commenté : Qun Wu le 12 Juil 2017
I need draw a fine grid on a figure and keep the XY Ticks as the default one. To get the fine grid, I referred to the idea using a fine ticks. For example:
figure
plot( [-1 1] , [ -1 1] )
set(gca,'Xtick',-1 : 0.02 : 1);
set(gca,'Ytick',-1 : 0.02 : 1);
grid on;
However, I got plenty of ticks which are not required. I try to remove the ticks, and only keep a few of them. But I can only remove them all or not when using the line below:
set(gca,'XTickLabel','','YTickLabel','')
I am trying to keep the ticks shown the way just like generated by the first 2 line codes. Thank you.

Réponse acceptée

Alex Karle
Alex Karle le 12 Juil 2017
I ran your code and see your problem: the high frequency grid lines make it impossible to read the ticks and hard to see the plot.
You should be able to solve this issue using "Minor Ticks", which is exactly what you are looking for--a smaller tick that has no label, but will allow you to manipulate the size of the grid to your desire. Keep in mind that this functionality was introduced in MATLAB R2015b.
Minor ticks are associated with each axis of the axes. So you can specify a range of minor ticks for the XAxis and the YAxis. After you specify your tick values, make sure you write `grid minor` to make the grid follow the minor ticks!
You can find more information on minor ticks in the following documentation page: https://www.mathworks.com/help/matlab/ref/numericruler-properties.html
Here is an example based on your attached code:
figure
plot( [-1 1] , [ -1 1] )
set(gca,'Xtick',-1 : .25 : 1); %sets the numbered ticks .25 apart
set(gca,'Ytick',-1 : .25 :1); %same as above
ax = gca; %get the current axes
ax.XAxis.MinorTickValuesMode = 'manual'; %say you are setting the values yourself
ax.XAxis.MinorTickValues = -1:.025:1; %specify tick spacing to be .025 for minor ticks
ax.XAxis.MinorTick = 'on'; %set the minor ticks to display!
ax.YAxis.MinorTickValuesMode = 'manual';
ax.YAxis.MinorTickValues = -1:.025:1;
ax.YAxis.MinorTick = 'on';
grid minor; %set the grid to follow minor ticks.
Hope this helps!
  1 commentaire
Qun Wu
Qun Wu le 12 Juil 2017
Unfortunately, my Matlab version is 2015a, so clse. Anyway, thanks for your help.

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