Several plotsomposes in one figure (Plot) - matlab

I've created network for function prediction. There are several neurons, each represents own function piece.
Is there a way to display each piece in one figure(in subplots)?
I know, that plotsompos do it for one neuron, but it makes big graphic,
so I can't do this every time for each of 10-20 neurons.
Can I build graphics on
net.weights{}
by myself for example?

If you want several plots in single plot you can try Subplot function
For example here 4 subplots are plotted in single window with x against y1,y2,y3,y4:
x = linspace(-5,5);
y1 = sin(x);
subplot(2,2,1)
plot(x,y1)
title('First subplot')
y2 = sin(2*x);
subplot(2,2,2)
plot(x,y2)
title('Second subplot')
y3 = sin(4*x);
subplot(2,2,3)
plot(x,y3)
title('Third subplot')
y4 = sin(6*x);
subplot(2,2,4)
plot(x,y4)
title('Fourth subplot')

Related

Matlab show two fig files in one plot

I am trying to show two fig. files in one plot but it seems harder then expected. How can this be achieved?
Lets say I have two figs.
fig1 = openfig("someFig1.fig");
fig2 = openfig("someFig2.fig");
If you want to display them in the same plot, you can use copyobj:
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
savefig('test.fig')
y2 = cos(x);
plot(x, y2)
savefig('test2.fig')
fig1 = openfig("test.fig");
fig2 = openfig("test2.fig");
figure
h=subplot(1,1,1); %define subplot of 1x1
%copyobj of each figure on h subplot
copyobj(allchild(get(fig1,'CurrentAxes')),h)
copyobj(allchild(get(fig2,'CurrentAxes')),h)
Output:

Correctly aligning labels for subgroups within a tiledlayout

I want to put 5 plots in a figure using tiledlayout, 2x2 plots at the top then a plot at the bottom that spans two columns. There should be two sets of axis labels: one for the 2x2 plot and another for the bottom plot. My MWE is
x = linspace(-2,2,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(x).*cos(2*x);
y4 = sin(2*x).*cos(x);
y5 = y3 + y4;
figure('PaperUnits', 'inches', 'PaperSize', [4 6], 'PaperPosition', [0 0 4 6]);
t = tiledlayout(3,2,'TileSpacing','compact','Padding','compact');
nexttile; plot(x,y1); title('y_1');
nexttile; plot(x,y2); title('y_2');
nexttile; plot(x,y3); title('y_3');
nexttile; plot(x,y4); title('y_4');
xlabel(t,'time t_\alpha')
ylabel(t,'amplitude \alpha')
nexttile([1 2]); plot(x,y5); title('y_5');
xlabel('time t_\beta')
ylabel('amplitude \beta')
saveas(gcf,'myfig.jpg')
This gives me the following figure:
I want the amplitude \alpha ylabel and the time t_alpha xlabel to be aligned correctly for the 2x2 plots (as indicated by my red annotations). Is there a way to do this?
I'm using MATLAB R2020a. Note that the tiledlayout function was introduced in R2019b.
An approximation of what you wanted can be achieved by the approach I mentioned in my comment (using a "nested" tiledlayout). The steps necessary to make it work are:
Creating an external vertical layout.
Reserving the first two rows for the nested tiledlayout, by asking a 2-by-1 nexttile, making the resulting axes hidden, and creating a uipanel in its place. Calling uipanel is necessary because the parent of a tiledlayout cannot be another tiledlayout, but it can be a Panel.
Plotting everything in the appropriate panels, and applying the labels accordingly.
x = linspace(-2,2,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(x).*cos(2*x);
y4 = sin(2*x).*cos(x);
y5 = y3 + y4;
hF = figure('PaperUnits', 'inches', 'PaperSize', [4 6], 'PaperPosition', [0 0 4 6]);
tOut = tiledlayout(hF,3,1);
hAx = nexttile(tOut, [2,1]); hAx.Visible = 'off';
hP = uipanel(hF, 'Position', hAx.OuterPosition, 'BorderWidth', 0);
tIn = tiledlayout(hP,2,2,'TileSpacing','compact','Padding','compact');
nexttile(tIn); plot(x,y1); title('y_1');
nexttile(tIn); plot(x,y2); title('y_2');
nexttile(tIn); plot(x,y3); title('y_3');
nexttile(tIn); plot(x,y4); title('y_4');
xlabel(tIn,'time t_\alpha')
ylabel(tIn,'amplitude \alpha')
hAx = nexttile(tOut); plot(x,y5); title('y_5');
xlabel(hAx, 'time t_\beta')
ylabel(hAx, 'amplitude \beta')
Resulting in:
From here you can try tweaking the individual GUI elements (layouts, axes, panel) to get a result closer to what you wanted.
A completely different route I can suggest is starting with your code and adding the labels for the top 4 plots using the annotation function (instead of xlabel and ylabel).

Matlab: Plots and Subplots - How do I make Matlab zoom into all the subplots simultaneously?

I have multiple sub-plots. When I zoom into the first subplot to view a certain data set Matlab does not zoom the rest of the subplots. How do I make Matlab zoom into all subplots simultaneously?
subplot(2,1,1)
hold on
plot(Tim1.in,IA1.raw32SPC,'b-')
hold off
subplot(2,1,2)
hold on
plot(Tim1.in,IA1.cos,'r-*')
hold off
Since the x variable is the same, It makes sense to just link the x axes in this case. I added some code to create the x and y variables so that anyone can run the code below. You were also using hold unnecessarily.
x = 1:10;
y1 = 3*x;
y2 = x.^2;
ax(1) = subplot(2,1,1);
plot(x, y1, 'b-')
ax(2) = subplot(2,1,2);
plot(x, y2, 'r-*')
linkaxes(ax, 'x')

Title over group of subplots

there are many subplots and each subplot has its own title. how can add a title over all of these group of subplots?
I want this title to be shown at top center.
x = linspace(-5,5);
y1 = sin(x);
subplot(2,5,[1:2])
plot(x,y1)
title('y=sin(x)')
y2 = cos(x);
subplot(2,5,[3:4])
plot(x,y2)
title('y=cos(x)')
y3 = tan(x);
subplot(2,5,[5,10])
plot(x,y3)
title('y=tan(x)')
y4 = sin(2*x);
subplot(2,5,[6:7])
plot(x,y1)
title('y=sin(2x)')
y5 = cos(2*x);
subplot(2,5,[8:9])
plot(x,y2)
title('y=acos(2x)')
Since Matlab 2018b, the new function sgtitle adds a title to a subplot group, simply add sgtitle('Subplot Title');. It doesn't need a toolbox.
For example:
subplot(1,2,1)
plot(cos(0:40));
title('cos');
subplot(1,2,2)
plot(sin(0:40))
title('sin');
sgtitle('Trigo');
The simplest way I have found for people without the bioinformatics toolbox is this:
a = axes;
t = title('My title');
a.Visible = 'off';
t.Visible = 'on';
What you're doing is creating a new set of axes which, by default, covers the whole figure, and creating a title on those axes. Then the axes are made invisible, and this is overridden for the title which is made visible again.
If the resulting title collides with things, fiddle with a.Position to move the axes around.
Yes, it's ridiculous that this isn't part of base functionality, but there are plenty of one- or two-line functions hidden in toolboxes that one might say that about ;-) (looking at you, range.)
x = linspace(-5,5);
y1 = sin(x);
subplot(2,5,[1:2])
plot(x,y1)
title('y=sin(x)')
y2 = cos(x);
subplot(2,5,[3:4])
plot(x,y2)
title('y=cos(x)')
y3 = tan(x);
subplot(2,5,[5,10])
plot(x,y3)
title('y=tan(x)')
y4 = sin(2*x);
subplot(2,5,[6:7])
plot(x,y1)
title('y=sin(2x)')
y5 = cos(2*x);
subplot(2,5,[8:9])
plot(x,y2)
title('y=acos(2x)')
suptitle('my title');

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code