plot 2 different time data on the same axis in same graph
Show older comments
I have 2 dataset to plot both wrt to time axis in the same plot but time data is different for both the data. on yaxis 1 I want DATA1(line) on yaxis 2 i want DATA2(buuble markers) how can i do so. I am attaching the data file
2 Comments
Walter Roberson
on 29 Jan 2021
See yyaxis left and yyaxis right
Abhijit Sardar
on 29 Jan 2021
Accepted Answer
More Answers (2)
J. Alex Lee
on 29 Jan 2021
0 votes
or potentially you are just looking for "hold on"
1 Comment
Abhijit Sardar
on 29 Jan 2021
T = readtable('TIMEDATA.txt');
yyaxis right
plot(T.TIME2, T.DATA2, 'Displayname', 'DATA2')
yyaxis left
plot(T.TIME1, T.DATA1, 'Displayname', 'DATA1')
legend show
1 Comment
You might notice that is not very useful. The reason is that you used a single x axes even though the x values are fairly different.
T = readtable('TIMEDATA.txt');
fig = gcf;
ax1 = axes(fig);
h(1) = line(ax1, T.TIME1, T.DATA1, 'Displayname', 'DATA1', 'color', 'k');
legend(h(1), 'Location', 'northwest')
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
h(2) = line(T.TIME2, T.DATA2, 'parent', ax2, 'Displayname', 'DATA2', 'color', 'b');
legend(h(2), 'Location', 'northeast')
Unfortunately in order to get the axes in the right location, it becomes a challenge to get a merged legend.
Categories
Find more on Graphics Object Properties 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!

