Subplot: How can i set the size of my diagram in my subplot? (Size definition of each placeholder)

6 views (last 30 days)
i want to create a subplot with 2 columns and 3 rows. i want to add some diagrams there. Each of them should have the same size. for example 5cm * 5cm. i know the subplot command (subplot(3,2,1)) but i dont know how to use it exactly and how i can define the size of this diagrams. for example the size 5cm*5cm. between this should be a little space for x/y lables so i can avoid overlapping. thank you for your help. sincerely philipp
My Code:
clear all %löscht alle Variablen
clear clc %löscht den Bildschirm
%RAINFLOWDARSTELLUNG%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Input_Matrix = textread('Input1.txt')%liest die Daten für die Rainflow Darstellung ein
[zeilen,spalten]=size(Input_Matrix)
x = Input_Matrix(:,1)
y = Input_Matrix(:,2)
z = Input_Matrix(:,3)
colorbar('location','Manual', 'position', [0.93 0.1 0.02 0.81]);
az = 0;
el = 90;
view(az, el);
hold on
%scatter3(x,y,z,'filled')%Problem i dont know to make it filled
view(0,90)% view from above !!!http://de.mathworks.com/help/matlab/ref/view.html
a = 30;%markersize
scatter3(x, y, z, a, z, 'filled');
hold on
view(0, 90)
%value = find(max(z)) ;
%hold on
%plot3(x(idx),y(idx),z(idx),'*r')
[m,ind]=max(z);
scatter3(x(ind), y(ind), z(ind),'*r');
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%***************** Wie man eine Linie zeichnet ***************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Hier die dicke braune "dummy Linie"
%axis([-350 350 0 250]); % definiert den Achsen Bereich
line([-350 -150],[20 20],[2000000 2000000],'Marker','.','LineStyle','-','LineWidth',9, 'Color',[.5 .4 .3])
%***************** Ende **************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%********************Cutofflinien zeichnen********************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%Initialisierungen für die Berechnungen:***********************************
sigma_wk = 50; % Default Wert angenommen
tau_wk = 70; % Default Wert angenommen
m_sigma = 0.07; % Steigung - Defaultwert angenommen
m_tau = 0.07; % Steigung - Defaultwert angenommen
m_sigma_zwei = m_sigma*0.33; % Steigung etwas flacher -> 1/3 von m_sigma -> vom Punkt 2 nach 3
m_tau_zwei = m_tau*0.33; % Steigung etwas flacher -> 1/3 von m_sigma -> vom Punkt 2 nach 3
multipikator=1.2;% Für das Zeichnen der Waagrechten Linien
%Normalspannungen:*********************************************************
%X-Koordianten der einzelnen Punkte
sigma_m_1=sigma_wk/(m_sigma-1);%Punkt1
sigma_m_2=sigma_wk/(1+m_sigma);%Punkt2
sigma_m_3=sigma_wk*((3+m_sigma)/(1+m_sigma_zwei)^2);%Punkt3
%Y-Werte der Punkte der einzelnen Punkte
sigma_a_1=sigma_wk/(1-m_sigma);%Punkt1
sigma_a_2=sigma_wk/(1+m_sigma);%Punkt2 Vorzeichen plus gemacht!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sigma_a_3=(sigma_wk/3)*((3+m_sigma)/(1+m_sigma)^2);%Punkt3
%Schubspannungen***********************************************************
%X-Koordianten der einzelnen Punkte
tau_m_1=tau_wk/(m_tau-1);
tau_m_2=tau_wk/(1+m_tau);
tau_m_3=tau_wk*((3+m_tau)/(1+m_tau_zwei)^2);%
%Y-Werte der Punkte der einzelnen Punkte
tau_a_1=tau_wk/(1-m_tau);
tau_a_2=tau_wk/(1+m_tau);
tau_a_3=(tau_wk/3)*((3+m_tau)/(1+m_tau)^2);
%Diagramm Beschriftungen***************************************************
%Farbauswahl http://www.avatar.se/molscript/doc/colour_names.html
title('\color[rgb]{0.698039 0.133333 0.133333}Normalspannungen - Schubspannungen');%Titel des Diagrammes.
ylabel('Beschriftung der y - Achse \color[rgb]{0 0.392157 0}\bf\sigma_{a}\tau_{a}');
xlabel('Beschriftung der x - Achse \color[rgb]{0 0.392157 0}\bf\sigma_{m}\tau_{m}');
grid on; %Gitter eingeblendet
%axis ([ -5 5 0 5]);%manuelle Festlegung der
%Grenzen!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
%axis tight ;%Mit "..." werden die Bereiche wieder so gesetzt, dass die Daten perfekt in den Plot passen.
%Griechische Buchstaben https://de.mathworks.com/help/matlab/creating_plots/greek-letters-and-special-characters-in-graph-text.html?searchHighlight=greek
%subplot (1 , 2 , 1);%1 Zeile, 2 Spalten, 1 Plot.6t
%http://matlab.izmiran.ru/help/techdoc/ref/linespec.html
% X-Werte, Y-Werte, Z-Werte %Hier werden 2 Punkte im 3D Raum miteinander verbunden.....
line([0 sigma_m_2],[sigma_wk sigma_a_2],[1 1],'Marker','.','LineStyle','-','LineWidth',1, 'Color',[1 0 0])% 0-2
line([sigma_m_2 sigma_m_3],[sigma_a_2 sigma_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',1, 'Color',[0 1 0])%2-3
line([0 sigma_m_1],[sigma_wk sigma_a_1],[1 1],'Marker','.','LineStyle','-','LineWidth',1, 'Color',[0 0 1])%0-1
%Waagrechte Linien:
line([sigma_m_3 sigma_m_3*multipikator],[sigma_a_3 sigma_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',3, 'Color',[1 0 0])%Rechts
line([sigma_m_1 sigma_m_1*multipikator],[sigma_a_1 sigma_a_1],[1 1],'Marker','.','LineStyle','-','LineWidth',3, 'Color',[1 0 0])%Links
%Schubspannungen***********************************************************
line([0 tau_m_2],[tau_wk tau_a_2],[1 1],'Marker','.','LineStyle','-','LineWidth',5, 'Color',[0 1 1])
line([tau_m_2 tau_m_3],[tau_a_2 tau_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',5, 'Color',[0 0 1])
line([0 -tau_m_2],[tau_wk tau_a_2],[1 1],'Marker','.','LineStyle','-','LineWidth',5, 'Color',[1 0 1])
line([-tau_m_2 -tau_m_3],[tau_a_2 tau_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',5, 'Color',[0 1 1])
line([tau_m_3 tau_m_3*multipikator],[tau_a_3 tau_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',3, 'Color',[1 0 0])% %Waagrechte Linien: Rechts
line([-tau_m_3 -tau_m_3*multipikator],[tau_a_3 tau_a_3],[1 1],'Marker','.','LineStyle','-','LineWidth',3, 'Color',[1 0 0])% %Waagrechte Linien: Links
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
%****************************************************************************************
datacursormode on

Accepted Answer

Nick Counts
Nick Counts on 4 Nov 2016
Grüße, Herr Mueller :)
It was not clear to me if you needed your layout to be as described when you print, save to an image, or as they appear in Matlab.
I have included an example that will display as you described in Matlab.
fig = figure;
fig.Units = 'centimeters';
fig.Position(3:4) = [13 19];
% Subplot is really a shortcut to axes generation and axes handles. If you
% have specific layout needs, it is sometimes easier to generate the axes
% yourself.
ax11 = axes('units', 'centimeters', 'position', [ 1, 13, 5, 5]);
ax12 = axes('units', 'centimeters', 'position', [ 7, 13, 5, 5]);
ax21 = axes('units', 'centimeters', 'position', [ 1, 7, 5, 5]);
ax22 = axes('units', 'centimeters', 'position', [ 7, 7, 5, 5]);
ax31 = axes('units', 'centimeters', 'position', [ 1, 1, 5, 5]);
ax32 = axes('units', 'centimeters', 'position', [ 7, 1, 5, 5]);
% Note that you can still use subplot to set the active axes by passing the
% axes handle as the argument. You can also use axes()
subplot(ax12)
% Plot some data
scatter( randi(10, 10, 1), randi(10, 10, 1), 'bo')
If you want to use subplot as above, it is not as easy to control position and margins. My approach would be to generate all the axes with subplot, and then set their 'units' and 'position' properties to achieve my desired layout.
fig = figure;
fig.Units = 'centimeters';
fig.Position(3:4) = [13 19];
% Make an array of your desired plot positions
plotPositions = [ 1, 13, 5, 5;
7, 13, 5, 5;
1, 7, 5, 5;
7, 7, 5, 5;
1, 1, 5, 5;
7, 1, 5, 5];
for i = 1:length(plotPositions)
plotHandle = subplot(3, 2, i);
plotHandle.Units = 'centimeters';
plotHandle.Position = plotPositions(i,:);
end
% Set the active subplot axes
subplot(3,2,2)
% Plot some data
scatter( randi(10, 10, 1), randi(10, 10, 1), 'bo')
There are some functions in the Matlab File Exchange that are pretty helpful. I have used tight_subplot(), which allows you to set sizes and margins for your plots.
Hope this helped - Good luck!
  3 Comments
Philipp Mueller
Philipp Mueller on 8 Nov 2016
@nick counts @walter roberson thank you for help ;) i see you know i am german regarding my last name :) one last question i have: i have now several subplots. how can i define the x/y axis ranges? matlab does it automatically i can not use this command -> axis ([ -500 500 0 222]);
Good luck for your election in USA
Nick Counts
Nick Counts on 8 Nov 2016
You have several options for setting axis limits:
set(gca, 'XLim', [2 12])
xlim([2 12])
axis([x1 x2 y1 y2])
axisHandle.XLim = [2 10] %if you have the handle to the axes
Your code worked for me. I think you are correctly setting your axis boundaries and then when you plot to the axes it automatically rescales.
look at doc axis and consider
axis manual
if you are setting up your subplots first and you know in advance what your desired X and Y limits are, then you can:
axis manual
hold on
xlim([2 12])
ylim([-5 5])
before you start plotting. Note that you need hold on for axis manual to have the desired effect.
Hopefully this helps. If not, post some sample code and we will get it sorted out.
tschüß :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!