![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288601/image.png)
Illustrate two close curves
25 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Minas Emiris
le 28 Avr 2020
Commenté : Minas Emiris
le 1 Mai 2020
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 commentaires
Réponse acceptée
Ameer Hamza
le 1 Mai 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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289367/image.png)
2 commentaires
Ameer Hamza
le 1 Mai 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.
Plus de réponses (1)
John D'Errico
le 1 Mai 2020
Modifié(e) : John D'Errico
le 1 Mai 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289370/image.jpeg)
Yes, you can use different colors. but if they over lay each other so closely, nothing will really help.
H2.LineWidth = 2;
H2.LineStyle = '--';
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289371/image.jpeg)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/289374/image.jpeg)
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.
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!