Title over group of subplots - matlab

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');

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')

Several plotsomposes in one figure (Plot)

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')

Adding a second x-axis to my graph, which I can scale myself

I am having a problem with Matlab at the moment and I hope you can help me. I have written this code below to display a graph, but now I want to add another x-axis above the image, which I can scale myself. Is there any possibility to add another line like: set(gca, 'XTick2', [bla:bla:bla]); and another label?
EDIT: I have solved a part of the problem, hope you can help me with the rest, too... I have now 2x-axes, but still a few problems.
The labels are in wrong positions, the y-label is inside the scale and I want two different labels for the two x-axes.
Also I would like to remove the scale of the lower x-axis from the upper one.
Code is also the new one:
x1 = [0, 421.441, 842.882, 1264.323, 1685.764, 2107.205, 2528.646, 2950.087, 3371.528, 3792.969, 4214.41, 4635.851, 5057.29];
y1 = [55.659, 55.856, 56.081, 56.279, 56.312, 56.169, 56.038, 55.903, 55.75, 55.604, 55.512, 55.534, 55.661];
y2 = [51.231, 51.735, 52.063, 52.152, 51.632, 51.16, 51.014, 50.911, 50.721, 50.596, 50.597, 50.858, 51.242];
y3 = [50.939, 51.381, 51.644, 51.687, 51.353, 50.944, 50.829, 50.706, 50.538, 50.43, 50.412, 50.614, 50.948];
y4 = [50.023, 50.328, 50.506, 50.535, 50.352, 50.113, 50.032, 49.938, 49.801, 49.705, 49.672, 49.801, 50.03];
plot(x1,y1, 'ks-',x1,y2, 'bx--',x1,y3, 'gd-.',x1,y4, 'c.-'),
ax1 = gca;
ax1.XLim = [0, 5075];
ax1.XTick = 0:1000:5075;
ay1 = gca;
ay1.YLim = [49.5, 56.5];
ay1.YTick = 49.5:0.5:56.5;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.XLim = [0, 360];
ax2.XTick = 0:30:360;
ax2.XAxisLocation = 'top';
ax2.YTick = [];
grid off
xlabel('Time [s]')
ylabel('Temperature [°C]')
legend('A','B','C','D')
Just use the function text,
text(x,y,'string')
where x and y are the coordinates of you new x-axis and string your blabla.
UPDATE: use this sample code and adjust it to your needs
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
which gives the plot,