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

58 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 21 Mai 2019
Bearbeitet: Kojiro Saito am 21 Mai 2019
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 Kommentare
Jake
Jake am 5 Jun. 2019
Kojiro, it was an issue with the version. I was on 2018a. Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by