Reduce x-axis line length in a bar plot that is in a tiledlayout

4 visualizzazioni (ultimi 30 giorni)
I am having 3 bar plots in one figure, two on the top row and on the bottom row. I want to use tight tile spacing
% Define the data 1
Names1 = {'A'; 'B'}; % Categories
Data1 = [16; 2]; % Data for the table
% Create the table
T1 = table(Names1, Data1);
% Define the data 2
Names2 = {'Red'; 'Black'}; % Categories
Data2 = [5; 13]; % Data for the table
% Create the table
T2 = table(Names2, Data2);
% Define the data 3
Names3 = {'Small red cat'; 'Large red cat'; 'Small black cat'; 'Medium black cat';'large black cat'}; % Row names
Data3 = [4; 1; 1; 8; 4]; % Data for the table
% Create the table
T3 = table(Names3, Data3);
f1 = figure(1);
% Create a tiled layout for the bar plots
t = tiledlayout(2, 2, 'TileSpacing', 'tight');
ax1 = nexttile; %% first row first column
b1 = bar (T1.Names1,T1.Data1);
% Display the height values, stored in the YData property, at the tips of the of bars
b1(1).Labels = b1(1).YData;
% Remove the frame around the plot
ax1.Box = 'off';
% Remove y-axis and numbers
set(ax1, 'YColor', 'none');
set(ax1, 'YTick', []);
title("a)")
%--------
ax2 = nexttile; %% first row second column
b2 = bar (T2.Names2,T2.Data2);
% Display the height values, stored in the YData property, at the tips of the of bars
b2(1).Labels = b2(1).YData;
% Remove the frame around the plot
ax2.Box = 'off';
% Remove y-axis and numbers
set(ax2, 'YColor', 'none');
set(ax2, 'YTick', []);
% Add title
title("c)")
ax4 = nexttile([1 2]); %% [1 2] makes over two columns %% second row column 1 and 2
b4=bar (T3.Names3([1 2],:),T3.Data3([1 2],:), 'FaceColor',[0 0.6 .078]);% rural Green
hold on
b5=bar (T3.Names3([3 4 5],:),T3.Data3([3 4 5],:),'FaceColor',[.5 0 .5]);%% built-up area violet
% Display the height values, stored in the YData property, at the tips of the of bars
b4(1).Labels = b4(1).YData;
b5(1).Labels = b5(1).YData;
hLg = legend("Red cat","Black Cat","Location","northwest");
hold off
% Remove the frame around the plot
ax4.Box = 'off';
% Remove y-axis and numbers
set(ax4, 'YColor', 'none');
set(ax4, 'YTick', []);
%xlim tight; % This will adjust the limits to fit the data closely
xticklabels({'small','large', 'small', 'medium', 'large'})
title("d)")
As you can see the lines of the x-axis is getting unneccessary long and it looks like they would almost be connected.
So I would like them to be shorter. Kind of the length they hav between A and B but to the side
I tryed to use
% Set the x-axis limits for the first plot
%xlim(ax1, [1, 2]); % Replace xmin and xmax with your desired limits
However, I am unsure if these are the limits. Since the data are a categories A and B
and I tryed to use
%xlim tight; % This will adjust the limits to fit the data closely
No effect on the line length
How should I approche the problem?
Thanks for your support.

Risposta accettata

Matt J
Matt J il 10 Set 2025
Modificato: Matt J il 10 Set 2025
For finer control, download subaxis. Use that instead of tiledlayout.
clear,
%close all
% Define the data 1
Names1 = {'A'; 'B'}; % Categories
Data1 = [16; 2]; % Data for the table
% Create the table
T1 = table(Names1, Data1);
% Define the data 2
Names2 = {'Red'; 'Black'}; % Categories
Data2 = [5; 13]; % Data for the table
% Create the table
T2 = table(Names2, Data2);
% Define the data 3
Names3 = {'Small red cat'; 'Large red cat'; 'Small black cat'; 'Medium black cat';'large black cat'}; % Row names
Data3 = [4; 1; 1; 8; 4]; % Data for the table
% Create the table
T3 = table(Names3, Data3);
f1 = figure(1);
% Create a tiled layout for the bar plots
% t = tiledlayout(2, 2, 'TileSpacing', 'tight');
% ax1 = nexttile; %% first row first column
ax1=subaxis(2,2,1,'SH',0.01);
b1 = bar (T1.Names1,T1.Data1);
% Display the height values, stored in the YData property, at the tips of the of bars
b1(1).Labels = b1(1).YData;
% Remove the frame around the plot
ax1.Box = 'off';
% Remove y-axis and numbers
set(ax1, 'YColor', 'none');
set(ax1, 'YTick', []);
title("a)")
%--------
ax2=subaxis(2,2,2,'SH',0.01);
b2 = bar (T2.Names2,T2.Data2);
% Display the height values, stored in the YData property, at the tips of the of bars
b2(1).Labels = b2(1).YData;
% Remove the frame around the plot
ax2.Box = 'off';
% Remove y-axis and numbers
set(ax2, 'YColor', 'none');
set(ax2, 'YTick', []);
% Add title
title("c)")
ax4 = subaxis(2,2,1,2,2,1,'PT',0.05);
b4=bar (T3.Names3([1 2],:),T3.Data3([1 2],:), 'FaceColor',[0 0.6 .078]);% rural Green
hold on
b5=bar (T3.Names3([3 4 5],:),T3.Data3([3 4 5],:),'FaceColor',[.5 0 .5]);%% built-up area violet
% Display the height values, stored in the YData property, at the tips of the of bars
b4(1).Labels = b4(1).YData;
b5(1).Labels = b5(1).YData;
hLg = legend("Red cat","Black Cat","Location","northwest");
hold off
% Remove the frame around the plot
ax4.Box = 'off';
% Remove y-axis and numbers
set(ax4, 'YColor', 'none');
set(ax4, 'YTick', []);
%xlim tight; % This will adjust the limits to fit the data closely
xticklabels({'small','large', 'small', 'medium', 'large'})
title("d)")
  1 Commento
Désirée Kroner
Désirée Kroner il 10 Set 2025
Modificato: Désirée Kroner il 10 Set 2025
The function subaxis doesn't modifies the length of the line as intended but it solves my problem with the overlapping lines.
Thanks for your answer it was really helpfull.

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by