在MATLAB绘图中不同的左右坐标轴?

时间:2022-05-13 23:45:15

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?

我用plot()在MATLAB中绘制单个跟踪。我想添加一个右y轴与一组不同的刻度标记(线性缩放)。这是可能的吗?

6 个解决方案

#1


16  

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:

在这个密切相关的问题上有很多好的建议,尽管它们涉及的情况比你的复杂。如果你想要一个超级简单的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

And here's what you should get:

你应该得到的是:

在MATLAB绘图中不同的左右坐标轴?

#2


8  

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

您可以尝试提交到MATLAB文件交换- PLOT2AXES。

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

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

#3


2  

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');

Jiro的解决方案是好的(文件交换函数),但是它不允许使用Matlab内置的plot函数(bar, scatter等),你必须使用plot2axes。Matlab自己的帮助给出了在任意类型的图上有两个轴的解决方案:ax2 =坐标轴('Position',get(ax1,'Position'),……‘XAxisLocation’,‘前’,……“YAxisLocation”、“对”,……“颜色”,“没有”,…“XColor”、“k”,“YColor”、“k”);

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

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

#4


1  

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.

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

UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.

更新:实际上,plotyy并不是g初学者所指出的问题的答案。

#5


1  

From matlab 2016 and onwards there is an option to define on what axis one plots:

从matlab 2016开始,有一个选项可以定义第一个轴的图形:

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

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

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

#6


-1  

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.

希望它可以帮助。

#1


16  

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:

在这个密切相关的问题上有很多好的建议,尽管它们涉及的情况比你的复杂。如果你想要一个超级简单的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

And here's what you should get:

你应该得到的是:

在MATLAB绘图中不同的左右坐标轴?

#2


8  

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

您可以尝试提交到MATLAB文件交换- PLOT2AXES。

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

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

#3


2  

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');

Jiro的解决方案是好的(文件交换函数),但是它不允许使用Matlab内置的plot函数(bar, scatter等),你必须使用plot2axes。Matlab自己的帮助给出了在任意类型的图上有两个轴的解决方案:ax2 =坐标轴('Position',get(ax1,'Position'),……‘XAxisLocation’,‘前’,……“YAxisLocation”、“对”,……“颜色”,“没有”,…“XColor”、“k”,“YColor”、“k”);

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

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

#4


1  

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.

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

UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.

更新:实际上,plotyy并不是g初学者所指出的问题的答案。

#5


1  

From matlab 2016 and onwards there is an option to define on what axis one plots:

从matlab 2016开始,有一个选项可以定义第一个轴的图形:

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

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

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

#6


-1  

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.

希望它可以帮助。