我在MATLAB中使用plot()
函数绘制单条线。我想添加一个右侧的y轴,并用不同的刻度标记(按线性比例缩放)。这个可能吗?
在这个相关问题上提出了许多很好的建议,虽然它们涉及到的情况比你的更加复杂。如果你想尝试一个超级简单的自助解决方案,可以尝试这个:
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
这是您应该得到的内容:
从Matlab 2016版本开始,有一个选项可以定义在哪个轴上绘制:
yyaxis left
plots...
yyaxis right
plots...
source: https://se.mathworks.com/help/matlab/ref/yyaxis.html
yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph
希望能有所帮助。