Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different right and left axes in a MATLAB plot?

Tags:

plot

matlab

I plot a single trace in MATLAB with plot(). I'd like to add a right-y axis with a different set of tick marks (scaled linearly). Is this possible?

like image 639
AndyL Avatar asked Apr 20 '10 14:04

AndyL


People also ask

How do I change the direction of my axis in MATLAB?

Control the direction of increasing values along the x-axis and y-axis by setting the XDir and YDir properties of the Axes object. Set these properties to either 'reverse' or 'normal' (the default). Use the gca command to access the Axes object. stem(1:10) ax = gca; ax.


1 Answers

There are a number of good suggestions on this closely related question, although they deal with a more complicated situation than yours. If you want a super-simple DIY solution, you can try this:

plot(rand(1, 10));       % Plot some random data
ylabel(gca, 'scale 1');  % Add a label to the left y axis
set(gca, 'Box', 'off');  % Turn off the box surrounding the whole axes
axesPosition = get(gca, 'Position');           % Get the current axes position
hNewAxes = axes('Position', axesPosition, ...  % Place a new axes on top...
                'Color', 'none', ...           %   ... with no background color
                'YLim', [0 10], ...            %   ... and a different scale
                'YAxisLocation', 'right', ...  %   ... located on the right
                'XTick', [], ...               %   ... with no x tick marks
                'Box', 'off');                 %   ... and no surrounding box
ylabel(hNewAxes, 'scale 2');  % Add a label to the right y axis

And here's what you should get:

enter image description here

like image 86
gnovice Avatar answered Oct 21 '22 07:10

gnovice