Is there any way to use plotregression in a subplot?
12 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dimitris Iliou
on 11 Oct 2016
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.
More Answers (0)
See Also
Categories
Find more on Subplots 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!