Creating a stacked bar plot with a non-unique x-axis.

조회 수: 83 (최근 30일)
Martijn Heemskerk
Martijn Heemskerk 2022년 7월 28일
편집: Martijn Heemskerk 2022년 7월 29일
Im working with fear data where the subject will freeze for a specific amount of time. This time varies and the location on the track where this happens also varries. I have 2 arrays, one with the location and one with the length of the fear. I wanted to plot this 'stacked' in a barplot so you can easily see the amount of occurances and the total time someone was frozen at a specific location. However, when i try to do this i get the following error: "XData values must be unique."
Is there a way for me to fix this?
Some example data: (as you can see freezes can happen on the same x location, in this example (and my own data) the 1st x value corresponds with the 1st y value).
x = [2, 3, 6, 9, 2, 6, 2, 5, 4];
y = [4, 2, 3, 1, 3, 2, 5, 2, 5];
bar(x, y, 'stacked')

채택된 답변

Voss
Voss 2022년 7월 28일
Does this work?
x = [2, 3, 6, 9, 2, 6, 2, 5, 4];
y = [4, 2, 3, 1, 3, 2, 5, 2, 5];
xx = min(x):max(x);
nxx = numel(xx);
counts = histcounts(x,nxx);
yy = NaN(max(counts),nxx);
for ii = 1:nxx
yy(1:counts(ii),ii) = y(x == xx(ii));
end
bar(xx, yy, 'stacked')
  댓글 수: 2
Martijn Heemskerk
Martijn Heemskerk 2022년 7월 29일
Hey, it works, but it also doesn't work. It does allow me to plot the graph but its doing something weird to the values. I currently have 2 plots which use the same x-axis data. The bottom plot has the added y data of the duration, where the top plot is simply just a histcount of how often a freeze has occured on a specific location. As you can see the same amount of bars appear, but these bars are not on the exact same place as the graph on top. This behaviour is also seen on the rest of the plots, I simply used this one as an example. The environment x range, ranges from -2:39 (in case that changes anything).
Martijn Heemskerk
Martijn Heemskerk 2022년 7월 29일
편집: Martijn Heemskerk 2022년 7월 29일
Nevermind, i found the anomaly! My code was doing something weird, not yours. Thank you for your help!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by