Matlab: проблема с галочками при установке второстепенного стиля сетки и двух осей Y - PullRequest
0 голосов
/ 05 июля 2011

с помощью сообщества в этой теме: Незначительная сетка со сплошными линиями и серым цветом

Я получил его, чтобы установить второстепенные линии сетки в виде сплошного и цветного стиля.Но при добавлении второй оси 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')

и как это выглядит:

enter image description here

Ответы [ 2 ]

3 голосов
/ 05 июля 2011

Это проще, чем вы думаете: когда вы создаете вторую ось ax2, установите для свойства 'Box' значение 'off' вместо 'on'.

Более того, вы можете упростить эту часть исоздайте его как:

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

enter image description here

1 голос
/ 05 июля 2011

2-я ось y "запуталась", потому что автоматически сгенерированные YTick из y3 не согласуются с YTick из y1 и y2.

Если это представление является окончательным (то есть вам не нужно увеличивать / уменьшать или перемещать график), вы можете вручную определить YTick из ax3, чтобы сопоставить его с 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');
...