I want to plot same X axis and different Y axis like timing diagram
5 views (last 30 days)
Show older comments
Hi,
I want to plot same X axis and different Y axis like timing diagram.X axis will appear as a single axis on the graph and will determine the x-value of all graph.All graphs will have different Y axis.I added a picture for example.Can i help you?
Thanks in advance.
1 Comment
Walter Roberson
on 22 Jul 2024
You almost want stackedplot ... except that is restricted to line plots, and does not appear to handle stairs plots.
Answers (3)
Voss
on 22 Jul 2024
Edited: Voss
on 22 Jul 2024
I don't know what form your variables are in, but here's something:
t = 0:0.5:4.5;
Vc = [0 1 0 1 0 1 0 1 0];
Vb = [0 0 1 1 0 0 1 1 0];
Va = [0 0 0 0 1 1 1 1 0];
t_plot_V = t([1 repelem(2:end-1,1,2) end]);
Vc_plot = repelem(Vc,1,2);
Vb_plot = repelem(Vb,1,2);
Va_plot = repelem(Va,1,2);
t_plot_Vo = t([1 repelem(2:end-1,1,3) end]);
VoMemR_plot = [0 0 0 0.5 0.5 0 0.5 0.5 0.5 0.6 0.6 0 0.5 0.5 0.5 0.6 0.6 0.5 0.6 0.6 1 1 1 1 0 0];
VoMemC_plot = [0 0 0.5 0 0 0.6 0 0 0.1 0 0 1 0 0 0.6 0 0 1 0 0 0 1 1 0 0 0];
offset = 1.5*(0:4);
figure
hold on
box on
plot(t_plot_Vo,VoMemC_plot+offset(1),'--','Color',[0 0.6 0],'LineWidth',2)
plot(t_plot_Vo,VoMemR_plot+offset(2),'b','LineWidth',2)
plot(t_plot_V,Va_plot+offset(3),'r','LineWidth',2)
plot(t_plot_V,Vb_plot+offset(4),'Color',[0 0.8 0.8],'LineWidth',2)
plot(t_plot_V,Vc_plot+offset(5),'Color',[0.8 0 0.8],'LineWidth',2)
yticks(offset)
yticklabels({'Vo (MemC)','Vo (MemR)','Va','Vb','Vc'})
ylim(offset([1 end])+0.5*[-1 1]+[0 1])
xlim(t_plot_V([1 end]))
xlabel('Time (ms)')
xline(t,':','Color',[0.5 0.5 0.5])
x_text = repmat((t(1:end-1)+t(2:end))/2,3,1).';
y_text = offset(end-2:end)+0.5*ones(numel(t)-1,1);
str_text = reshape(compose('%d',[Va Vb Vc]),[],3);
text(x_text(:),y_text(:),str_text(:),'HorizontalAlignment','center')
2 Comments
William Rose
on 22 Jul 2024
Make some simulated data:
t=0:0.5:4.5; % time (ms)
N=length(t);
Va=randi([0,1],1,N);
Vb=randi([0,1],1,N);
Vc=randi([0,1],1,N);
VoR=rand(1,N);
VoC=rand(1,N);
Plot the data
figure
stairs(t,VoC,'--g',Linewidth=1.5); hold on
stairs(t,VoR+1.5,'-b',Linewidth=1.5);
stairs(t,Va+3,'-r',Linewidth=1.5);
stairs(t,Vb+4.5,'-c',Linewidth=1.5);
stairs(t,Vc+6,'-m',Linewidth=1.5);
yticks([.5:1.5:6.5]);
ylim([-.5,7.5])
yticklabels({'V0_C','V0_R','Va','Vb','Vc'});
xlim([0,4.5]);
xticks(t)
ax=gca; ax.XGrid='on'; % vertical grid lines
OK
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!