在同一图中绘制多个相似数据 [英] Plot multiple similar data in same graph

查看:102
本文介绍了在同一图中绘制多个相似数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个图(X与Y),Z值取决于Y.示例如下图所示. X的矩阵大小与Z相同,但与Y相同.我可以将Z与X作图,但我想将所有图组合成一个图,并与Y对Y成图.我可以将多个图绘制成一个图,但该图彼此重叠.

I wanted to generate a plot (X vs Y), and Z values depend on the Y. The example is shown in the figure below. The matrix size of X is same with Z but not Y. I can plot Z against X, but I wanted to combine all the plot into a single plot and become Y against X. I can plot multiple plots into a single plot but the plot is overlapping each other.

我的问题是,有什么方法可以将多个图合并成一个图而不会重叠每个图,因为每个图之间的差异非常小(例如Z1 = 1,2,3,4,5和Z2 = 1.0001,2.0002 ,3.0001,4.0002,5.0001).因此,我想将每个Z绘图设置为不同的Y轴. (例如,Y = 1时为Z1,Y = 2时为Z2 ...)

My question is there any method I can merge multiple plots into a single plot without overlapping each plot as the difference between each plot is very small (e.g Z1=1,2,3,4,5 and Z2=1.0001,2.0002,3.0001,4.0002,5.0001). So, I wanted to set each Z plot at different Y axis. (e.g Z1 at Y=0, Z2 at Y=2 ...)

有人有什么建议或想法吗?

Does anyone have any suggestions or idea?

谢谢

推荐答案

我将澄清我在评论中写的想法. 首先,让我们获取一些数据:

I'll clarify the ideas I wrote in a comment. First, let's get some data:

x = 470:0.1:484;
z1 = cos(x)/2;
z2 = sin(x)/3;
z3 = cos(x+0.2)/2.3;

我将只绘制三个数据集,所有这些对于将其扩展到任意数量的数据集都是微不足道的.

I'll plot just three data sets, all of this is trivial to extend to any number of data sets.

这里的想法是简单地使用subplot来创建小倍数类型的图:

The idea here is simply to use subplot to create a small-multiple type plot:

ytick = [-0.5,0.0,0.5];
ylim = [-0.9,0.9]);
figure

h1 = subplot(3,1,1);
plot(x,z1);
set(h1,'ylim',ylim,'ytick',ytick);
title('z1')

h2 = subplot(3,1,2);
plot(x,z2);
set(h2,'ylim',ylim,'ytick',ytick);
title('z2')

h3 = subplot(3,1,3);
plot(x,z3);
set(h3,'ylim',ylim,'ytick',ytick);
title('z3')

请注意,例如,可以从顶部的两个图中删除刻度线标签,仅在底部的底部留下标签.然后,您还可以移动轴,使它们靠得更近(如果同一图中有很多这些线,则可能有必要):

Note that it is possible to, e.g., remove the tick labels from the top two plot, leaving only labels on the bottom one. You can then also move the axes so that they are closer together (which might be necessary if there are lots of these lines in the same plot):

set(h1,'xticklabel',[],'box','off')
set(h2,'xticklabel',[],'box','off')
set(h3,'box','off')
set(h1,'position',[0.13,0.71,0.8,0.24])
set(h2,'position',[0.13,0.41,0.8,0.24])
set(h3,'position',[0.13,0.11,0.8,0.24])
axes(h1)
title('')
ylabel('z1')
axes(h2)
title('')
ylabel('z2')
axes(h3)
title('')
ylabel('z3')

这是更简单的方法,因为您只处理单个轴. @Zizy Archer已经表明,如果将数据全部放在单个2D矩阵Z中,则移动数据是多么容易.在这里,我将绘制z1z2+2z3+4.根据您的喜好调整偏移量.接下来,设置'ytick'属性以创建单独图形的错觉,并设置'yticklabel'属性,以使y轴上的数字与绘制的实际数据相匹配.最终结果类似于上面的多轴图,但是它们都在一个轴上:

This is the simpler approach, as you're dealing only with a single axis. @Zizy Archer already showed how easy it is to shift data if they're all in a single 2D matrix Z. Here I'll just plot z1, z2+2, and z3+4. Adjust the offsets to your liking. Next, I set the 'ytick' property to create the illusion of separate graphs, and set the 'yticklabel' property so that the numbers along the y-axis match the actual data plotted. The end result is similar to the multiple axes plots above, but they're all in a single axes:

figure
plot(x,z1);
hold on
plot(x,z2+2);
plot(x,z3+4);
ytick = [-0.5,0.0,0.5];
set(gca,'ytick',[ytick,ytick+2,ytick+4]);
set(gca,'yticklabel',[ytick,ytick,ytick]);
text(484.5,0,'z1')
text(484.5,2,'z2')
text(484.5,4,'z3')

这篇关于在同一图中绘制多个相似数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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