Sorting barh plots in MATLAB -
i have data plotted in barh plot:
now sort in ascending or descending way, can me how it?
thanks in advance!
cheers, marko
in matlab, sorting (and data manipulation) handled before figure generated. once has been rendered can change appearance.
i don't know specifics of (bioinformatics?) dataset, guess based on image provided have vector data
, , cell array containing strings, labels
.
to bar plot need to:
- sort data vector
- rearrange cell array reflect new sequence
barh()
- (optional) change sort order of barh plot
it's important keep track of default sort orders in matlab:
barh
plots vectors according indices in descending order (i.e. elements higher indices appear first)sort
sorts vectors according values in ascending order
here simplified example:
data = [35.42, 32.9, 36.8]; labels = { 'foo_fastqc', 'bar_fastqc', 'baz_fastqc'}; % sort data & rearrange labels [sorted_data, new_indices] = sort(data); % sorts in *ascending* order sorted_labels = labels(new_indices); % plot in descending order figure(); barh(sorted_data); set(gca,'yticklabel',sorted_labels); % plot in ascending order figure(); barh(sorted_data); set(gca,'yticklabel',sorted_labels); set(gca,'ydir','reverse'); % flips y axis
Comments
Post a Comment