Plot two sets of data in one plot (different Time vs Temp data sets)
4 views (last 30 days)
Show older comments
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!
Answers (1)
VBBV
on 19 Oct 2022
% 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 Comments
VBBV
on 19 Oct 2022
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
See Also
Categories
Find more on Legend 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!