How can I create a bar plot from sorted values with correct sorted label values?

조회 수: 3 (최근 30일)
Dear All, I would like to create a bar plot with sorted precipitation data (pr) along its sorted date (date). I tried the following:
labels=num2str(date);
bar(pr);
set(gca,'XTickLabel',labels);
I got the attached figure. I do not understand why the figure shows the date incorrectly? I am mean I would like to have every bar one label value. Could someone suggest me a solution? Thank you for your help in advance!
  댓글 수: 1
Szabó-Takács Beáta
Szabó-Takács Beáta 2017년 3월 1일
Dear All, I could improve the bar.fig figure. I edited its xticks in Property Editor. Now it shows every date but unfortunately, the x-axis is slipped a bit. Could someone suggest me a solution?

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

답변 (1개)

Rik
Rik 2017년 2월 24일
편집: Rik 2017년 3월 1일
As far as I understand this plot and the axis and bar objects it contains, you have only put in 4 years, but you have many more bars in pr. What you should try is the following:
sorted_date=sort(date);
labels=num2str(sorted_date);%date must be a col-vector (nx1)
bar(date,pr);
set(gca,'XTick',sorted_date);
set(gca,'XTickLabel',labels);
  댓글 수: 2
Szabó-Takács Beáta
Szabó-Takács Beáta 2017년 2월 27일
Dear Rik,
Thank you for your comment, but unfortunately, it does not work. I tried your suggestion and I got the error message:
Error using matlab.graphics.axis.Axes/set
While setting the 'XTick' property of 'Axes':
Value must be a vector of type single or double whose values increase
Rik
Rik 2017년 3월 1일
Your dates are apparently not sorted, which I did not expect. I'll edit my answer.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by