x-axis error when adjusting axis label

1 次查看(过去 30 天)
Hello all,
i am having trouble with x-axis adjustment.
Below is the original code before implementing any change to the x-axis.
As it is to be seen on the first picture, the x-axis labeling is clustered and the numbers are not horizontally placed.
figure
boxplot(TT3.P,round(TT3.OT),'PlotStyle','compact')
title('Power consumption depending on the outside temperature')
xlabel('Outside temperature [°C]')
ylabel('Heating power [kW]')
grid on
In order to correct the issues mentioned above i have added 'set' to adjust the x-axis.
figure
boxplot(TT3.P,round(TT3.OT),'PlotStyle','compact')
title('Power consumption depending on the outside temperature')
xlabel('Outside temperature [°C]')
xlim([-15 35])
ylabel('Heating power [kW]')
set(gca, 'XLim',[-15 35], 'XTick', -15:5:35 , 'XTickLabel', -15:5:35);
grid on
However the plotted figure does not represent what i intended to show.
It simply shifted to the right and is not showing the negative side of values on the x-axis (as it is to be seen in the picture below).
What may be the problem and how may i proceed?
Thank you.
  2 个评论
Callum Heath
Callum Heath 2020-10-20
编辑:Callum Heath 2020-10-20
Try either:
set(gca, 'XLim',[-15 35], 'XTick', -15:5:35 , 'XTickLabel', 'auto');
Or, XTickLabel requires a 1xn cell array with string inputs! So something like this:
xticklabels=cell(1,length(-15:5:35));
indx = 1;
for i =-15:5:35
xticklabels{indx} = num2str(i);
indx=indx+1;
end
figure
boxplot(TT3.P,round(TT3.OT),'PlotStyle','compact')
title('Power consumption depending on the outside temperature')
xlabel('Outside temperature [°C]')
xlim([-15 35])
ylabel('Heating power [kW]')
set(gca, 'XLim',[-15 35], 'XTick', -15:5:35 , 'XTickLabel', xticklabels);
grid on

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-10-20
编辑:Adam Danz 2020-10-20
What appears to be x-ticks in the compact boxplot are really text labels that are a member of an hggroup along with the boxplot components.
Here are two ways to rotate the text and remove some of the "ticks".
Method 1: Remove some existing labels
This demo isolates the text labels, rotates them, and removes 2/3 of them.
boxplot(randi(50,39),(1:39)-9,'plotstyle','compact');
ax = gca();
% Get handle to the hggroup
boxplotGroupHandle = findobj(ax,'Type','hggroup','Tag','boxplot');
% Get handle to the text labels that serve as x-ticks
textHandles = findobj(boxplotGroupHandle, 'type', 'text');
% The handles are in reverse order so we'll flip the vector to make indexing easier
textHandles = flipud(textHandles);
% Rotate and recenter text
set(textHandles, 'rotation', 0,'HorizontalAlignment','center','VerticalAlignment','bottom')
% Keep 1 "tick label" out of every 3
% Instead of deleting handles, replace their string with an empty ''
rmIndx = ~ismember(1:numel(textHandles), 1:3:numel(textHandles));
set(textHandles(rmIndx), 'String', '')
Method 2: Replace the labels with actual x-ticks
This demo isolates the text labels, removes them, and adds in actual x-tick labels.
boxplot(randi(50,39),(1:39)-9,'plotstyle','compact');
ax = gca();
% Get handle to the hggroup
boxplotGroupHandle = findobj(ax,'Type','hggroup','Tag','boxplot');
% Get handle to the text labels that serve as x-ticks
textHandles = findobj(boxplotGroupHandle, 'type', 'text');
% Store their string values; note that the original order is reversed so
% we'll flip it now to correct that.
textStr = get(flipud(textHandles), 'String');
% Delete the text
delete(textHandles)
% Add xticks: add 1 tick for every 3
ax.XTickLabelMode = 'auto';
ax.XTick = 1:3:numel(textStr);
ax.XTickLabel = textStr(1:3:end);
  1 个评论
Sehoon Chang
Sehoon Chang 2020-10-20
thanks! without your advise, i wouldnt have been able to solve the problem.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by