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

조회 수: 57 (최근 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 CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by