Revert y-axis order in BarPlot

3 次查看(过去 30 天)
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]);

采纳的回答

Dyuman Joshi
Dyuman Joshi 2024-1-16
编辑:Dyuman Joshi 2024-1-16
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';

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by