MATLAB Answers

x-axis error when adjusting axis label

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

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

채택된 답변

Adam Danz
Adam Danz 20 Oct 2020
편집: Adam Danz 20 Oct 2020
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 20 Oct 2020
thanks! without your advise, i wouldnt have been able to solve the problem.

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by