在Matlab图中叠加两个轴 [英] Overlaying two axes in a Matlab plot

查看:149
本文介绍了在Matlab图中叠加两个轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以在由"contourf"生成的显示之上叠加一个用"plot"创建的x-y时间序列,并且在y轴上具有不同的缩放比例.

I am looking for a way to overlay an x-y time series, say created with 'plot', on top of a display generated by 'contourf', with different scaling on the y-axes.

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

It seems that the typical way to do this in the case of two x-y plots is to use the built-in function 'plotyy', which can even be driven by functions other than 'plot' (such as 'loglog') as long as the input arguments remain the same (x,y). However, since in my case contourf requires three input arguments, 'plotyy' seems to not be applicable. Here is some sample code describing what I would like to do:

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

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

Ideally, I would like the timeseries (x1,y2) to have its own y-axes displayed on the right, and be scaled to the same vertical extent as the contourf plot.

感谢您的时间.

推荐答案

我认为没有干净"的方法可以这样做,但是您可以通过将两个轴相互重叠来伪造它.

I don't think there's a "clean" way to do this, but you can fake it by overlaying two axes over each other.

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

实际上,这种图形叠加"几乎可以肯定是plotyy函数在内部执行的操作.

In fact, this "plot overlay" is almost definitely what the plotyy function does internally.

以下是示例输出(为清晰起见,我增加了字体大小):

Here's example output (I increased the font size for legibility):

这篇关于在Matlab图中叠加两个轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆