Different right and left axes in a MATLAB plot?

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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?

回答1:

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:



回答2:

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

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



回答3:

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



回答4:

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.



回答5:

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



回答6:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!