필터 지우기
필터 지우기

Plot two sets of data in one plot (different Time vs Temp data sets)

조회 수: 4 (최근 30일)
Matt R
Matt R 2022년 10월 19일
댓글: VBBV 2022년 10월 19일
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!
  댓글 수: 2
Matt R
Matt R 2022년 10월 19일
Hi Jiri,
Thanks for the comment. However, I am unable to use yyaxis as my x-axis are two different data sets of time that I want to overlay.

댓글을 달려면 로그인하십시오.

답변 (1개)

VBBV
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'
ax1 =
Axes with properties: XLim: [1 10] YLim: [0 0.9000] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
% ax2 = axes('position',pos,'color','none');
h2 = plot(x2, A4, 'k','parent',ax2, 'LineWidth', 1);
ax2 = gca;
set(ax2,'visible','off');
legend;
  댓글 수: 3
Matt R
Matt R 2022년 10월 19일
Hi VBBV,
Thanks for the detail, however, your result is the same as my first result in the question (one x-y axis and tow lines). In other words, I want the second axis to be visible.
VBBV
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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by