필터 지우기
필터 지우기

plot 2 different time data on the same axis in same graph

조회 수: 3 (최근 30일)
Abhijit Sardar
Abhijit Sardar 2021년 1월 29일
댓글: Abhijit Sardar 2021년 1월 30일
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
Walter Roberson
Walter Roberson 2021년 1월 29일
See yyaxis left and yyaxis right
Abhijit Sardar
Abhijit Sardar 2021년 1월 29일
i am using the same but giving error. can please check the data

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

채택된 답변

J. Alex Lee
J. Alex Lee 2021년 1월 30일
편집: J. Alex Lee 2021년 1월 30일
To overcome the merged legend issue, you can create an empty plot to make the legend entry
T = readtable('TIMEDATA.txt');
fig = gcf;
ax(1) = axes(fig,'NextPlot','add');
ax(2) = axes(fig,'NextPlot','add',...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
Colors = lines(2);
for i = 1:2
xlabel(ax(i),"Time"+i)
ylabel(ax(i),"Data"+i)
ax(i).XColor = Colors(i,:);
ax(i).YColor = Colors(i,:);
end
h(1) = plot(ax(1), T.TIME1, T.DATA1, 'Displayname', 'DATA1','Color',Colors(1,:));
h(2) = plot(ax(1), nan, nan, 'Displayname', 'DATA2','Color',Colors(2,:));
% either manually set the other plot in the other axes to be the same
% h2copy = plot(ax(2),T.TIME2, T.DATA2,'Displayname','DATA2','Color',Colors(2,:));
% or do it this way if you want to keep things synced without needing to track your changes
h2copy = copyobj(h(2),ax(2));
h2copy.XData = T.TIME2;
h2copy.YData = T.DATA2;
legend(ax(1),h, 'Location', 'northwest')
% compute axes positions in a kind of naive way if you want to label the axes
pos = reshape([ax.Position],4,2)'
AxPos = [max(pos(:,1),[],1),max(pos(:,2),[],1),min(pos(:,3:4),[],1)]
set(ax,'Position',AxPos)

추가 답변 (2개)

J. Alex Lee
J. Alex Lee 2021년 1월 29일
or potentially you are just looking for "hold on"
  댓글 수: 1
Abhijit Sardar
Abhijit Sardar 2021년 1월 29일
no hold on does not work. i want 2 different time data to be represented on the same axis please see the attachment you will understand

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


Walter Roberson
Walter Roberson 2021년 1월 29일
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
Walter Roberson
Walter Roberson 2021년 1월 29일
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.

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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by