Matlab:设置小网格样式和两个 y 轴时出现刻度问题

发布于 2024-11-18 10:55:48 字数 1938 浏览 3 评论 0原文

在此线程中的社区的帮助下: 带有实线的小网格和grey-color

我让它将小网格线设置为纯色和彩色样式。但是当添加第二个 y 轴时,它只会弄乱右轴上的 y 刻度!这是示例代码:

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

及其外观:

在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-11-25 10:55:48

它比您想象的更简单:当您创建第二个轴 ax2 时,将 'Box' 属性设置为 'off' 而不是 “开”

更重要的是,您可以简化该部分并将其创建为:

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

在此处输入图像描述

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

请帮我爱他 2024-11-25 10:55:48

第二个 y 轴“混乱”,因为从 y3 自动生成的 YTicky1< 的 YTick 不一致/code> 和 y2

如果此视图是最终视图(意味着您不必放大/缩小或移动绘图),您可以手动定义 ax3YTick 以匹配斧头1

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

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文