MATLAB绘图中的不同右侧和左侧轴?

13

我在MATLAB中使用plot()函数绘制单条线。我想添加一个右侧的y轴,并用不同的刻度标记(按线性比例缩放)。这个可能吗?


4
您可以在这个重复问题上找到许多解决方案:https://dev59.com/IXI-5IYBdhLWcg3wtaxq - gnovice
1
尽管回想起来,我有点犹豫称其为“完全”重复。它涉及到更加复杂的情况,但那里提供的解决方案仍然适用(有些需要进行一些修改以适应您的问题)。 - gnovice
6个回答

16

这个相关问题上提出了许多很好的建议,虽然它们涉及到的情况比你的更加复杂。如果你想尝试一个超级简单的自助解决方案,可以尝试这个:

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
Jiro的解决方案很好(文件交换功能),但是它不允许使用Matlab内置的绘图函数(如条形图、散点图等),你必须使用plot2axes代替。Matlab自己的帮助文档给出了在任何类型的绘图上拥有两个轴的解决方案: ax2 = axes('Position',get(ax1,'Position'),... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none',... 'XColor','k','YColor','k'); 请参考:http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html

您所引用的页面已不存在。 - Fraukje

2

1

按下F1键打开MATLAB帮助,查看下面提到的plot函数,你会看到plotyy。这可能是你需要的。

更新:实际上,正如gnovice所指出的那样,plotyy并不是问题的答案。


谢谢您的回复,尽管我觉得很奇怪plotyy需要你两次绘制数据才能达到预期效果。 - AndyL
PLOTYY函数绘制两条线,每条线都有自己的y轴刻度。为了获得一条带有两个y轴刻度的线,您可能需要做一些棘手的事情(例如绘制两条线,将第二条线缩放到所需范围,然后使其不可见)。 - gnovice

0
在绘制左轴图后,我能够使用以下内容完成它:
yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph

希望能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接