tim
tim

Reputation: 10176

Matlab: Problem with ticks when setting minor grid style and two y-axis

with help of the community in this thread: Minor grid with solid lines & grey-color

I got it to work to set minor grid lines as solid and coloured style. But when adding a second y-axes it just messes up the y-ticks on the right axis! heres the example code:

x = linspace(0, 10, 11);
y1 = x.^3+1;
y2 = x+1;
y3 = y1./y2+5;

% plotte: http://www.mathworks.com/help/techdoc/ref/linespec.html
myfig = figure('Position', [500 500 445 356]); %[left, bottom, width, height]:
ax1 = axes('Position',[0.13 0.18 0.75 0.75]);
hold on

p1 = plot(x,y1,'x--r');
p2 = plot(x,y2,'*-b');

xlim([0 max(x)]);
ylim([0 max([max(y1) max(y2)])]);


col=.85*[1 1 1];
%# create a second transparent axis, same position/extents, same ticks and labels
ax2 = axes('Position',get(ax1,'Position'), ...
    'Color','none', 'Box','on', ...
    'XTickLabel',get(ax1,'XTickLabel'), 'YTickLabel',get(ax1,'YTickLabel'), ...
    'XTick',get(ax1,'XTick'), 'YTick',get(ax1,'YTick'), ...
    'XLim',get(ax1,'XLim'), 'YLim',get(ax1,'YLim'));

%# show grid-lines of first axis, give them desired color, but hide text labels
set(ax1, 'XColor',col, 'YColor',col, ...
    'XMinorGrid','on', 'YMinorGrid','on', ...
    'MinorGridLineStyle','-', ...
    'XTickLabel',[], 'YTickLabel',[]);


%# link the two axes to share the same limits on pan/zoom
linkaxes([ax1 ax2],'xy');

ax3 = axes('Position',get(ax1,'Position'),...
       'XAxisLocation','top',...
       'YAxisLocation','right',...
       'Color','none',...
       'XTickLabel', [],...
       'XColor','k','YColor','k');

%# link the two axes to share the same limits on pan/zoom
linkaxes([ax1 ax2 ax3], 'x');

ylabel(ax3, 'Speedup []');
ylim(ax3, [0 max(y3)]);

hold on
p3 = plot(x,y3,'s-.m','Parent',ax3);
hleg = legend([p1 p2 p3], {'CPU', 'GPU', 'Speedup'}, 'Location', 'NorthWest');
xlabel(ax2, 'N_{Funcs}');
ylabel(ax2, 't [s]'); 

set(hleg, 'FontAngle', 'italic')

and how it looks like:

enter image description here

Upvotes: 0

Views: 3687

Answers (2)

Amro
Amro

Reputation: 124563

Its simpler than you think: when you create the second axis ax2, set the 'Box' property to 'off' instead of 'on'.

Even more, you can simplify that part and create it as:

ax2 = copyobj(ax1,myfig);
delete( get(ax2,'Children') )
set(ax2, 'Color','none', 'Box','off')

enter image description here

Upvotes: 3

YYC
YYC

Reputation: 1802

The 2nd y-axis is "messed up" because the automatically generated YTick from y3 does not agree with the YTick from y1 and y2.

If this view is final (meaning you don't have to zoom in/zoom out or move the plot), you can manually define the YTick of ax3 to match those of ax1

ax3 = axes('Position',get(ax1,'Position'),...
   'XAxisLocation','top',...
   'YAxisLocation','right',...
   'Color','none',...
   'XTickLabel', [],...
   'YTick', [0:max(y3)/5:max(y3)], ...  %% Define 6 YTick (including 0) like ax1
   'XColor','k','YColor','k');

Upvotes: 1

Related Questions