Bar graph doesn't show bars

조회 수: 18 (최근 30일)
Batuhan Arik
Batuhan Arik 2021년 10월 7일
댓글: Batuhan Arik 2021년 10월 9일
Hello everyone, I am trying to plot a bar graph with duplicate x values. I want to sum the y values of the duplicate x values. I found some help on the internet which seems to be working. However, when I try to run the code, axes appear bur bars don't appear.
here is my code;
clc
close all
x=X(:,1);
y=X(:,2);
[xnew,~,idx] = unique(x,'rows');
ynew = accumarray(idx(:),y(:));
hold on
bar(xnew,ynew);
hold on
title('X-Ray');
xlabel('E (kEV)');
ylabel('Yıllık Kullanım Yüzde Miktarı (mCi)');
grid minor
and here how it looks when I run it
I couldn't figure out the reason. Any help is appreciated. Thanks in advance.
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 10월 7일
As outside observers, we do not know that any of the ynew values are non-zero and not nan.
min(x), max(x)
min(y), max(y)
min(ynew), max(ynew)
Batuhan Arik
Batuhan Arik 2021년 10월 7일
oh sorry, I forgot to mention it. I checked the data, y_new, x_new, x, y are all as they are supposed to be.

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

채택된 답변

Prateek Rai
Prateek Rai 2021년 10월 9일
To my understanding, you are trying to plot a bar graph with duplicate x values bur bars don't appear.
I think the problem is that the y values for duplicate x values are summing up to zero which is why you are not able to see the bars.
I will illustrate this with your example but with some arbitrary values.
x=[7,1,2,3,1,4,5,1,2,3,7,7,]';
y=[1,1,2,3,4,5,6,7,8,9,0,-1]';
I am using arbitrary x and y values here such that:
  • We have duplicate x values
  • We have duplicate x = 7 such that corresponding y value sums to 0 ( x = 7 y =1 , x = 7 y =0, x = 7 y = -1 ==> x = 7 y = 0)
Now, I will use a code similar to the code provided, and let's look at the possible bar graph.
clc
close all
x=[7,1,2,3,1,4,5,1,2,3,7,7,]';
y=[1,1,2,3,4,5,6,7,8,9,0,-1]';
[xnew,~,idx] = unique(x,'rows');
ynew = accumarray(idx(:),y(:));
hold on
bar(xnew,ynew);
hold on
title('X-Ray');
xlabel('E (kEV)');
ylabel('Yıllık Kullanım Yüzde Miktarı (mCi)');
grid minor
You can see the resultant plot in the attachment.
It is clear from the plot that we cannot see a bar for the case when x=7 because at that time the corresponding y sums to 0.
You can also refer to bar MathWorks documentation page to learn more on bar graph.
  댓글 수: 1
Batuhan Arik
Batuhan Arik 2021년 10월 9일
Thanks for your effort. I have solved the problem. Actually, there was not even an actual problem. My array included too many elements. Thus, the bars were so tiny that they were invisible. Only thing I had to do was to increase bar width to 700000. It wass all my carelessness. Sorry for wasting your time.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by