Revert y-axis order in BarPlot

조회 수: 15 (최근 30일)
Edoardo Taccaliti
Edoardo Taccaliti 2024년 1월 16일
편집: Dyuman Joshi 2024년 1월 16일
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에서 Color and Styling에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by