Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Matt R
Matt R le 19 Oct 2022
Commenté : VBBV le 19 Oct 2022
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 commentaires
Jiri Hajek
Jiri Hajek le 19 Oct 2022
Maybe this could help?
Matt R
Matt R le 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.

Connectez-vous pour commenter.

Réponses (1)

VBBV
VBBV le 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 commentaires
Matt R
Matt R le 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 le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by