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

65 vues (au cours des 30 derniers jours)
Christian
Christian le 20 Mai 2019
Commenté : Jake le 5 Juin 2019
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

Réponse acceptée

Kojiro Saito
Kojiro Saito le 21 Mai 2019
Modifié(e) : Kojiro Saito le 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 commentaires
Kojiro Saito
Kojiro Saito le 5 Juin 2019
yline was introduced in R2018b. Could you confirm your version?
Jake
Jake le 5 Juin 2019
Kojiro, it was an issue with the version. I was on 2018a. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by