Issues plotting simple bar graph.

조회 수: 22 (최근 30일)
Scuba
Scuba 2023년 3월 20일
댓글: Scuba 2023년 3월 20일
Hello, I have simplified the example problem down to what you see. I have two variables size 11x1 with correspoding data. That said, once I try and plot it comes up with this error as shown below, could someoe care to explain and show what i can do to work around this. I don't understand why this wont work, both array sizes are the same it should plot x and y coordinates respectively?
A = [0.1; 0.4; 1.1; 2.2; 2.5; 0.2; 2.5; 2.2; 1.1; 0.4; 0.1];
B = [25; 20; 15; 10; 5; 0; 5; 10; 15; 20; 25];
bar(B, A);

채택된 답변

VBBV
VBBV 2023년 3월 20일
편집: VBBV 2023년 3월 20일
A = [0.1; 0.4; 1.1; 2.2; 2.5; 0.2; 2.5; 2.2; 1.1; 0.4; 0.1];
B = [25; 20; 15; 10; 5; 0; 5; 10; 15; 20; 25];
bar(A);
xticks(1:length(A));
xticklabels({B})
As @cyclist mentioned , you need to code it differently if you want to group or stack them. But if you still want the values in the same order as you mentioned, one option is to work with xticks and xticklabels
  댓글 수: 1
Scuba
Scuba 2023년 3월 20일
Thank you, I realised i've been doing bar graphs wrong in Matab... plot just the x and working with xticks and xticklabels is the way to go.

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

추가 답변 (1개)

the cyclist
the cyclist 2023년 3월 20일
편집: the cyclist 2023년 3월 20일
The error message is quite clear. Your X values are not unique.
Let's look at a simpler example:
x = [1 2 2];
y = [3 5 7];
bar(x,y)
Error using bar
XData values must be unique.
It is clear that at x==1, there should be a bar of height y==3.
But would should the bar be at x==2? Should it be 5? 7? 5+7? A bar with two sections? It is not clear, which is why the x values need to be unique.
If you want stacked or group bars, you need to code this differently.
  댓글 수: 2
Scuba
Scuba 2023년 3월 20일
I see, could i just plot the x values and change the x value lettering to what i want?
Scuba
Scuba 2023년 3월 20일
Thank you @the cyclist

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

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by