x-axis error when adjusting axis label

조회 수: 1 (최근 30일)
Sehoon Chang
Sehoon Chang 2020년 10월 20일
댓글: Sehoon Chang 2020년 10월 20일
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
Sehoon Chang
Sehoon Chang 2020년 10월 20일
thank you for the alternative option.

댓글을 달려면 로그인하십시오.

채택된 답변

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