Different right and left axes in a MATLAB plot?

后端 未结 6 821
我在风中等你
我在风中等你 2020-12-05 15:28

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?

相关标签:
6条回答
  • 2020-12-05 16:11

    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.

    0 讨论(0)
  • 2020-12-05 16:12

    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

    0 讨论(0)
  • 2020-12-05 16:13

    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 讨论(0)
  • 2020-12-05 16:15

    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:

    0 讨论(0)
  • 2020-12-05 16:16

    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

    0 讨论(0)
  • 2020-12-05 16:23

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

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

    0 讨论(0)
提交回复
热议问题