Custom Colors for Stacked Bar Chart

조회 수: 112 (최근 30일)
Nicole Xu
Nicole Xu 2020년 7월 28일
편집: Adam Danz 2020년 8월 3일
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?

답변 (1개)

Adam Danz
Adam Danz 2020년 7월 28일
편집: Adam Danz 2020년 8월 3일
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)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by