Bar chart samples are shuffled

4 ビュー (過去 30 日間)
Iron1759
Iron1759 2021 年 10 月 2 日
編集済み: dpb 2021 年 10 月 3 日
Hi,
I want to create a stacked bar chart that is ordered according to total values; however, no matter what I do it seems that dispite the values matrix for the bar chart, the stacks are shuffled around regardless the order of values in the matrix:
here is the ordered values matrix according to total values - each row represents a sample and contains two observations:
8.89817512249006 10.6114775114530
8.97084136825914 10.6925827879448
9.48697169408196 10.9541916851987
9.68518825475912 10.8490244459713
10.0674964213323 11.5608323808692
The categorical variable:
X = {'Pristine' 'Ne' 'He' 'Ar' 'Kr'};
b = bar(X,vals,'stacked');
and the result attached - like the rows and X are mixed.
Really don't know what's going on.
Thanks!

採用された回答

dpb
dpb 2021 年 10 月 2 日
編集済み: dpb 2021 年 10 月 2 日
As @Star Strider says, categorical variables are not ordinal by default and are valued by lexical sorting. To fix
X = {'Pristine' 'Ne' 'He' 'Ar' 'Kr'};
X=categorical(X,X,"Ordinal",1)
hB = bar(X,E,'stacked');
...
Produces
in which the order is preserved since X is now an ordinal categorical array.
You can generalize the above by using sort with the optional returned index array by which to reorder the categorical values array to match the data order desired or for different datasets before creating the categorical variable.
  2 件のコメント
Iron1759
Iron1759 2021 年 10 月 3 日
Without the 'E' :)
Thanks!
dpb
dpb 2021 年 10 月 3 日
編集済み: dpb 2021 年 10 月 3 日
I used E as local variable based on the title in your graph...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by