Effacer les filtres
Effacer les filtres

How to change the color range on bar chart

4 vues (au cours des 30 derniers jours)
endystrike
endystrike le 18 Juin 2020
Commenté : Ashwin le 18 Juin 2020
Hi everyone!
Is it possible to change the color range of this bar chart?
Sometimes assets' colors seems to be too similar and it's difficult to understand to which of them each color is referred to...
Thanks a lot! :)
idx = find(sum(stocks_glob_perc)~=0);
alloc_f = figure('units','normalized','position',[0 0 1 1]); %#ok<NASGU>
ax = bar(date_ax,100*stocks_glob_perc(1:end-1,idx),'stacked','BarWidth',1);
xlim([date_ax(FirstDayOfPtot)-5 date_ax(end)+5]);
set(gca,'XTick',date_ax(EoYi+1));
ytickformat('percentage');
ylim([0 100]);
datetick('x','mm/YYYY','keeplimits','keepticks');
hlegx = legend(all_i(idx),'Location','southoutside','Orientation','horizontal','FontSize',13);

Réponse acceptée

Ashwin
Ashwin le 18 Juin 2020
My understanding is that you require a way to modify the color range of the bar graph to distinctly identify the colors being used.
A direct modification of the range may lead to the same issue you’re facing. However, using the colororder function will allow you to mention desired RGB/hex values, allowing to maintain their clear distinction.
a = [1,3,6;2,5,6;8,2,1];
H=bar(a, 'stacked');
newColors = ['#eb9234'
'#34eb86'
'#d034eb'];
colororder(newColors);
Refer to the below link for more information on how to use colororder: https://www.mathworks.com/help/matlab/ref/colororder.html
  4 commentaires
endystrike
endystrike le 18 Juin 2020
Modifié(e) : endystrike le 18 Juin 2020
Thanks a lot Ashwin,
I solved combining your answer with the usage of this function found on File Exchange... ;)
alloc_f = figure('units','normalized','position',[0 0 1 1]); %#ok<NASGU>
ax = bar(date_ax,100*stocks_glob_perc(1:end-1,idx),'stacked','BarWidth',1,'FaceColor','flat');
newColors = maxdistcolor(length(idx),@srgb_to_Jab);
for k=1:length(idx)
ax(k).CData = newColors(k,:);
end
xlim([date_ax(FirstDayOfPtot)-5 date_ax(end)+5]);
set(gca,'XTick',date_ax(EoYi+1));
ytickformat('percentage');
ylim([0 100]);
datetick('x','mm/YYYY','keeplimits','keepticks');
hlegx = legend(all_i(idx),'Location','southoutside','Orientation','horizontal','FontSize',13);
Ashwin
Ashwin le 18 Juin 2020
Glad to be of help!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by