How to plot multiple data sets on the same graph - Complex Aggression study

I am relatively new to Matlab. I am aware how to use the hold functions to do basic plots however I can't work out how to implement this into the following code to achieve multiple sets of data but on the same plot. Any help will be greatly appreciated.
Edit: Some code such as 'neighbours' are functions I have created so if some of the code seems strange, that may also be why. - If anyone needs to see these in order to help please let me know. Once again thanks!
Edit 2: I have added all the code you need to run the experiment. If you do this you will see what I am trying to achieve (hopefully) - Thanks again for the responses! __________________________________________________________________________
Firstly the Neighbours function (save this as a function):
function nbrs = neighbours(m, n, bdry, conn, prnt)
% function nbrs = neighbours(m, n, bdry, conn)
%
% calculate adjacency list for image graph
%
% m, n = image dimensions (m x n)
% bdry = boundary condition 'closed' or 'torus'
% conn = connectivity '4' or '8'
% prnt = optional printout (print when prnt = 1)
%
% nbrs = cell array of lists of indices k = 1 to m*n
% nbrs{k} = [k1 k2 k3 ...]
% Lookup table for i, j values of the three
% [-1 0 1]-shifted versions of rows and coumns,
% NaN indicates neighbour is off edge
switch bdry
case 'closed'
I = [[NaN 1:m-1]; 1:m; [2:m NaN]];
J = [[NaN 1:n-1]; 1:n; [2:n NaN]];
case 'torus'
I = [[m 1:m-1]; 1:m; [2:m 1]];
J = [[n 1:n-1]; 1:n; [2:n 1]];
end
%%%%nested function to simplify coding below
function k = ind(i1, j1, i, j)
k = sub2ind([m n], I(i1, i), J(j1, j));
end
nbrs = {};
for k = 1:m*n
[i j] = ind2sub([m n], k);
switch conn
case '4' % fancy formatting so easy to check layout
nbr = [ind(1, 2, i, j) ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 2, i, j)];
case '8'
nbr = [ind(1, 1, i, j), ind(1, 2, i, j), ind(1, 3, i, j), ...
ind(2, 1, i, j),ind(2, 3, i, j), ...
ind(3, 1, i, j), ind(3, 2, i, j), ind(3, 3, i, j)];
end
nbrs{k} = nbr(~isnan(nbr)); % remove any missing (boundary) pixels
end
if prnt == 1
for k = 1:length(nbrs)
fprintf('%5d :', k); fprintf('%5d ', nbrs{k}); fprintf('\n')
end
end
end % function end
___________________________________________________________________
Here is the code for the function rand_binary
function im = rand_binary(m, n, prob)
% function im = rand_binary(prob)
%
% Make binary image with pixels randomly set to black
%
% m, n = image size (m x n)
% prob = probablity of black pixel
%
% im = returned image
im = double(rand(m, n) > prob);
__________________________________________________________________
And run_simulation function:
function [pop2, xagg] = run_simulation(pop1, sf, nbrs, niter, plt)
% function [pop2, xagg] = run_simulation(pop1, sf, nbrs, niter, sf, plt)
%
% pop1 = binary image for initial aggression map(aggressive = 0 = black)
% sf = binary image for short-fuse map (short-fuse = 0 = red-star)
% nbrs = adjacency list to specify network topology
% niter = number of updates to apply
% plt = plot flag (0 = no plots, 1 = ahow plots)
%
% pop2 = final aggresion map
% xagg = final proportion of aggressives
if nargin < 5
plt = 1;
end
[m n] = size(pop1);
if plt
plot_world(pop1, sf);
drawnow
end
a = 0; % aggressive
p = 1; % peaceful
pop2 = pop1;
for i = 1:niter
for k = 1:m*n
nbr = nbrs{k};
na = sum(pop1(nbr) == a); % number aggressive;
np = sum(pop1(nbr) == p); % number peaceful
if sf(k) == 0
if na >= 1
pop2(k) = a;
else
pop2(k) = p;
end
else
if na > np
pop2(k) = a;
elseif na < np
pop2(k) = p;
end % else stays unchanged
end
end
pop1 = pop2;
if plt
plot_world(pop1, sf);
title(sprintf('Day %d', i+1));
pause(0.2)
end
end
xagg = sum(pop2(:) == a)/(m*n);
____________________________________________________________________
Now this is the code to plot my graph. If you run this data as it is it should plot a smooth sigmoidal curve for a single sf value. However I want to compare the effect of different sf values by plotting their results on the same graph.
clear all % check this is stand-alone
% specify grid size and topology
m = 100;
n = 100;
nbrs = neighbours(m, n, 'closed', '4', 0);
% independent variable: proportion of aggressives
nsamp = 1000; % number of values of pagg to use
pagg = linspace(0, 1, nsamp); % pagg increases from 0 (none aggaggresive) to 1 (all aggresive)
% dependent variable xagg: the final proportion of aggresssives
xagg = NaN(1, nsamp); % xagg is storage for this number, one for each pagg value
% specify proportion of short-fuse individuals
psf = 0.2;
sf = rand_binary(m, n, psf);
% number of iterations
ndays = 20;
niter = ndays-1;
plt = 0; % no movie
figure(3); clf
for i = 1:nsamp
pop1 = rand_binary(m, n, pagg(i)); % make initial population with pagg(i)
[pop2, xagg(i)] = run_simulation(pop1, sf, nbrs, niter, plt); % get final population & proportion of aggressives
figure(3);
plot(pagg, xagg, '.', 'MarkerSize', 10)
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
drawnow
end
Any help with this problem would be greatly appreciated (sorry i haven't been all that clear, struggling to wrap my head around the code and the way to verbally express it).
Many thanks again, John

7 Commenti

If there any problems with running the code above, please let me know! Desperate to find a solution to this problem.
Many thanks to all!
Added it in, thanks for pointing that out!
...and run_simulation. I recommend reading this page.
Yeah... I appreciate what you mean. Im not really sure how to simplify it, really not that good at Matlab. Hopefully will get there eventually though!
while some of this code I had written, some of the functions I have used were coded by someone else.
Anyhow, I have added that in too.
Sorry about the confusion, John
I understand--when you're starting out it's hard to know which lines are absolutely necessary and which are not. I don't have Matlab with me, but next time I do I'll give your code another shot.
Thank you Chad, for both your help and patience with this,
really appreciate it!

Accedi per commentare.

Risposte (2)

if you want the points have similar color, use
hold on
if you want the points have alternating color, use
hold all
*put the hold command after the plot command

1 Commento

Hi, thanks for the reply.
Ive managed to use the hold functions previously to plot multiple plots on a single graph,
For some reason however, I am having a lot of issue implementing this into the above code to get the desired effect.
Any ideas?
Kind regards, John

Accedi per commentare.

A few notes:
1. Don't use i as a variable name. Sometimes Matlab thinks it's the imaginary unit.
2. Only call axis settings once. No need to keep making Matlab perform the same operation multiple times.
Run this:
figure(3); clf
for k = 1:20
x = rand(3,1);
y = rand(3,1);
plot(x, y, '.', 'MarkerSize', 10)
if k == 1
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
box off
hold all
end
drawnow
pause(.1)
end

7 Commenti

Sorry i realised some of my code was missing, I've corrected this above. Not sure if this changed anything? But thanks for the tips
Run the code above, see if it works, then replace x and y with your data. If you still have questions, please be specific about the problem you're facing and what you're trying to do. And whenever possible, upload code that we can copy, paste, and run without the need to save any extra functions. The rand function is good for generating example data.
I apologise for not putting in the code for the functions, I can include it all if it would be helpful but I didn't want to overload the question.
I have tried to replace the x,y values as you suggested but I'm struggling to implement this.
Basically, as you can see I am plotting the initial aggression against final aggression alternating how many short fuse individuals are present. For each value of percentage of short fuses in the population there is 1000 random values generated.
This is where I get confused with the code, so I have 1000 points per percentage of population initially aggressive from 0% to 100% up in 10%s
I simply cannot work out how to alter this code to be able to generate such values so many times for each percentage of short fuses.
I understand this may not be super clear. I am struggling to word this / code this in an accessible way. I am fairly new to matlab and this is pretty much the most complicated thing i will need to complete for my research.
Thanks for your help, its greatly appreciated
Hi John,
You're wise to consider overloading your question. There's a fine balance between giving too much information and not enough.
It seems that the heart of your question has changed. Are you having trouble plotting data, or are you having trouble calculating data?
Hi Chad,
So to try and clarify, the above code has two IVs which is the percentage of population aggressive and also the percentage of population short fuse.
Population of starting aggression is along the x axis and the y axis shows the end population aggressive (what I am looking at / DV)
At the moment i have to do separate plots to alter the starting population short fuse, So I am trying to add something to the code so i can have multiple plots to show different short fuses on the same graph.
Thanks again for all your troubles, its greatly appreciated. John
The jargon is a bit confusing. Can you strip out all lingo specific to your field and just talk in terms of x and y?
How many dependent variables do you have? Perhaps two, y1 and y2? Do they share an independent variable x or do they each have their own x1 and y1?
As I understand it, your dependent variables are changing through time, and that's what you want to animate? Does this do what you want?
% independent variable:
x = 0:.1:1;
% starting values of y1 and y2:
y1 = .2*rand(size(x));
y2 = .2*rand(size(x));
figure(3); clf
for k = 1:30
% new values of y1 and y2:
y1 = y1+.05*rand(size(x));
y2 = y2+.02*rand(size(x));
% plot y1:
h1 = plot(x, y1, 'r.', 'MarkerSize', 12);
% Set axis properties the first time:
if k == 1
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
box off
hold all
end
% plot y2:
h2 = plot(x, y2, 'b.', 'MarkerSize', 10);
% add a legend:
if k==1
legend('y1','y2')
end
drawnow
pause(.1)
% delete the data we just plotted:
delete([h1 h2])
end
Hi Chad, Firstly thank you for continued help. I have taken what you said and tried to re word my question above with the code for the added function and additional comments on the code itself. Hopefully this has clarified my question.
Thanks, John

Accedi per commentare.

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Richiesto:

il 7 Mag 2015

Commentato:

il 13 Mag 2015

Community Treasure Hunt

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

Start Hunting!

Translated by