Plot two sets of data in one plot (different Time vs Temp data sets)
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
I have two sets of data (Time vs Temperature) and I would like to overlay the two plots to compare the behaviour.
I am having difficulty getting the right code. Either, I can plot both lines but only one set of x-y axis or I can plot both sets of x-y axis but only one line (it loses the first set of data).
As a new Matlab user, this feels like a basic task but I am struggling to find any clear answer on it. Can anyone help?
This gives me one x-y axis (ax1) but both lines on the plot.
figure('WindowState','maximized');
plot(x1, A1, 'LineWidth', 1);
set(gcf, 'Position', [100 100 1000 550]);
ax1 = gca;
pos = get(ax1, 'Position');
set(ax1, 'Position', pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right');
hold(ax1,"on");
ax2 = axes('position',pos,'color','none');
plot(x2, A4, 'k','parent',ax2, 'LineWidth', 1);
set(ax2,'visible','off');
legend;
This gives me two x-y axis but only one line (plot two). I am aware that this is tied to the Visibility of ax2 but I am not sure how to proceed.
figure('WindowState','maximized');
plot(x1, A1, 'LineWidth', 1);
set(gcf, 'Position', [100 100 1000 550]);
ax1 = gca;
pos = get(ax1, 'Position');
set(ax1, 'Position', pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right');
hold(ax1,"on");
ax2 = axes('position',pos,'color','none');
plot(x2, A4, 'k','parent',ax2, 'LineWidth', 1);;
legend;
Thank you in advance!
답변 (1개)
VBBV
2022년 10월 19일
% figure('WindowState','maximized');
x1 = 1:10; A1 = rand(10,1);
x2 = 1:10; A4 = rand(10,1);
plot(x1, A1, 'LineWidth', 1);
set(gcf, 'Position', [100 100 1000 550]);
ax1 = gca;
pos = get(ax1, 'Position');
set(ax1, 'Position', pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right');
hold(ax1,"on");
ax2 = axes('position',pos,'color','none');
plot(x2, A4, 'k','parent',ax2, 'LineWidth', 1);
set(ax2,'visible','off');
legend;
% F = figure('WindowState','maximized')
h1 = plot(x1, A1, 'LineWidth', 1);
set(gcf, 'Position', [100 100 1000 550]);
ax1 = gca;
pos = get(ax1, 'Position');
set(ax1, 'Position', pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right');
hold(ax1,"on");
ax1.NextPlot = 'add'
% ax2 = axes('position',pos,'color','none');
h2 = plot(x2, A4, 'k','parent',ax2, 'LineWidth', 1);
ax2 = gca;
set(ax2,'visible','off');
legend;
댓글 수: 3
VBBV
2022년 10월 19일
set(ax1,'visible','off');
Then you can set the first axes ax1 to off. instead of
set(ax2,'visible','off');
If you want both axes, the resulting figure will become blurred with overlapped axes
참고 항목
카테고리
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!