How to merge two plots into one

I have the following data, related to the given script
% Plot profiles
load pressure_displacement_profiles
% Plot initial -Cp and shape
fig1=figure;
plot(initial.x,-initial.cp,'b-'); hold on;
a = plot(initial.x,initial.disp,'k-');
legend('cp','disp');
get(a)
% Plot optimal -Cp and shape
fig2=figure;
plot(optimal.x,-optimal.cp,'b-'); hold on;
b = plot(optimal.x,optimal.disp,'k-');
legend('cp','disp');
get(b);
% Save to eps
print(fig1,'-depsc2','Hwk1Prob3_starter1');
print(fig2,'-depsc2','Hwk1Prob3_starter2');
How do i show both Optimal and initial plot on the same graph?

 Accepted Answer

Adam Danz
Adam Danz on 24 Apr 2019
Edited: Adam Danz on 24 Apr 2019
To put both axes on the same figure, use subplot()
% Plot initial -Cp and shape
fig1 = figure;
subplot(2,1,1)
plot(initial.x,-initial.cp,'b-');
hold on;
plot(initial.x,initial.disp,'k-');
legend('cp','disp');
% Plot optimal -Cp and shape
subplot(2,1,2)
plot(optimal.x,-optimal.cp,'b-');
hold on;
plot(optimal.x,optimal.disp,'k-');
legend('cp','disp');
% Save to eps
print(fig1,'-depsc2','Hwk1Prob3_starter1');
*Not tested
To put all objects on the same axes, use hold on
% Plot initial -Cp and shape
fig1 = figure;
p1 = plot(initial.x,-initial.cp,'b-');
hold on;
p2 = plot(initial.x,initial.disp,'k-');
% Plot optimal -Cp and shape
p3 = plot(optimal.x,-optimal.cp,'b-');
p4 = plot(optimal.x,optimal.disp,'k-');
legend([p1,p2,p3,p4], {'cp1','disp1','cp2','disp2'});
% Save to eps
print(fig1,'-depsc2','Hwk1Prob3_starter1');
*not tested

6 Comments

By merging I meant both of these curves to exist in the same plot rather than two plots on the same window.
You're already using "hold on". Just continue that logic by adding more objects to the axes.
% Plot initial -Cp and shape
fig1 = figure;
p1 = plot(initial.x,-initial.cp,'b-');
hold on;
p2 = plot(initial.x,initial.disp,'k-');
% Plot optimal -Cp and shape
p3 = plot(optimal.x,-optimal.cp,'b-');
p4 = plot(optimal.x,optimal.disp,'k-');
legend([p1,p2,p3,p4], {'cp1','disp1','cp2','disp2'});
% Save to eps
print(fig1,'-depsc2','Hwk1Prob3_starter1');
*not tested
I updated my answer to include this interpretation of the question.
That did work. Thanks for it.
Also, if I have to get Y-tick labels as "Distance traversed to airfoil", which property do I have to modify for correct spacing on Y axis? I checked all properties of the curve but I didn't understand half of them.
To modify the y tick labels, look at the properties of the axes not the properties of the line (curve).
For example, this will change the ticks along the yaxes
axHandle = gca; %get axis handle
axHandle.YTick = -2 : 0.5 : 1.5;
ylim([-2, 1.5])
After you set the ticks, you can change their labels if needed.
axHandle.YTickLabel = {'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'};
If I use the command
p1 = plot(initial.x,-initial.cp,'b-');
get(p1)
it shows all properties of the plot but axHandle is not one of them. Where do I find these properties which aren't mentioned in the plot itself, such as the properties of axes?
Some pointers
  1. get(p1) in your example lists all properties of the line, not the axes.
  2. to list properties of an axes, you need to get the axis handle. See the code below.
  3. "axHandle" is just the name I chose for the variable that stores the axis handle.
% Methods of getting axis handle
% 1) If and only if the axes are currently selected
axHandle = gca;
% 2) get the handle when you create the axes
figure
axHandle = axes;
% 3) get the handle from the line properties
h = plot(x,y);
axHandle = h.Parent;
And here's a description of all axes properties

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Asked:

on 24 Apr 2019

Edited:

on 30 Apr 2019

Community Treasure Hunt

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

Start Hunting!