Create a bar graph where the next bar starts where the previous ends
6 ビュー (過去 30 日間)
古いコメントを表示
I have a data set where I have alternatingly positive and negative values for each year. I need to plot them on a bar chart but the 2nd bar must start where the first bar ends. I cannot make it work with changing basevalue and can't find anyother option.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/849300/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/849305/image.png)
0 件のコメント
採用された回答
Matt J
2021 年 12 月 31 日
編集済み: Matt J
2021 年 12 月 31 日
For example,
y = [4 2 3;
1 6 5]';
n=size(y,1);
dy=([1,-1].*y).';
yc=[0, repelem(cumsum(dy(:).'),2)];
ymin=min(yc(:));
lims=sort(reshape(yc(1:end-1),2,[]))-ymin;
ys=[lims(1,:); diff(lims)]';
h=bar(ys,'stacked','FaceColor','flat','ShowBase',0);
h(1).Visible='off';
h(2).CData(1:2:end,:)=repmat( [0 0 1] ,n,1);
yticklabels( str2double(yticklabels) +ymin + "")
7 件のコメント
その他の回答 (1 件)
Image Analyst
2021 年 12 月 31 日
I think you'll have to use the patch() function to create rectangular patches "floating" off the axis.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!