Clear Filters
Clear Filters

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

4 views (last 30 days)
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 Comments
Matt R
Matt R on 19 Oct 2022
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.

Sign in to comment.

Answers (1)

VBBV
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'
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 Comments
Matt R
Matt R on 19 Oct 2022
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 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

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!