在Matlab图中叠加两个轴

Fox*_*son 8 matlab plot

我正在寻找一种方法来覆盖xy时间序列,比如用'plot'创建,在'contourf'生成的显示上,在y轴上有不同的缩放.

似乎在两个xy图的情况下执行此操作的典型方法是使用内置函数"plotyy",甚至可以由"plot"之外的函数(例如"loglog")驱动,只要输入参数保持不变(x,y).但是,因为在我的情况下,contourf需要三个输入参数,'plotyy'似乎不适用.以下是一些示例代码,描述了我想要做的事情:

x1 = 1:1:50;
y1 = 1:1:10;
temp_data = rand(10,50);
y2 = rand(50,1)*20;
figure; hold on;
contourf(x1,y1,temp_data);
colormap('gray'); 
plot(x1,y2,'r-');
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望时间序列(x1,y2)在右侧显示自己的y轴,并缩放到与contourf图相同的垂直范围.

谢谢你的时间.

Dan*_*den 6

我不认为有一种"干净"的方法可以做到这一点,但你可以通过将两个轴重叠在一起来伪造它.

x1 = 1:1:50;
y1 = 1:1:10;
temp_data = rand(10,50);
y2 = rand(50,1)*20;
figure;
contourf(x1, y1, temp_data);
colormap('gray');
h_ax = gca;
h_ax_line = axes('position', get(h_ax, 'position')); % Create a new axes in the same position as the first one, overlaid on top
plot(x1,y2,'r-');
set(h_ax_line, 'YAxisLocation', 'right', 'xlim', get(h_ax, 'xlim'), 'color', 'none'); % Put the new axes' y labels on the right, set the x limits the same as the original axes', and make the background transparent
ylabel(h_ax, 'Contour y-values');
ylabel(h_ax_line, 'Line y-values');
Run Code Online (Sandbox Code Playgroud)

事实上,这个"情节叠加"几乎肯定是plotyy内部函数的作用.

这是示例输出(我增加了易读性的字体大小): 重叠的轴