MATLAB Answers

0

How to adapt the space between the grid lines to the space between the ticks?

Asked by beginner94 on 17 May 2018
Latest activity Commented on by dpb
on 18 May 2018

Good evening everyone

As I wanted the grid lines of my plot to have a different color from the axis, I put a second axis under the first. Now I doubled the ticks on the y axis but the grid lines aren't doubleing too. Has anyone an idea how to fix this?

Thank you (Btw I am still using R2007b)

  0 Comments

Sign in to comment.

1 Answer

Answer by dpb
on 17 May 2018

You have to address the proper axes object to change its properties...to make working with multiple axes easier, be absolutely sure to save the handles of each when you create them so have to "talk to" the desired one.

Post your code so far so we can use it for pedagogical purposes to get from where you got (sounds like good start) to where you're headed...(and actually, the above hint may be all the help you really need, we'll see :) ).

  4 Comments

Show 1 older comment

OK, will annotate some...

xdata = linspace(dates(1),dates(end),12); % 12 values between first/last dates given
set(gca,'XTick',xdata);                   % sets current axes xtick

Here should have already saved the axes handle when it was created and use it to be unequivocal as to which is being addressed for robust code...so we'll reorder slightly:

ax1 = gca;
set(ax1,'XTick',xdata);       % sets current (first?) axes xtick
ax2 =  axes('Position', get(ax1,'Position'),'Color','none', 'Box','off');   % good; saved the handle when created...
set(ax2, 'yTick', (-30:0.5:30), 'xColor', [0 0 0], 'yColor',[0 0 0]);            % saved it; now use it!!!  :)

Don't write labels, fix the format string as desired (and, yes, I agree it's absolutely tacky the TMW default)

hA.YAxis.TickLabelFormat='%0.1f';   % use the new(ish) ruler object
ax1.XColor=[0.85 0.85 0.85]; ax1.YColor=[0.85 0.85 0.85];  % dot notation as alternative to |set|
grid(ax1,'on')                % higher-level functions available
box(ax1,'off')
set(ax1,'GridLineStyle','-');
linkaxes([ax1 ax2],'xy');

...

Following again reverts to gca; not sure at this point who is still, but actually is ax2?

xdata = linspace(dates(1),dates(end),12);
set(gca,'XTick',xdata);          
datetick('x','keepticks','keeplimits')

Apparently your using datenum for the date array; it has been superceded by datetime object and plot is automagically aware of datetime so don't need the somewhat klunky datetick any longer.

If it's inconvenient to go back to the beginning and replace all datenum references with datetime, you can convert for plotting by

xdata=datetime(xdata,'convertfrom','datenum');

Excellent start for relatively new user! Congrats!

Thank you very much! I didn't realise that ''gca'' always addresses to the last called axes in the script, therefor the confusion! This also explains why I had to use ''linspace'' twice. It works now and the best: I even know why!:D

I wasn't able to use the newish ruler object and the ''datetime'' function though because of my (very) ancient matlab version (R2007b) :/

(I would accept your answer but it came as a comment :S)

>> help gca
gca Get handle to current axis.
  H = gca returns the handle to the current axis in the current
  figure. The current axis is the axis that graphics commands
  like PLOT, TITLE, SURF, etc. draw to if issued.
  ...

gca is a function so its return value is variable depending on what has happened since -- this is particularly significant when either in a GUI or interactive session and the user (or you) switches context.

Ah, ok, older releases do prevent using later features...

Sign in to comment.