How to combine multiple plots in one graph?

Hello all. I wanted to know what I'm missing in combining three Lorenz curves into one graph. We are using a psuedocode to find the Lorenz curve, but I was wondering how I can combine the three curves into one graph.The code below is what I've done so far. Each respective plot is referring to different sets of data, but again, the main thing I'd like to know is how to combine three plots into a single graph. Or maybe the code below has some inconsistences as well. As of now, the code I've done below produces three different graphs. Thanks! I know it's a strange thing to ask.
close all;
T = readtable('CSV2006.CSV', 'HeaderLines1',1);
Data = table2array(T);
a = Data (:,1);
b = Data (:,2);
p = Data (:,3);
[xa, ya] = my_lorenz(a, b, p)
plot (xa, ya, '-b')
hold on;
T = readtable('CSV2011.CSV', 'HeaderLines',1);
Data = table2array(T);
a = Data (:,1);
b = Data (:,2);
p = Data (:,3);
[xb, yb] = my_lorenz(a,b,p)
plot (xb, yb, '-c');
T = readtable('CSV2016.CSV', 'HeaderLines',1);
Data = table2array(T);
a = Data (:,1);
b = Data (:,2);
p = Data (:,3);
[xc,yc] = my_lorenz(a,b,p)
plot (xc, yc, '-y');
hold off;

 Accepted Answer

The code looks like it should add all three plots on the same figure, but we don't know what my_lorenz is doing.
% Create data
y1 = -5:5;
y2=y1.^2;
y3 = y1.^3;
plot(y1)
hold on
plot(y2)
plot(y3)
hold off

3 Comments

function [x,y] = my_lorenz(a,b,p)
N = length(a);
m = (a+b)/2;
income = m.*p;
for i = 1:N
income_acc(i) = sum(income(1:1:i));
prop_acc(i) = sum(p(1:1:i));
end
income_acc = income_acc/income_acc(N);
prop_acc = prop_acc/prop_acc(N);
income_acc=[0,income_acc];
prop_acc=[0,prop_acc];
x = prop_acc;
y = income_acc;
figure;
hold on;
plot (x,y, '-o');
plot ([0 1], [0 1], '-r');
grid on;
end
Oh right, this is what is written in my_lorenz. My bad.
You have a figure command at the bottom of your function. That creates a new figure every time the function is called.
Because you don't specify a target axes in your plot commands, it always uses the current axes, which is the last one created. Try commenting out the last 5 lines of your function and see if you get the results you want.
Thank you Cris! It worked. I'll keep that in mind next time.
Cheers!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 28 Oct 2021

Commented:

on 28 Oct 2021

Community Treasure Hunt

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

Start Hunting!