MATLAB Answers

Illustrate two close curves

20 views (last 30 days)
Hi all, I have I was wondering what is a good way of plotting two curves that are close to each other. E.g.:
x = linspace (0,100,101); % x-coordinates
a1 = 1;
a2 = 1.001;
y1 = x*a1;
y2 = x*a2;
Plotting them as follows clearly does not help to distinuish between the two curves easily:
plot(x,a1,x,a2)
I don't want to zoom the plot, or plot their difference, as this does not serve my goal. I reckon using logarithmic axes might help, but:
set(gca,'YScale','log');
or
set(gca,'XScale','log');
or both of the above did not help. Any ideas?

  3 Comments

Adam Danz
Adam Danz on 29 Apr 2020
Another option is to change the LineWidth and LineStyle properties.
Minas Emiris
Minas Emiris on 1 May 2020
Ameer’s code is what I was looking for. Using Linewidth will not really help as I want to shade the area between these curves. Thank you! Ps.: it appears that I cannot accept it as an answer.
Ameer Hamza
Ameer Hamza on 1 May 2020
Minas, I have posted that as an answer. You can accept it.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 May 2020
At this closeness, there is no good way to plot them on the same graph and distinguish them. One way is to use the different markers on both lines
x = linspace(0,100,101); % x-coordinates
a1 = 1;
a2 = 1.001;
y1 = x*a1;
y2 = x*a2;
plot(x,y1,'.-',x,y2,'+-')
Other way (which I prefer) is to create a small new axes and zoom on a small portion of the line to show the difference
x = linspace(0,100,101); % x-coordinates
a1 = 1;
a2 = 1.001;
y1 = x*a1;
y2 = x*a2;
fig = figure;
ax = axes();
plot(x,y1,x,y2);
x2 = linspace(39.6, 40);
y12 = x2*a1;
y22 = x2*a2;
ax_small = axes('Position', [0.25 0.55 0.25 0.25], ...
'Box', 'on');
plot(ax_small, x2, y12, x2, y22)
annotation(fig, 'rectangle', [0.425 0.368 0.017 0.017], ...
'LineWidth', 1);
annotation(fig, 'arrow', [0.421 0.382], [0.395 0.507], ...
'LineWidth',1,'HeadStyle','plain');

  2 Comments

John D'Errico
John D'Errico on 1 May 2020
+1 for the pretty trick of overlaying figures with an inset.
Ameer Hamza
Ameer Hamza on 1 May 2020
Thanks, I find this trick to be quite handy when writing papers, and information needs to be presented concisely due to limitations on the number of pages.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 1 May 2020
Edited: John D'Errico on 1 May 2020
There really is little way to visualize the difference between two curves that are so close together that they overlay completely on top of each other. That is, if the two curve are so close together that the width of the most narrow line possible on a monitor or your printer shows no difference, what can you do?
Perhaps an example or two will help, and I can show some things you might try.
y1 = @(x) 2 + 3*x + x.^2
y2 = @(x) 2.005 + 3.002*x + 0.999*x.^2
H1 = fplot(y1);
hold on
H2 = fplot(y2);
legend
Yes, you can use different colors. but if they over lay each other so closely, nothing will really help.
H2.LineWidth = 2;
H2.LineStyle = '--';
You can use two lines of different width, in different colors, where one of the lines is made intermittent, as I did in this ssecond figure. At least there it will become clear they are virtually overlaid.
But if you want to truly show they are different, then you may need to focus on the difference.
Hdiff = fplot(@(x) y2(x) - y1(x));
xlabel X
ylabel 'y2(x) - y1(x)'
grid on
legend
You can do that as a separate figure, or you can even inset it as Ameer did very nicely in the main figure.
My point is a picture paints different images, it teaches different things, depending on how you draw that picture. If you want to focus the attention of the reader on the idea that both lines are virtually identical, then you show them as overlapping so perfectly that one cannot see the difference between the lines. If you want to focus the attention to the difference between the curves, then plotting the difference, AS a difference will draw the focus to that aspect. Make sure you point out the small scaling on the y axis in the difference plot.
If you want to focus the attention of the person to both aspects, then plot both figures. You can do that as separate figures, or overlay the figures.

  1 Comment

Minas Emiris
Minas Emiris on 1 May 2020
That’s a good idea, perhaps my mistake I didn’t further specify why I need to resolve these lines-but preferred not to make my statement chaotic: The vertical difference between the curves represents the standard deviation from a simulation, while the “y” value is related to the mean. For this reason, it’s required to plot both curves rather than just their difference- otherwise I’d have to make two plots. Unfortunately, I need these for a report and I currently need to save space. Thank you for your code!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!