Matlab:设置次要网格样式和两个 y 轴时出现刻度问题 [英] Matlab: Problem with ticks when setting minor grid style and two y-axis

查看:24
本文介绍了Matlab:设置次要网格样式和两个 y 轴时出现刻度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此线程中的社区帮助下:带实线的小网格线&灰色

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

我可以将次要网格线设置为纯色和彩色样式.但是当添加第二个 y 轴时,它只会弄乱右轴上的 y 刻度!下面是示例代码:

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')

以及它的外观:

推荐答案

比你想象的要简单:在创建第二个轴ax2时,设置'Box'属性为 'off' 而不是 'on'.

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')

这篇关于Matlab:设置次要网格样式和两个 y 轴时出现刻度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆