【问题标题】:Different right and left axes in a MATLAB plot?MATLAB图中的左右轴不同?
【发布时间】:2010-04-20 14:39:42
【问题描述】:

我在 MATLAB 中使用plot() 绘制了一条迹线。我想添加一个带有不同刻度线的右 y 轴(线性缩放)。这可能吗?

【问题讨论】:

  • 你可以在这个重复的问题上找到很多解决方案:stackoverflow.com/questions/1719048/…
  • ...虽然回想起来,我有点犹豫是否称它为 exact 副本。它处理更复杂的情况,但那里的解决方案仍然适用(有些需要一些修改以适应您的问题)。

标签: matlab plot


【解决方案1】:

this closely related question 上有很多很好的建议,尽管它们处理的情况比你的更复杂。如果你想要一个超级简单的 DIY 解决方案,你可以试试这个:

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

这就是你应该得到的:

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      Jiro 的解决方案很好(文件交换功能),但是它不允许使用 Matlab 的内置绘图功能(条形图、散点图等),您必须改用 plot2axes。 Matlab 自己的帮助提供了在任何类型的图上都有两个轴的解决方案: ax2 = 轴('位置',get(ax1,'位置'),... 'XAxisLocation','顶部',... 'YAxisLocation','对',... '颜色','无',... 'XColor','k','YColor','k');

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

      【讨论】:

      • 您所指的页面已不存在。
      【解决方案4】:

      使用 F1 打开 MATLAB 帮助并查看您提到的函数 plot 下面的函数,在那里您将看到 plotyy。这可能是您需要的。

      更新:实际上plotyy 不是 gnovice 所指出问题的答案。

      【讨论】:

      • 谢谢你,虽然我觉得很奇怪 plotyy 实际上需要你绘制数据两次才能获得预期的效果。
      • PLOTYY 函数绘制 两条 线,每条线都有自己的 y 刻度。为了获得一条带有两个 y 刻度的线,您可能需要做一些棘手的事情(例如绘制两条线,将第二条线缩放到所需范围,然后使其不可见)。
      【解决方案5】:

      从 matlab 2016 及以后的版本中,有一个选项可以定义在哪个轴上绘制:

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

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

      【讨论】:

        【解决方案6】:

        在绘制左轴图后,我能够做到这一点:

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

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-08
          • 1970-01-01
          • 2015-04-17
          • 2020-12-28
          • 2017-08-06
          • 2014-04-27
          • 2013-08-01
          相关资源
          最近更新 更多