Technical Solutions
How do I define colors for individual bars on my bar graph according to their values in MATLAB 7.1 (R14SP3) ?
Date Last Modified: Monday, December 7, 2009
| Solution ID: |
|
1-1T6UHS |
| Product: |
|
MATLAB |
| Reported in Release: |
|
No Release |
| Platform: |
|
All Platforms |
| Operating System: |
|
All OS |
Subject:
How do I define colors for individual bars on my bar graph according to their values in MATLAB 7.1 (R14SP3) ?
Problem Description:
I am trying to change colors on the individual bars in a bar graph according to the value of my data. When I issue the following command,
mydata=rand(1,5);
h=bar(mydata);
all the bars in my bar graph have the color blue. I want the bar colors to be such that if the value of 'mydata' is between [0, 0.2), the bars are black, if the value of my data is between [0.2, 0.6), the bars are blue, and if the value of my data is between [0.6, 1] then the corresponding bars are red.
Solution:
There is no built in function in MATLAB 7.1(R14SP3) to control the color of individual bars in the bar graphs. The following example provides a method to set the color of a bar in the chart based on its value.
First, obtain the handle to the 'Children' of a bar graph.
mydata=rand(1,5); bar_h=bar(mydata); bar_child=get(bar_h,'Children');
1. In order to map the color on the bars to your data, execute the following code:
set(bar_child,'CData',mydata);
Once 'CData' is set to "mydata", the colors on the individual bars are decided by comparing their values linearly with the default colormap "jet". You can change the colors on your barplot now by changing the colormap to one of the supplied colormaps. For example,
colormap(pink)
2. Since the default colormap is "jet", the minimum value in "mydata" is mapped to blue and the maximum value in "mydata" is mapped to red. To have more control on the colors on the bar, create your own colormap
mycolor=[0 0 0;0 0 1;1 0 0]; colormap(mycolor)
This will create a colormap with black as the first color, blue as the second, and red as the third color. Applying this colormap forces the color of the bars in the chart to be dependent on a linear scale between "mydata" and the number of available colors in the colormap. So the first third of the data range is black, the second third is blue, and the final third is red.
3. To apply specific colors for specific data ranges, set the 'CDataMapping' property to 'direct'.
set(bar_child,'CDataMapping','direct');
For example, to map all values between [0, 0.2) to black, all values between [0.2, 0.6) to blue, and all values between [0.6, 1] to red, execute the following code:
mycolor=[0 0 0;0 0 1;1 0 0];
for iCount=1:length(mydata) if (mydata(iCount)<.2) index(iCount)=1; elseif(mydata(iCount)>=.6) index(iCount)=3; else index(iCount)=2; end end
Set the 'CData' to the desired indices of the colormap.
set(bar_child, 'CData',index); colormap(mycolor);
As an alternative to looping through the data, apply a vectorized method using logical arrays. In the following example, a logical array is found for a specific data range, and then wieghted by the color index associtated with the data range. The sum of the weighted logical arrays will result in the index vector.
blackRange = 1*(mydata<0.2); redRange = 3*(mydata>=0.6); blueRange = 2*~(blackRange+redRange); index = blackRange + redRange + blueRange;
|
Related Solutions: