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

2 visualizzazioni (ultimi 30 giorni)
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]];
%%%%nested function to simplify coding below
function k = ind(i1, j1, i, j)
k = sub2ind([m n], I(i1, i), J(j1, j));
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)];
nbrs{k} = nbr(~isnan(nbr)); % remove any missing (boundary) pixels
if prnt == 1
for k = 1:length(nbrs)
fprintf('%5d :', k); fprintf('%5d ', nbrs{k}); fprintf('\n')
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;
[m n] = size(pop1);
if plt
plot_world(pop1, sf);
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;
pop2(k) = p;
if na > np
pop2(k) = a;
elseif na < np
pop2(k) = p;
end % else stays unchanged
pop1 = pop2;
if plt
plot_world(pop1, sf);
title(sprintf('Day %d', i+1));
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
plot(pagg, xagg, '.', 'MarkerSize', 10)
xlabel('Initial Population Aggressive');
ylabel('Final Population Aggressive');
title('Parametric Study for Neighbourhood Connectivity')
axis([0 1 0 1]);
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
John Doe
John Doe il 13 Mag 2015
Thank you Chad, for both your help and patience with this,
really appreciate it!

Accedi per commentare.

Risposte (2)

Ilham Hardy
Ilham Hardy il 7 Mag 2015
Modificato: Ilham Hardy il 7 Mag 2015
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
John Doe
John Doe il 7 Mag 2015
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.

Chad Greene
Chad Greene il 7 Mag 2015
Modificato: Chad Greene il 7 Mag 2015
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
  7 Commenti
John Doe
John Doe il 8 Mag 2015
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.


Scopri di più su Discrete Data Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by