incomplete minor gridlines in semilogx plot

11 views (last 30 days)
I would like to plot a semi-log plot with gridlines. The problem is that the plot needs to have a slightly elongated aspect ratio, and when I switch the aspect ratio the gridlines disappear. For example:
x = 10.^(-9).*[2,3,4];
y = [3,4,5];
figure(1); close(gcf); figure(1);
pos = get(gcf,'Position'); set(gcf,'Position',[pos(1),pos(2),0.5.*pos(3),pos(4)]);
semilogx(x,y,'-ok'); grid on; xlim([3*10^(-11),4*10^(-8)]);
no longer has minor xaxis gridlines even though XMinorGrid = on and XMinorTick = on. I can add most of the gridlines by altering the code to be:
x = 10.^(-9).*[2,3,4]; y = [3,4,5];
figure(1); close(gcf); figure(1);
pos = get(gcf,'Position'); set(gcf,'Position',[pos(1),pos(2),0.5.*pos(3),pos(4)]);
semilogx(x,y,'-ok'); grid on; xlim([3*10^(-11),4*10^(-8)]);
set(gca,'XTickMode','manual'); set(gca,'XTick',10.^[-11:1:-7]); %change
but I'm still missing the minor gridlines to the far right. Any ideas?
Thanks!!

Accepted Answer

dpb
dpb on 24 Jul 2014
AFAIK only drawing them manually or increasing the width. Here experimentally I've determined that the internal logic will draw them with
pos(3)>=325;
anything smaller they aren't displayed. You could experiment and see where you lose them and see if can stand the size to make them show automatically. I did something like
for i=1:10,pos(3)=pos(3)-2;set(1,'Position',pos),pause(2),disp(pos(3)),end
and ^-C'ed out when it failed from a starting point that displayed them to find the magic number.
Alternatively, to draw the missing ones directly if can't afford any more real estate at all,
xl=repmat([2:3]*1e-8,2,1);
yl=repmat([3 5].',1,2);
line(xl,yl,'linestyle',':','color','k')
Obviously in general retrieve the limits and compute the locations based on the actual plot parameters instead of using the fixed values specific for the particular case here...
  1 Comment
Namaka
Namaka on 25 Jul 2014
Edited: Namaka on 25 Jul 2014
Thanks dpb.
I've generalized this and added tickmarks too since they are missing. Here is the code in case It's helpful to anyone else. Any improvements are welcome :)
function add_missing_logxgrid(g)
%Adds ticks and gridlines to a semilog plot
% g = the gca
limx = get(g,'XLim');
limy = get(g,'YLim');
min10 = 10.^(floor(log10(limx(1))));
max10 = 10.^(floor(log10(limx(2))));
lim_rem = limx/min10;
%finds ticks
ticks_new = [];
max_tick = max10-1;
while max_tick<=max10*10 | isempty(ticks_new)
ticks_new = [ticks_new,[lim_rem(1):1:10].*min10];
min10 = min10.*10;
lim_rem(1) = 1;
max_tick = max(ticks_new);
end
%makes gridlines
x = repmat(ticks_new,2,1);
y = repmat(limy',1,size(x,2));
line(x,y,'linestyle',':','color','r')
%adds ticks
span = limy(2)-limy(1);
line(x,[y(2,:);y(2,:)-span/200],'color','k')
line(x,[y(1,:);y(1,:)+span/200],'color','k')
end

Sign in to comment.

More Answers (1)

dpb
dpb on 24 Jul 2014
Edited: dpb on 24 Jul 2014
It's a fignewton of the handle graphics and incomplete log cycles(*). AFAIK other than drawing the lines manually, all you can do is
xlim([3e-11),1e-7])
and they'll reappear. I've noticed this in the past--it'll complete them as long as the upper limit is the next power of 10 so you can chop off on the left but not on the right.
Mayhaps that's worthy of a bug report/enhancement request--I've not done so; don't know if it has been submitted previously or not.
ADDENDUM
(*) In conjunction with narrow-width plotting. At a certain point they do show up again despite the incomplete log cycle. It seems this may well be a bug rather than an intended feature; probably worth report to official TMW support at www.mathworks.com
  1 Comment
Namaka
Namaka on 24 Jul 2014
Thanks for the comment. I need the final limits of the plot to be xlim([3*10^(-11),4*10^(-8)]), not xlim([3*10^(-11),10^(-8)]). Is there any work around that will allow me to accomplish this within Matlab?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!