如何在matlab中显示两条重叠的线条

时间:2013-06-16 17:04:37

标签: matlab plot matlab-figure

我必须绘制5条在某些区域重叠的线条,我需要能够看到所有线条。

我可以考虑稍微移动线条以允许它们显示,但这似乎不是一个非常优雅的解决方案。即便如此,我怎么能编写这样的东西呢?

有没有其他方法可以绘制多条重叠的线条,同时能够在每个点区分它们?

例如,这是一个有3条重叠线的例子: enter image description here

提前谢谢!

3 个答案:

答案 0 :(得分:1)

另一种方法是使用透明度 不幸的是,线对象不服从 透明度命令:(

解决方法是:
 1.下载patchline(< - 链接到Matlab Central)
 2.用它来绘制具有透明度的补丁线

获得patchline后,您可以尝试以下内容:

% create some lines:
l1 = [1, 1, 1, 0, 0, 0.25, 1, 1, 0, 0, 0, 0, 1 1];
l2 = [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1];
l3 = [1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0];  

% plot with patchline (notice the use of 'EdgeAlpha'):
figure; 
patchline(1:length(l1), l1, [], 'EdgeColor', [0.8, 0.2, 0.35],...
    'LineWidth', 5, 'EdgeAlpha', 0.5 ); 
hold on;  

patchline(1:length(l1), l2, 'EdgeColor', [0.2, 0.7, 0.55],...
    'LineWidth', 5, 'EdgeAlpha', 0.5 );  

patchline(1:length(l1), l3, 'EdgeColor', [0.1, 0.2, 0.95],...
    'LineWidth', 5, 'EdgeAlpha', 0.5);  

% change y limits to see line overlap clearly
set(gca, 'YLim', [-0.5, 1.5]);

As you can see - this is far from perfect
不是理想的做法 - 粗糙的'裂缝'将保持这种状态,
但你可以尝试不同的线宽或移动
y轴上的线,其值对应于每个图像的图像 线只覆盖最近邻居的一半...

答案 1 :(得分:0)

您可以使用plot3并将不同的Z值分配给不同的重叠线。但是,如果你交换Y轴和Z轴,它看起来会更像你期望的(Z是"向上"方向):

示例:

Y1 = randn(1,100);
Y2 = randn(1,100);
X = 1:100;
Z1 = 1*ones(size(X));
Z2 = 2*ones(size(X));

plot3(X,Z1,Y1);
hold on;
plot3(X,Z2,Y2);

答案 2 :(得分:0)

您可以使用情节线的EraseMode属性。以下代码示例显示了如何移动行和EraseMode效果:

% First we generate some data
NLines      = 2;
NPoints     = 50;
LineWidth   = 3;
ShiftStep   = 1.1;
XData       = linspace(0,1,NPoints);
YData       = rand(NPoints,NLines);
for k=1:NLines
    YData(:,k) = YData(:,k) > (k/(NLines+1));
end

% Then we create plot
figure('color','w');
subplot(3,1,1); plot(XData,YData, 'LineWidth',LineWidth);
                ylim([-0.1 1.1]);
                title('simple')
subplot(3,1,2); plot(XData,YData+repmat((0:NLines-1)*ShiftStep,NPoints,1), 'LineWidth',LineWidth, 'EraseMode','xor');
                ylim([-0.1 1.1+ShiftStep*(NLines-1)]);
                title('Shifted vertically')
subplot(3,1,3); plot(XData,YData, 'LineWidth',LineWidth, 'EraseMode','xor');
                ylim([-0.1 1.1]);
                title('EraseMode = xor')

在我看来,如果你有三条以上的线条与你的情节类似,那么转移在视觉上更具吸引力。你也可以创建几个轴(就像我做的那样)并在每个轴上绘制每条线,因此它们会相应地设置y标签,但它们的X标签基本相同。