Matlabs plotyy with an axis for each graph on one side only

The following code shows my problem. plotyy completely fails if the tics are not in the same position on both sides (which is more likely the normal case ...)

I need a chart with two y axis, but ticks only on one side. I was asked to use addaxis, but I do not see how this helps me, since I do not want a dedicated axis.

clf;
clc;
xaxis = 0:0.1:25;
ydata1 = linspace(12.1712,12.7679, length(xaxis));
ydata2 = linspace(0.3597,-28.7745, length(xaxis));

[AX,H1,H2] = plotyy(xaxis, ydata1, xaxis, ydata2);

% axis limits - x axis (min to max)
xlimits(1) = min(xaxis); xlimits(2) = max(xaxis);
set(AX, 'XLim', xlimits);
set(AX(2),'XTick',[]);

% y1 axis limits 
ylimits(1) = min(ydata1); ylimits(2) = max(ydata1);
ylimits(2) = ylimits(2) + (ylimits(2)-ylimits(1))*0.05;
set(AX(1), 'YLim', ylimits);

% y2 axis limits 
ylimits(1) = min(ydata2); ylimits(2) = max(ydata2);
ylimits(2) = ylimits(2) + (ylimits(2)-ylimits(1))*0.05;
set(AX(2), 'YLim', ylimits);

% y1 ticks 
set(AX(1),'YTick',[12.0:0.1:12.8]);
% y2 ticks 
set(AX(2),'YTick',[-25:5:0]);

print(gcf, ['-r' num2str(400)], ['test' '.png' ], ['-d' 'png']);

enter image description here

+5
source share
4 answers

Here's the approach I got from the mathworks forum . The idea is to remove the box property that creates ticks on the opposite side.

set(AX(1),'Box','off') % Turn off box of axis 1, which removes its right-hand ticks
set(AX(2),'Box','off') % Turn off box of axis 2, which removes its left-hand ticks

, . - , , . Mybe

enter image description here

0

,

XAxisLocation XTickLabel. :)

set(AX(2),'XAxisLocation','top', 'XTickLabel',[])
+2

:

 set(AX(2),'YTick',[]);

 set(AX(1),'YTick',[]);

(1). []:

clf;
clc;
xaxis = 0:0.1:25;
ydata1 = linspace(12.1712,12.7679, length(xaxis));
ydata2 = linspace(0.3597,-28.7745, length(xaxis));

[AX,H1,H2] = plotyy(xaxis, ydata1, xaxis, ydata2);

% axis limits - x axis (min to max)
xlimits(1) = min(xaxis); xlimits(2) = max(xaxis);
set(AX, 'XLim', xlimits);
set(AX(2),'XTick',[]);

% y1 axis limits 
ylimits(1) = min(ydata1); ylimits(2) = max(ydata1);
ylimits(2) = ylimits(2) + (ylimits(2)-ylimits(1))*0.05;
set(AX(1), 'YLim', ylimits);

x  = linspace(ylimits(1),ylimits(2),10);
ticks1 =  arrayfun(@(t){sprintf('%2.2f',t)},x);


% y2 axis limits 
ylimits(1) = min(ydata2); ylimits(2) = max(ydata2);
ylimits(2) = ylimits(2) + (ylimits(2)-ylimits(1))*0.05;
x  = linspace(ylimits(1),ylimits(2),10);
ticks2 =  arrayfun(@(t){sprintf('%2.2f',t)},x);
set(AX(2), 'YLim', ylimits);

ticks = cell(size(ticks1));
for i=1:numel(ticks1)
    ticks{i} = sprintf('%s / %s',ticks1{i},ticks2{i});
end
% y1 ticks 
set(AX(1),'YTickLabel',ticks);
% % y2 ticks 
set(AX(2),'YTick',[]);
0

. ,

set(ax(1),'Box','off') % Turn off box of axis 1, which removes its right-hand ticks
set(ax(2),'Box','off') % Turn off box of axis 2, which removes its left-hand ticks

.

    ax3 = axes( 'Position',         get(ax(1), 'Position'),...
                'XAxisLocation',    'top',...
                'XTickLabel',       my_XTickLabels_on_top,...
                'YColor',           'none',...
                'YTick',            [],...
                'YTickLabel',       [],...
                'Color',            'none', ...
                cell_with_further_pValPairs{:});

'x' . .

    linkaxes([ax ax3], 'x')

, , , , MATLAB 2014b "" ,

    axes(ax)

:

enter image description here

0

All Articles