Rita
Rita

Reputation: 23

How to use plotregression in a subplot in MATLAB?

I'm plotting a linear regression using the MATLAB function plotregression in this way:

plotregression(x, y)

Now, I want to plot six different regressions and plot them in subplots, as shown below:

subplot(2,3,1)
plotregression(x1,y1)
subplot(2,3,2)
plotregression(x2,y2)
...

However, this doesn't work: the subplot command is ignored and only the last regression plot is shown. How can I display multiple regression plots in the same window?

I would also like to set the xlabel and the ylabel manually for each regression plot. Is that possible?

Upvotes: 0

Views: 1799

Answers (1)

hbaderts
hbaderts

Reputation: 14326

The plotregression function creates a new figure, so it overwrites your subplot configuration. By calling plotregression multiple times, the current plot is overwritten, and so only the last regression is shown. The documentation on plotregression tells you how to create multiple plots:

plotregression(targs1,outs1,'name1',targs2,outs2,'name2',...) generates multiple plots.

i.e. for two plots, this is

% Generate example data
x1 = 0:0.01:1;
y1 = x1 + 0.1*randn(size(x1));
x2 = -1:0.05:1;
y2 = x2 + 0.5*randn(size(x2));

% Plot regression
plotregression(x1,y1,'My first variable', x2,y2,'Another regression')

resulting regression plot

Internally, plotregression uses subplots, so you can change the xlabels, ylabels, titles and so on for each subplot individually by making it active with the subplot function, and calling the relevant commands:

subplot(1,2,1);
xlabel('This is x_1');
subplot(1,2,2);
xlabel('And this is x_2');
ylabel('\ldots and y_2');

Upvotes: 1

Related Questions