Daniel
Daniel

Reputation: 165

stop matlab line plots from overlapping

I plot many lines on top of one another, using plot and hold on however i want one of the lines to be shifted a bit if it falls on another line. for example in the following case:

plot(1:100); hold on; plot(-100:100,abs(-100:100))

i want it to be clear that there are 2 plots here i tried to simply increase x values for different plots but this skews the data too much

for z=1:numberofplots
plot((1:size(locations,2))+0.1*z,locations(z,:)','color', altclrz(z,:));
end

Upvotes: 3

Views: 3845

Answers (1)

Steve
Steve

Reputation: 4097

You can differentiate the curves in several ways:

-1- Skewing the data

As you said, you could shift the data a bit. I would suggest fixing your axis and then calculating how many units in linewidth is so you get a very tight fit, like this:

lineWidth = 5;

figure(33);
clf;
subplot(1,2,1);
h = plot(myData, 'linewidth', lineWidth);
xlim([1,5]);
ylim([1,5]);
title('Original');

myData = meshgrid(1:5)';

myLimDiff = diff(ylim);
set(gca,'units', 'pixels');
myPos = get(gca, 'position')
myWidthHeight= myPos(3:4)

PixelsPerUnit =myWidthHeight(2)./ myLimDiff;
myDataSkewed = myData + meshgrid(-2:2)*1/PixelsPerUnit(1)*lineWidth;

subplot(1,2,2);
plot(myDataSkewed, 'linewidth', lineWidth);
xlim([1,5]);
ylim([1,5]);
title('Skewed');

Result:

enter image description here

-2- Using solid lines and Dashes

As someone else noted in the comments, you could you a dashed line over a solid line, or some combination of styles.

-3- Using different line thickness

Use different line widths with the thickest on the bottom:

figure(54);
clf
hold all
for ind = 10:-3:1
    plot(1:5, 'linewidth', ind);
end

enter image description here

-4- Use separate plots for each line with a twist

Another way to call out each line is to plot each line in a subplot but to plot all the data in gray first. This way you can see where all the lines are with each particular line called out:

enter image description here

figure(55);
clf
data = rand(3);

for ind = 1:3    
    subplot(1,3,ind);
    plot(data, 'linewidth', 4, 'color', [1 1 1]*.75);
    hold on
    plot(data(:,ind), 'linewidth', 2);
end

Upvotes: 5

Related Questions