- https://www.mathworks.com/matlabcentral/answers/553894-barcharts-colours-based-on-other-vectors#answer_456622
- https://www.mathworks.com/matlabcentral/answers/486175-how-can-i-change-colors-and-generate-errorbars-in-a-bar-graph#answer_397071
Custom Colors for Stacked Bar Chart
89 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
This is my current stacked chart. How do I changed the color of each section? So for example for the first bar "Con" I would like the red and blue section to be one color, the blue and green to be another and the middle orange and purple to be a third color. In addition would I be able to do that for each bar?

0 Commenti
Risposte (1)
Adam Danz
il 28 Lug 2020
Modificato: Adam Danz
il 3 Ago 2020
Set the FaceColor property to Flat and then define the colormap which specifies the color for each segment.
Here are two demos
For stacked bar plots, it will look something like this,
bh = bar(rand(3,6),'stacked');
set(bh, 'FaceColor', 'Flat')
bh(1).CData = [0 0 1]; % Change color to first level
bh(2).CData = [0 1 0]; % Change color to second level, etc...
To set all colors in 1 line after setting FaceColor to Flat,
colors = mat2cell(jet(numel(bh)),ones(numel(bh),1), 3);
set(bh, {'CData'}, colors) % using jet colormap
% or to assign pairs of colors,
colors = repelem(mat2cell(jet(numel(bh)/2),ones(numel(bh)/2,1), 3),2,1); % requires even number of objects in bh
set(bh, {'CData'}, colors)
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Distribution Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!