开发者

Different right and left axes in a MATLAB plot?

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 possi开发者_StackOverflow社区ble?


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:

Different right and left axes in a MATLAB plot?


You may try this submission to MATLAB File Exchange - PLOT2AXES.

PLOT2AXES example http://www.mathworks.com/matlabcentral/fx_files/7426/2/plot2axes.png


Jiro's solution is good (file Exchange function), however, it does not allow to use Matlab's built-in plot functions (bar, scatter, etc.), and you have to use plot2axes instead. Matlab's own help gives the solution to have two axes on any type of plots: ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none',... 'XColor','k','YColor','k');

Look at: http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html


Open MATLAB Help with F1 and take a look at the functions below function plot which you mentioned, there you will see plotyy. This is what you probably need.

UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.


From matlab 2016 and onwards there is an option to define on what axis one plots:

yyaxis left
plots...
yyaxis right
plots...

source: https://se.mathworks.com/help/matlab/ref/yyaxis.html


I was able to do it with the following after plotting the left axis graph:

yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜