I want to plot same X axis and different Y axis like timing diagram
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2024-7-22
You almost want stackedplot ... except that is restricted to line plots, and does not appear to handle stairs plots.
回答(3 个)
Voss
2024-7-22
编辑:Voss
2024-7-22
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')
William Rose
2024-7-22
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!