i need to make space between two bar plot in x axis

조회 수: 48 (최근 30일)
eng m
eng m 2021년 5월 27일
댓글: eng m 2021년 5월 27일
i have this code
i need to make space between lena pepper airplane cut ,i dont need to near each to other
n=[81.2858,82.6818,82.0401,82.0560];
bar(diag(n),'stacked');
set(gca,'xticklabel',{lena','pepper ','airplane','cut'});
text(1:length(n),n,num2str(n','%0.2f%%'),'vert','bottom','horiz','center','FontSize',12, 'Color','black','FontName','memoir');
box off

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 27일
편집: Scott MacKenzie 2021년 5월 27일
With the labels you are using, a multi-line tick label might look better:
n = [81.2858,82.6818,82.0401,82.0560];
bar(diag(n), 0.6, 'stacked');
text(1:length(n),n,num2str(n','%0.2f%%'),'vert','bottom','horiz','center','FontSize',12, 'Color','black','FontName','memoir');
box off;
ax = gca;
ax.XTick = [1 2 3 4];
ax.XTickLabel = '';
myLabels = { 'lena', 'pepper', 'airplane', 'cut';
'transform', 'transform', 'transform', 'transform' };
for i = 1:length(myLabels)
text(i, ax.YLim(1), sprintf('%s\n%s\n%s', myLabels{:,i}), ...
'horizontalalignment', 'center', 'verticalalignment', 'top');
end
ax.XLabel.String = sprintf('\n\n%s', 'X-Axis Label');
Some discussion on alternative ways to create multi-line tick labels is here.

추가 답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 27일
Hi,
...
bar(diag(n),1); % Change 1 to any other number to make bars closer and more appart
...

Walter Roberson
Walter Roberson 2021년 5월 27일
Your axes is not wide enough to hold the full width of your tick labels with your current font size. The only way you could make your bars far enough apart that your labels do not overlap, would be if you used xlim() to zoom in to part of the plot, so that all of the bars and labels are not visible at the same time.
You need to do one of the following:
  • reduce the font size so that all the labels fit
  • use a larger figure to fit a larger plotting window
  • rotate the labels so they go down or at an angle so that they do not overlap; for example you might be able to use 30 degrees
  • depending how wide your window is, switching to latex might happen to allow the labels to fit, such as set(gca,'xticklabel',{'lena-transform','pepper-ransform','airplane-transform', 'cut-transform'},'ticklabelinterpreter','latex');
  • switch to tex interpreter: if the labels are too large to fit, it will automatically put them at an angle; set(gca,'xticklabel',{'lena-transform','pepper-transform','airplane-transform', 'cut-transform'},'ticklabelinterpreter','tex');
  • use tex or latex facilities to insert line breaks. This is a more advanced topic that I would have to look up

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by