Matlab:设置较小的网格样式和两个y轴时的刻度问题 [英] Matlab: Problem with ticks when setting minor grid style and two y-axis
本文介绍了Matlab:设置较小的网格样式和两个y轴时的刻度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在社区的帮助下,在此主题:小型网格与实线线&灰色
我得到它的工作设置小网格线为实体和彩色风格。但是当添加第二个y轴时,它只会混乱右轴上的y刻度! heres示例代码:
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];
%#创建第二个透明轴,相同的位置/范围,相同的刻度和标签
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'));
%#显示第一轴的网格线,给它们所需的颜色,但隐藏文本标签
set(ax1,'XColor',col,'YColor',col,...
'XMinorGrid','on','YMinorGrid','on',...
'MinorGridLineStyle',' - ',...
'XTickLabel',[], YTickLabel',[]);
%#链接两个轴以在平移/缩放上共享相同的限制
linkaxes([ax1 ax2],'xy');
ax3 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation' ,'right',...
'Color','none',...
'XTickLabel',[],...
'XColor','k' YColor','k');
%#链接两个轴以在平移/缩放上共享相同的限制
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'
。
更多,您可以简化该部分并创建为:
ax2 = copyobj(ax1,myfig);
delete(get(ax2,'Children'))
set(ax2,'Color','none','Box','off')
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:
解决方案
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屋!
查看全文