How to add a table beneath a plot?

32 visualizaciones (últimos 30 días)
Monica Vegsund
Monica Vegsund el 3 de Jun. de 2020
Comentada: Adam Danz el 19 de Abr. de 2023
I want to add a table beneath the the x-axis as shown in the picture.
I have made the plot, but not sure how to add the additional table. This is my code:
B30_R = [0.1305,0.2031,0.2735,0.2584,0.40526,0.5471,0.3835,0.5795,0.8407];
U30_R = [0.1291,0.1429,0.1516,0.2707,0.2861,0.3033,0.4216,0.4420,0.4586];
S30_R = [0.0879,0.1027,0.1131,0.1800,0.2060,0.2262,0.2754,0.3274,0.3473];
V30_R = [0.1351,0.1359,0.1272,0.2904,0.2722,0.2544,0.4609,0.4210,0.3758];
figure()
hold on
plot(V30_R)
plot(U30_R)
plot(S30_R)
plot(B30_R)
xlabel('Sea state')
ylabel('RMS Roll [deg.]')
title('RMS in roll at 30 deg')
legend('Violin','UT540','SWATH','Base case')
hold off

Respuesta aceptada

Adam Danz
Adam Danz el 3 de Jun. de 2020
Editada: Adam Danz el 4 de Jun. de 2020
The rows of the table are actually additional x-tick labels.
There are several ways to plot a table under the axes but the cleanest approach would be to create multi-row XTickLabels and all you need are these 6 lines of code. With this method, you don't need to worry about the spacing of your axes or the XLabel.
There are some important details that should be understood before adapting this to your needs. The details are added as footnotes.
xlim([0,9]) % [1]
labelMat= [1:numel(B30_R); B30_R; U30_R; S30_R; V30_R]; % [2]
tickLabels = compose('%5d\\newline%.3f\\newline%.3f\\newline%.3f\\newline%.3f',labelMat(:).'); % [3]
tickDefs = 'Index\newlineB30 R\newlineU30 R\newlineS30 R\newlineV30 R'; % [4]
tickLabels = [tickDefs,tickLabels];
set(gca,'XTick', 0:numel(tickLabels), 'XTickLabel', tickLabels, 'TickDir', 'out') % [5]
Footnotes:
  1. Your x-data span from 1 to 9 but xlim starts at 0 so we have an extra tick on the left for the x-tick-row-names.
  2. 'labelMat' is an n*m numeric matrix for n rows of m tick labels
  3. This is the key line of code to produce the multi-row tick labels. The '%5d' at the beginning is used to space the index integers in row 1 of the x lables so they are centered. The '5' is my estimate of what fits best. The %.3f shows 3 dp for each value in rows 2:5.
  4. This line creates the xtick row definitions on the left of the xtick 'table'.
  5. Make sure the xticks start at 0 since your data starts at 1. This will give space to add the row definitions. Also, notice that the TickDir is 'out' so that the ticks point to the labels. You could also try 'both'.
  2 comentarios
Madan Kumar
Madan Kumar el 19 de Abr. de 2023
It shows following error in matlab2015b.
Undefined function 'compose' for input arguments of type 'char'.
Adam Danz
Adam Danz el 19 de Abr. de 2023
If you scroll to the bottom of the compose doc page you'll see that this function became available in R2016b.

Iniciar sesión para comentar.

Más respuestas (1)

Matthias Kößling
Matthias Kößling el 3 de Jun. de 2020
You could do it similar to this (but you have to play with the positions and the gap value):
B30_R = [0.1305,0.2031,0.2735,0.2584,0.40526,0.5471,0.3835,0.5795,0.8407];
U30_R = [0.1291,0.1429,0.1516,0.2707,0.2861,0.3033,0.4216,0.4420,0.4586];
S30_R = [0.0879,0.1027,0.1131,0.1800,0.2060,0.2262,0.2754,0.3274,0.3473];
V30_R = [0.1351,0.1359,0.1272,0.2904,0.2722,0.2544,0.4609,0.4210,0.3758];
figure();
axes('Position',[.1 .3 .8 .6]);
hold on;
plot(V30_R)
plot(U30_R)
plot(S30_R)
plot(B30_R)
xlabel('Sea state')
ylabel('RMS Roll [deg.]')
title('RMS in roll at 30 deg')
legend('Violin','UT540','SWATH','Base case')
hold off;
uicontrol('Style','Text','String','H [m]','FontSize',14,'Units','Normalized','HorizontalAlignment','Left','Position',[.03 .1 .05 .05]);
uicontrol('Style','Text','String','T [s]','FontSize',14,'Units','Normalized','HorizontalAlignment','Left','Position',[.03 .05 .05 .05]);
H_S = [0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.50 3.75 4.25 4.75];
T_P = [4.5 4.5 5.5 6.5 7.5 7.5 8.5 8.5 8.5 9.5 9.5];
Gap = 0.8/(length(H_S)-1);
for Column = 0:1:length(H_S)
uicontrol('Style','Text','String',num2str(H_S(Column+1)),'FontSize',14,'Units','Normalized','HorizontalAlignment','Left','Position',[.1+Gap*Column .1 .05 .05]);
uicontrol('Style','Text','String',num2str(T_P(Column+1)),'FontSize',14,'Units','Normalized','HorizontalAlignment','Left','Position',[.1+Gap*Column .05 .05 .05]);
end

Community Treasure Hunt

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

Start Hunting!

Translated by