In 3D bar plot, my Ylabel is partially shown in the figure. Please help me such that the ylabel gets shown fully in the 3D graph.
Show older comments
Below is the code, where my ylabel is partially shown.
clear;
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 21)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 21)
% Define data
x = [100, 200, 300, 400, 500];
data = [
5.3, 7.3, 4.3, 3.3, 1.3;
4.3, 4.3, 4.3, 5.3, 10.3;
2.3, 7.3, 2.3, 8.3, 2.3;
1.3, 9.3, 8.3, 7.3, 0.3;
9.3, 2.3, 0.3, 2.3, 0.3;
6.3, 7.3, 9.3, 6.3, 7.3;
];
datasetNames = {'A', 'B', 'C', 'D', 'E', 'F'};
% Create a 3D bar chart
figure;
bar3(data);
% Customize the axes
set(gca, 'XTickLabel', x, 'YTickLabel', datasetNames, 'FontSize', 21); % Set x-axis labels as number of tasks
% Align labels
xlh = xlabel('Task Count', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal', 'Rotation', -30);
ylh = ylabel('Method', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal', 'Rotation', 37);
zlh = zlabel('Fitness', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal');
% Set Z-axis limits
zlim([0.31, max(data(:))]); % Start Z-axis at 0.0018
% Set view angle for better visualization
view(45, 30);
% Adjust figure size and layout
set(gcf, 'Units', 'normalized', 'Position', [0.2, 0.2, 0.6, 0.6]);
% Adjust paper size for saving
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 14.4 12.4];
fig.PaperSize = [13.4 11.5]; % width & height of page
% Save the figure
print('FST_D_S2.pdf','-dpdf','-r1200');
4 Comments
Santanu
on 12 Dec 2024
Edited: Walter Roberson
on 13 Dec 2024
Santanu
on 12 Dec 2024
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 21)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 21)
% Define data
x = [100, 200, 300, 400, 500];
datasetNames = {'AAAA', 'BBBB_{BBB}', 'CCCC_{CCC}', 'DDDD_{DDD}', 'EEEE', 'FFFF'};
data = [
0.32232487, 0.320441542, 0.318583931, 0.316753904, 0.314943598;
0.321124121, 0.319442648, 0.317649467, 0.315850378, 0.314056277;
0.321163754, 0.321163759, 0.317728854, 0.31589135, 0.314089277;
0.319935756, 0.318446025, 0.316853212, 0.315048821, 0.313293106;
0.32051537, 0.319042798, 0.317532998, 0.315527712, 0.313939031;
0.32036688, 0.318743823, 0.317055389, 0.315299815, 0.313427664;
];
% Create a 3D bar chart
figure;
bar3(data);
% Set view angle for better visualization
view(45, 30);
xticklabels(x);
yticklabels(datasetNames)
% Align labels
xlh = xlabel('Task Count','FontSize',21, 'Rotation', -30);
ylh = ylabel('Method','FontSize',21,'Rotation', 30)
zlh = zlabel('Fitness','FontSize',21);
% Set Z-axis limits
zlim([0.31, max(data(:))]);
The problem begins here; the very long y-axis ticklabel strings barelyt fit in the default allowed space with the large font. I only adjusted the rotation angle of the ylabel to match the chosen orientation so it is parallel to the axis; the original has a discernible angle. I did make a presumption the trailing were all intended to be subscripted, not just the one character; if that isn't correct, then remove the surrounding {} as your original
ylh
It appears the y-label could be moved up and to the left slightly, but any attempt I made at adjusting the position vector resulted in it disappearing at all...even
%ylh.Position=ylh.Position+[0 0 0]
caused the symptom with the postion vector not changed at all. Something internal is going on with this; the units scale is 'data', but the values on the axes don't correlate with a magnitude of 10 so not sure where that's coming from...
return
% Adjust figure size and layout
%set(gcf, 'Units', 'normalized', 'Position', [0.2, 0.2, 0.6, 0.6]);
However, I suspect the real problem is in the following -- when you set the paper size to hard limits and the position, then it is probably physically impossible for the figure as drawn to fit in that area -- the font size simply requires more space to render than there has been allowed for the labels. I don't have a printer on this system to actually try nor did I actually try to compute physical dimensions to confirm that hypothesis.
I'm not sure what else can be done other than reducing the tick label lengths and font sizes to something that will fit in the space you're allowing...
% Adjust paper size for saving
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 14.4 12.4];
fig.PaperSize = [13.4 11.5]; % width & height of page
return
% Save the figure
print('FST_D_S2.pdf','-dpdf','-r1200');
Accepted Answer
More Answers (1)
Hi @Santanu
I don’t see a problem with ylabel in the code, but the “datasetNames” are not properly formatted as shown in the reference image provided by you. To correctly display an underscore in labels, use a backslash (`\`) to escape it. This ensures that the underscore is rendered properly in the text.
You can see the modified code below:
clear;
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 21)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 21)
% Define data
x = [100, 200, 300, 400, 500];
data = [
0.32232487, 0.320441542, 0.318583931, 0.316753904, 0.314943598;
0.321124121, 0.319442648, 0.317649467, 0.315850378, 0.314056277;
0.321163754, 0.321163759, 0.317728854, 0.31589135, 0.314089277;
0.319935756, 0.318446025, 0.316853212, 0.315048821, 0.313293106;
0.32051537, 0.319042798, 0.317532998, 0.315527712, 0.313939031;
0.32036688, 0.318743823, 0.317055389, 0.315299815, 0.313427664;
];
datasetNames = {'AAAA', 'BBBB\_BBB', 'CCCC\_CCC', ['DDDD\' ...
'_DDD'], 'EEEE', 'FFFF'};
% Create a 3D bar chart
figure;
bar3(data);
% Customize the axes
set(gca, 'XTickLabel', x, 'YTickLabel', datasetNames, 'FontSize', 21); % Set x-axis labels as number of tasks
% Align labels
xlh = xlabel('Task Count', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal', 'Rotation', -30);
ylh = ylabel('Method', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal', 'Rotation', 37);
zlh = zlabel('Fitness', 'FontSize', 21, 'FontName', 'Times New Roman', 'FontWeight', 'normal');
% Set Z-axis limits
zlim([0.31, max(data(:))]); % Start Z-axis at 0.0018
% Set view angle for better visualization
view(45, 30);
% Adjust figure size and layout
set(gcf, 'Units', 'normalized', 'Position', [0.2, 0.2, 0.6, 0.6]);
% Adjust paper size for saving
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 14.4 12.4];
fig.PaperSize = [13.4 11.5]; % width & height of page
% Save the figure
%print('FST_D_S2.pdf','-dpdf','-r1200');
Hope this is helpful!
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




