Is there any way to use plotregression in a subplot?

조회 수: 11 (최근 30일)
Rita
Rita 2016년 10월 4일
댓글: Rita 2016년 10월 19일
I'm plotting a linear regression using the MATLAB function plotregression
plotregression(x, y)
I would like to have 6 figures in a subplot
subplot(2,3,1) plotregression(x1,y1) subplot(2,3,2) plotregression(x2,y2) and so on
...but it didn't work! Any idea much would be appreciated in advance?
  댓글 수: 2
Massimo Zanetti
Massimo Zanetti 2016년 10월 4일
What kind of error do you meet?
Rita
Rita 2016년 10월 4일
It doesnot show all of 6 figures in a subplot .It means that the function plotregression use "figure" for each plot so I can't all of 6 plots together in a subplot.

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

채택된 답변

Dimitris Iliou
Dimitris Iliou 2016년 10월 11일
If I understand correctly, you are using the ‘plotregression’ command 6 times and you want to put the results of each call into a subplot.
The issue you encounter is because ‘plotregression’ creates a new figure every time it is called. This means that you cannot directly put the results of ‘plotregression’ into a subplot.
One way to accomplish this, is to “move” the axes from the ‘plotregression’ figure to the subplot of the figure you want. To implement this, you would need to change the ‘Parent’ of the objects that are included within the ‘plotregression’ figure axes. In this case, you would need to change the Parent of the following objects:
  • The two line objects within the ‘plotregression’ figure axes
  • The x and y labels from the ‘plotregression’ figure axes
  • The x and y limits from the ‘plotregression’ figure axes
  • The legend from the ‘plotregression’ figure
You can perform this task for every one of the six subfigures, i.e. ‘plotregression’ figures that you have. Note that you would need to open a new figure every time you call ‘plotregression’, so that you do not lose the figure that contains the subplots. I am attaching a simple example with a figure that contains 2 subplots:
close all
clear
[x,t] = simplefit_dataset;
net = feedforwardnet(10);
net = train(net,x,t);
y = net(x);
h = figure();
plotregression(t,y,'Regression');
h2 = figure();
subplot(1,2,1)
ax = gca;
h.Children(3).Children(1).Parent = h2.Children(1);
h.Children(3).Children(1).Parent = h2.Children(1);
h.Children(3).Children(1).Parent = h2.Children(1);
legend( h.Children(2).String,'Location', h.Children(2).Location)
title(h.Children(3).Title.String);
xlabel(h.Children(3).XLabel.String);
ylabel(h.Children(3).YLabel.String);
ax.XLim = h.Children(3).XLim;
ax.YLim = h.Children(3).YLim;
close(h)
h = figure();
plotregression(t,y,'Regression');
figure(h2)
subplot(1,2,2)
ax = gca;
h.Children(3).Children(1).Parent = h2.Children(1);
h.Children(3).Children(1).Parent = h2.Children(1);
h.Children(3).Children(1).Parent = h2.Children(1);
legend( h.Children(2).String,'Location', h.Children(2).Location)
title(h.Children(3).Title.String);
xlabel(h.Children(3).XLabel.String);
ylabel(h.Children(3).YLabel.String);
ax.XLim = h.Children(3).XLim;
ax.YLim = h.Children(3).YLim;
close(h)
Please see the following comments about the attached code:
  • Both the ‘Location’ and ‘String’ are set for the legend ( the location can be changed to your preference)
  • For each subplot the x and y label are defined
  • For each subplot the x and y limits are defined
  • When you change the Parent of an object it will no longer appear as the Child of the original figure.
  • The ‘plotregression’ figures are closed after all the necessary objects are “moved” to the subplots.
The above example can be extended to as many subplots as you need.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by