Clear Filters
Clear Filters

Revert y-axis order in BarPlot

3 views (last 30 days)
I would like to revert the y-axis order of the plot , in such a way to have the most frequent element on top (close the y upper limit) and the less frequent (close to the x-sxis)
x = {'Breast' 'Colorectal' 'Lung\_NonSmallCell' 'Prostate' 'Skin\_Melanoma' 'Pancreas' 'Liver' 'Gastroesophageal' 'Ovarian' 'Kidney\_ClearCell' 'Lymphoid' 'Urothelial' 'CNS\_Medullo' 'CNS\_Glioma' 'Sarcoma\_Other' 'NET\_Pancreas' 'Biliary' 'HeadAndNeck\_Other' 'Uterus' 'Thyroid' 'Sarcoma\_GIST' 'Sarcoma\_Leiomyo' 'Cervix' 'CNS\_PiloAstro' 'NET\_Gastrointestinal' 'Lung\_SmallCell' 'Sarcoma\_Osteo' 'Kidney\_Chromophobe' 'Sarcoma\_Lipo' 'Mesothelium' 'HeadAndNeck\_SalivaryGland' 'Myeloid' 'Kidney\_Papillary' 'NET\_Lung' 'Skin\_Carcinoma'};
updatedStrings = strrep(x, '''', '"');
y = [996 710 588 560 406 347 340 328 284 237 218 200 138 131 131 119 114 91 75 67 67 64 59 58 54 53 44 43 42 41 34 34 32 26 25];
barh(x,y)
width = 1000; % Set the width in pixels
height = 900; % Set the height in pixels
set(gcf, 'Position', [100, 100, width, height]);

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 16 Jan 2024
Edited: Dyuman Joshi on 16 Jan 2024
There are multiple ways to do that.
One simple way is reverse the Y-axis direction (assuming the data is originally sorted descendingly) -
x = {'Breast' 'Colorectal' 'Lung\_NonSmallCell' 'Prostate' 'Skin\_Melanoma' 'Pancreas' 'Liver' 'Gastroesophageal' 'Ovarian' 'Kidney\_ClearCell' 'Lymphoid' 'Urothelial' 'CNS\_Medullo' 'CNS\_Glioma' 'Sarcoma\_Other' 'NET\_Pancreas' 'Biliary' 'HeadAndNeck\_Other' 'Uterus' 'Thyroid' 'Sarcoma\_GIST' 'Sarcoma\_Leiomyo' 'Cervix' 'CNS\_PiloAstro' 'NET\_Gastrointestinal' 'Lung\_SmallCell' 'Sarcoma\_Osteo' 'Kidney\_Chromophobe' 'Sarcoma\_Lipo' 'Mesothelium' 'HeadAndNeck\_SalivaryGland' 'Myeloid' 'Kidney\_Papillary' 'NET\_Lung' 'Skin\_Carcinoma'};
updatedStrings = strrep(x, '''', '"');
y = [996 710 588 560 406 347 340 328 284 237 218 200 138 131 131 119 114 91 75 67 67 64 59 58 54 53 44 43 42 41 34 34 32 26 25];
barh(x,y)
width = 1000; % Set the width in pixels
height = 900; % Set the height in pixels
set(gcf, 'Position', [100, 100, width, height]);
%Get the current axes
ax = gca;
%Reverse the y-axis direction
ax.YDir = 'reverse';

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!