How to plot bar chart with a layered background?

5 Ansichten (letzte 30 Tage)
Mark Kramer
Mark Kramer am 2 Sep. 2022
Kommentiert: Mark Kramer am 3 Sep. 2022
I have generated a bar graph consisting of 3 categorical variables, and I would like to "classify" the height of the bar graph by altering the background color (e.g. height less than 10 is green, and height greater than 20 is red). I would also like to set the xlim to be from [0 4] since this would make the graph look more asthetic, but this does not seem to work?
x_groups = categorical({'Jump Height', 'RSI','Stiffness'});
y_height = [30, 15, 26];
b1 = bar(x_groups, y_height);
b1.FaceColor = 'flat';
% Generate the relative patch 'boxes' for classification
box1 = [0 0 4 4]
box1 = 1×4
0 0 4 4
box_y1 = [0 10 10 0]
box_y1 = 1×4
0 10 10 0
box_y2 = [10 20 20 10]
box_y2 = 1×4
10 20 20 10
patch(box1, box_y1, [0 1 0], 'FaceAlpha', 0.2)
patch(box1, box_y2, [1 0 0], 'FaceAlpha', 0.2)
I would like to have the bars be in front of the patches. Please help.
  2 Kommentare
Geoff Hayes
Geoff Hayes am 2 Sep. 2022
@Mark Kramer - you may want to look at the accepted answer at https://www.mathworks.com/matlabcentral/answers/8350-how-send-to-back-patch-objects-in-a-graph to get an idea on how to change the order of objects (patches, bars) so that the patch objects are moved to the back. As for changing the x-axes limits, I'm not sure that you can do that when the categories are used for the x-axis.
Mark Kramer
Mark Kramer am 3 Sep. 2022
@Geoff Hayes thank you very much that helps a ton. I managed to find the solution for setting the x-axis limits when there is categorical data for anyone that might be interested:
hAx=gca;
hAx.XAxis.Limits = categorical({'Jump Height','Stiffness'});
Thank you again for the help!

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by