3

I am trying to apply a colormap on my bar chart in matlab. It should be a simple thing to do if you read the short explanation given on the web page of matlab but I still can't make it.

b = bar(cell2mat(data_plot'))
set(gca, 'YScale', 'log');
ylabel('Some Label');
xlabel('Some Label')  
colormap (bar, copper)

I don't get a copper color map, it is the same as it was. I have also tried the following command:

colormap copper

Still no results. Can someone tell me, what my mistake is ?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
CroatiaHR
  • 615
  • 6
  • 24

1 Answers1

2

The correct use is

colormap copper

However the result is probably not what you expect because if you use the colormap like this all bars will have the first color of the chosen map.

You can achieve what I think you want to see by using a loop and individually color the bars:

y = [1 3 5; 3 2 7; 3 4 2];
fHand = figure;
aHand = axes('parent', fHand);
hold(aHand, 'on')
colors = copper(numel(y));
for i = 1:numel(y)
    bar(i, y(i), 'parent', aHand, 'facecolor', colors(i,:));
end

Output of bar chart

Axel
  • 1,415
  • 1
  • 16
  • 40
  • thank you for your answer. Could you please explain me, what exactly are you doing with **'parent'**? – CroatiaHR Jul 19 '19 at 13:53
  • 2
    The approach here is that we create a "parent" figure `fHand` and a parent axes `aHand` and then assign the individual bar charts as children to this parent axes, by specifying that the parent is `aHand`. – Axel Jul 19 '19 at 13:57