Categorical bar plot in AppDesigner (change colors and add refline)

14 ビュー (過去 30 日間)
Christian
Christian 2019 年 5 月 20 日
コメント済み: Jake 2019 年 6 月 5 日
Hello everybody,
I'm currently facing some issues with my categorical bar plot in Appdesigner. In the following a short example of what I'd like to achieve:
categorie=categorical({'Asia','USA','Europe'});
categorie = reordercats(categorie,{'Europe' 'Asia' 'USA'});
Data=[Population_Europe Population_Aisa Population_USA];
bar(app.UIAxes,categorie,Data)
I'm able to plot this, but I want to add two things, which I'm currently not able to:
1) Change the colours of the bars -> e.g. Europe = blue, Asia = green and USA =red
2) Add a horizontal line at a certain value across the whole bar plot. Here's a quick example, which does not work in appdesigner:
hold(app.UIAxes,'on')
bar(app.UIAxes,xlim,[100 100], 'r','LineWidth',2)
Thank you in advance for your help and have a great day!
Cheers, Christian

採用された回答

Kojiro Saito
Kojiro Saito 2019 年 5 月 21 日
編集済み: Kojiro Saito 2019 年 5 月 21 日
1) You can set colors on each bar by CData property.
b = bar(app.UIAxes, categorie, Data, 'FaceColor', 'flat');
b.CData = [0 0 1; 0 1 0; 1 0 0];
UPDATED
2) In order to add red horizontal line, yline is the easiest way.
yline(app.UIAxes, 150, 'r', 'LineWidth', 2)
yline(app.UIAxes, 200, 'b', 'LineWidth', 2)
untitled2.png
Or alternatively, you can utilize grid, but line color and width is only one for this approach.
% Add holizontal red line using grid
grid(app.UIAxes)
app.UIAxes.XGrid = 'off';
app.UIAxes.Layer = 'top';
app.UIAxes.YTick = 100;
app.UIAxes.GridAlpha = 1;
app.UIAxes.GridColor = 'red';
This will work as the following.
  7 件のコメント
Kojiro Saito
Kojiro Saito 2019 年 6 月 5 日
yline was introduced in R2018b. Could you confirm your version?
Jake
Jake 2019 年 6 月 5 日
Kojiro, it was an issue with the version. I was on 2018a. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by