- Change the colormap to define the colors you want to apply. For two colors this is basically just cmap=[r1 g1 b1;r2 g2 b2]
- Set the Z-value of your dataset to assign these colors to elements.

19 views (last 30 days)

Show older comments

Recently, I asked a question on improving the performance of plotting functions like barh. The solution is very time efficient (about 150 times faster than barh), but I find it difficult to set the colormap for the surface https://www.mathworks.com/matlabcentral/answers/414253-why-do-plotting-functions-like-bar-or-barh-take-a-long-time-to-execute.

The code provided in the answer creates a 3D surface and projects it onto XY Plane to create the stacked bar graph.This projection has the correct length for every patch, but the color order changes from what I define. The graph has around 100 patches in the XY projection and I use only two colors based on a certain criteria defined by the identf vector. The resulting figure patches don't have the same color ordering as what I defined in the col matrix. I suspect this has to do with the fact that the graph is being projected onto the XY Plane. Note: colormap was not defined in the following figures.

for icol = 1 : length(data)

if identf(icol) == 0

col(icol,1) = 0;

col(icol,2) = 0.7;

col(icol,3) = 0.7;

elseif identf(icol) == 1

col(icol,1) = 0.79;

col(icol,2) = 0.79;

col(icol,3) = 0.79;

end

end

colormap(parent, col);

jonas
on 17 Aug 2018

Edited: jonas
on 17 Aug 2018

In the answer to the other question:

"You can use a different colormap and a different method for calculating the Z-values, if you need more distinct color differences."

To clarify:

- Change the colormap to define the colors you want to apply. For two colors this is basically just cmap=[r1 g1 b1;r2 g2 b2]
- Set the Z-value of your dataset to assign these colors to elements.

In the previous answer, the Z-value was selected randomly. From your attached figures, it appears they are still assigned randomly. You need to change this line of code and assign Z-values by your criteria.

In short: colormap sets the palette, but you paint the segments through the z-data

jonas
on 21 Aug 2018

If you have a vector of 1's and 0's, just use that as Z-data.

Let's say you have durations X of two process A(0) and B(1). You want A to be red and B to be blue.

X=[5 10 5 10 10 10];

id=[0 1 0 0 1 1];

cmap=[1 0 0;0 0 1];

First, prepare data:

%%Prepare data

Z=[id id(end)]; %Add one extra point to draw last segment

Xs=[0 cumsum(X)];

Xs=[Xs;Xs];

Zs=[Z;Z];

Y=ones(size(Xs))

Ys=Y.*[0.5;1.5];

And plot:

%%Draw

surf(Xs,Ys,Zs)

colormap(cmap)

colorbar

axis([0 50 -10 10])

Voila! See how the Z-data determine the color of the segment. You don't necessarly need 1's and 0's, any pair of numbers work as identifiers.

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

Start Hunting!