How to change the color range on bar chart

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

采纳的回答

Ashwin
Ashwin 2020-6-18
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 个评论
endystrike
endystrike 2020-6-18
编辑:endystrike 2020-6-18
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);

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by