# Showing the distribution of graphs as a bar graph attached to the plot

2 Ansichten (letzte 30 Tage)
kyuwon Oh am 29 Nov. 2021
Beantwortet: Malay Agarwal am 22 Feb. 2024
Hello, I wanted to reproduce the following figure.
So I ran the following code using stdshade.m package from here.
clear all
%set parameter values
am=300;
bm=1;
ar=1;
br=1;
kf=10;
kb=100;
kd=10;
%set number of reactions
Tend=3000;
%set initial conditions for # molecules and for time t
X=zeros(Tend,3);
Y=zeros(Tend,2);
t1=zeros(Tend,1);
t2=zeros(Tend,1);
X(1,:)=[100 100 0]; Y(1,:)=[100 100]; t1(1)=0; t2(1)=0;
j=1;
k=1;
km=2000;
M1=zeros(km,600);
M2=zeros(km,600);
while k<=km
while t1(j)<6
%calculate propensities
p1=am*X(j,3);
p2=bm*X(j,1);
p3=ar*X(j,1);
p4=br*X(j,2);
p5=kf*X(j,2)*X(j,3);
p6=kb*(1-X(j,3));
psum1=p1+p2+p3+p4+p5+p6;
%select next reaction
mu=rand(1);
z1=0;z2=0;z3=0;z4=0;z5=0;z6=0;
if 0 <= mu && mu < p1/psum1
z1=1;
else if p1/psum1 <= mu && mu < (p1+p2)/psum1
z2=1;
else if (p1+p2)/psum1 <= mu && mu < (p1+p2+p3)/psum1
z3=1;
else if (p1+p2+p3)/psum1 <= mu && mu < (p1+p2+p3+p4)/psum1
z4=1;
else if (p1+p2+p3+p4)/psum1 <= mu && mu < (p1+p2+p3+p4+p5)/psum1
z5=0.01;
else
z6=0.01;
end
end
end
end
end
%update state
X(j+1,1) = X(j,1) + z1 - z2;
X(j+1,2) = X(j,2) + z3 - z4 - z5 - z6;
X(j+1,3) = X(j,3) - z5 + z6;
%update time
t1(j+1) = t1(j) + log(1/rand(1))/psum1;
%update counter
j=j+1;
end
M1(k,:)=interp1(t1(1:j),X(1:j,2),0.01:0.01:6);
X=zeros(Tend,3);
X(1,:)=[100 100 0];
t1=zeros(Tend,1);
k=k+1;
j=1;
end
k=1;
while k<=km
while t2(j)<6
%calculate propensities
p1=am*kd/(Y(j,2)+kd);
p2=bm*Y(j,1);
p3=ar*Y(j,1);
p4=br*Y(j,2);
psum2=p1+p2+p3+p4;
%select next reaction
mu=rand(1);
z1=0;z2=0;z3=0;z4=0;
if 0 <= mu && mu < p1/psum2
z1=1;
else if p1/psum2 <= mu && mu < (p1+p2)/psum2
z2=1;
else if (p1+p2)/psum2 <= mu && mu < (p1+p2+p3)/psum2
z3=1;
else
z4=1;
end
end
end
%update state
Y(j+1,1) = Y(j,1) + z1 - z2;
Y(j+1,2) = Y(j,2) + z3 - z4;
%update time
t2(j+1) = t2(j) + log(1/rand(1))/psum2;
%update counter
j=j+1;
end
M2(k,:)=interp1(t2(1:j),Y(1:j,2),0.01:0.01:6);
Y=zeros(Tend,2);
Y(1,:)=[100 100];
t2=zeros(Tend,1);
k=k+1;
j=1;
end
xlim([0 6])
ylim([0 120])
hold on
stairs(0.01:0.01:6, M1(1,1:600), 'r', 'linewidth', 2)
stairs(0.01:0.01:6, M2(1,1:600), 'b', 'linewidth', 2)
xlim([0 6])
ylim([0 120])
hold off
ylabel('Concentration of R')
xlabel('time')
title('R')
Now I got very similar figure, but without 'a horizontal bar graphs which shows the distribution of the plotted graphs'.
My question is: Is there any nice code or package to show a horizontal bar graphs which shows the distribution of the plotted graphs next to this figure, exactly like the first figure?
It is okay to just look 'similar', I mean, it does not need to be a bar graph but can be any type of graphs if it gives the desired informations to us clearly.
Thanks.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Malay Agarwal am 22 Feb. 2024
Hi Kyuwon,
I understand that you want to display a bar graph along the right boundary of another plot.
• It uses the “subplot” function and manually changes the position of the bar graph to align it with the right boundary of the line graph.
• Note that since I am not sure about the data that should appear in the bar graph, I am using dummy data. Please change the data accordingly.
% Use subplot to create a subplot
ax_line = subplot(1, 2, 1);
% Your code to plot the line graph
xlim([0 6])
ylim([0 120])
hold on
stairs(0.01:0.01:6, M1(1,1:600), 'r', 'linewidth', 2)
stairs(0.01:0.01:6, M2(1,1:600), 'b', 'linewidth', 2)
xlim([0 6])
ylim([0 120])
hold off
ylabel('Concentration of R')
xlabel('time')
title('R')
% Turn off the box to remove ticks
% On the right boundary of the line plot
set(ax_line, 'box', 'off');
% Adjust the position of the line graph to remove space for y-axis ticks
lineGraphPos = get(ax_line, 'Position');
lineGraphPos(1) = 0.05; % Adjust the left position as needed
lineGraphPos(3) = lineGraphPos(3) + 0.05; % Increase the width to fill the space
set(ax_line, 'Position', lineGraphPos);
% Plot the bar graph
ax_bar = subplot(1, 2, 2);
% TODO: Change data here
values = randi([1 10], [1 10]);
barh(ax_bar, values, 'r', 'FaceColor', 'flat');
% Adjust the position of the bar graph to attach it to the line graph
barGraphPos = get(ax_bar, 'Position');
barGraphPos(1) = lineGraphPos(1) + lineGraphPos(3); % Adjust the left position
barGraphPos(3) = 0.05; % Adjust the width of the bar graph as needed
set(ax_bar, 'Position', barGraphPos);
% Remove the boundaries (spines), ticks, and labels of the bar graph
set(ax_bar, 'box', 'off', 'Color', 'none');
axis(ax_bar, 'off');
This generates the following plot:
The code can be extended to display two bar graphs as in the image attached in the question by using “hold on” after the first “barh” call, calling “barh” again with the data for the second bar graph and then using “hold off”. For example:
ax_bar = subplot(1, 2, 2);
% TODO: Change data here
values1 = randi([1, 10], [1 10]);
values2 = randi([1 10], [1 10]);
barh(ax_bar, values1, 'r', 'FaceColor', 'flat');
hold on
barh(values2, 'b', 'FaceColor', 'flat');
hold off
This results in the following graph:
Please refer to the following resources for more details:
Hope this helps!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Discrete Data Plots finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by