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.
56 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Santanu
am 12 Dez. 2024 um 6:58
Bearbeitet: dpb
am 14 Dez. 2024 um 16:17
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 Kommentare
dpb
am 12 Dez. 2024 um 23:10
% 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');
Akzeptierte Antwort
dpb
am 13 Dez. 2024 um 22:53
Bearbeitet: dpb
am 14 Dez. 2024 um 12:35
% 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,'VerticalAlignment','bottom');
zlh = zlabel('Fitness','FontSize',21);
% Set Z-axis limits
zlim([0.31, max(data(:))]);
hAx=gca;
hAx.ZAxis.TickLabelFormat='%0.3f'; % get rid of inconsistent decimal places
Compare this figure with the prior in my earlier comment -- the default vertical alignment in the 3D orientation is 'top' which as we've noted, leaves quite a lot of white space. While my attempts to move the label mysteriously failed miserably, setting the 'VerticalAlignment','bottom' has the desired effect of appearing to move it up and to the left...
I still suspect you're going to run into trouble when try to print into the constrained space, however...
There may be some reason for the large fonts, but to my eye, they're far too large for the figure; they almost run together (are the subscripts for "B" and "C" subscripts on them or superscripts on "D" and "C", respectively?) and are overpowering--I'd cut them probably at least 1/3rd and more like 1/2....but, it's your figure in the end.
1 Kommentar
dpb
am 14 Dez. 2024 um 14:30
Bearbeitet: dpb
am 14 Dez. 2024 um 16:17
"-I'd cut them probably at least 1/3rd ..."
% 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);
hAx=gca;
xticklabels(x);
hAx.XAxis.FontSize=14;
hAx.XAxis.TickLabelRotation=30;
yticklabels(datasetNames)
hAx.YAxis.FontSize=14;
hAx.YAxis.TickLabelRotation=-30;
xlh = xlabel('Task Count','FontSize',21, 'Rotation', -30,'VerticalAlignment','bottom');
ylh = ylabel('Method','FontSize',21,'Rotation',37,'VerticalAlignment','bottom');
zlim([0.31, max(data(:))]);
hAx.ZAxis.TickLabelFormat='%0.3f'; % get rid of inconsistent decimal places
hAx.ZAxis.FontSize=14;
zlh = zlabel('Fitness','FontSize',21);
Appears more proportionally spaced to my eye, anyway, and probably gives at least a reasonable chance of being able to render the entire thing...that's 14 pts for the tick labels and keeping 21 for the axis labels and also moving the y changing the y axis label vertical alignment.
This version also moves up the x label as well; one might try 'middle' also...
NOTA BENE: With this modification, the additional adjustment to the rotation angle of the y label was reintroduced; the kerning of the tick labels makes the label appear to move away from the tick labels using the 30 degree rotation of the axes. That wasn't apparent earlier; the opposite visual effect showed up. .not quite sure the "why" of that,either...
As always "salt to suit"...
Weitere Antworten (1)
Yukthi S
am 13 Dez. 2024 um 5:42
Bearbeitet: Yukthi S
am 13 Dez. 2024 um 5:42
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!
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!