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')
댓글 수: 2
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!