xticklabels in bar plot being messy

조회 수: 3 (최근 30일)
Doron Ben Shachar
Doron Ben Shachar 2022년 1월 10일
편집: Abhiroop Rastogi 2022년 1월 17일
Hello,
i have a code that used to work nicely:
bar(ratio,0.4)
title("M/O ratio vs land/sea ratio, 2000-2020")
ylabel("M/O ratio")
set(gca,'xticklabel',(station_name+": "+land_sea_ratio+"%"))
grid on
text([1:length(ratio)], ratio', num2str(ratio','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom',FontSize=6)
and i got this graph from it:
today i switched to another computer (the matlab version stayed same) and im getting this:
where you can see the x labels wont set up, and i can't find a way how to fix. I did add more columns, could it be the problem?
any suggestions?
thank you very much.

답변 (1개)

Abhiroop Rastogi
Abhiroop Rastogi 2022년 1월 17일
편집: Abhiroop Rastogi 2022년 1월 17일
Hey Doron Ben Shachar!
As can be seen from the above two images the number of data points in the vector "ratio" is different, which is 14 in the first figure and 19 in the figure below that. The problem you are facing is due to the "xticks" getting expanded from a tick for each data to a tick for alternate data points, as highlighted below with red circles encircling the x-tick marks.
This happens because the bar plot tries to automatically adust the tick positions when the number of data points increase.
You can eliminate this by manually setting the position of the x-ticks by using the command
xticks(1:length(ratio))
Using this will sort out the issue of expanded ticks, as shown below using the following code
ratio = [1.73, 1.20, 2.06, 7.92, 0.88, 1.53, 2.18, 1.83, 0.86, 1.18, 0.75, ...
0.90, 1.43, 1.78, 6.99, 1.60, 1.21, 1.50, 1.67];
station_name = ["Afula Nir Haemeq", "Ariel", "Sde Boqer", "Yotvata", "Jerusalem", ...
"Zemah south to Sea of Galilee", "Arad", "Zefat Har Kennan", ...
"Negba", "Besor Farm", "Eilat", "Afeq", "Bet Dagan", "En Karmel"];
land_sea_ratio = ["100", "100", "100", "100", "99", "97", "94", "94", "91", ...
"84", "79", "74", "47", "18"];
bar(ratio,0.4)
title("M/O ratio vs land/sea ratio, 2000-2020")
ylabel("M/O ratio")
% Setting ticks position manually
xticks(1:length(ratio))
set(gca,'xticklabels',(station_name+": "+land_sea_ratio+"%"))
grid on
text([1:length(ratio)], ratio', num2str(ratio','%0.2f'),'HorizontalAlignment','center','VerticalAlignment','bottom',FontSize=6)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by