How to fill a bar plot using an indexed colormap?

29 ビュー (過去 30 日間)
10B
10B 2017 年 5 月 31 日
編集済み: Image Analyst 2017 年 5 月 31 日
Hello Community,
I have a plotting problem I cant seem to get the hang of. I have 4 model predictor variables that have been run on the same set of data. This results in the predictor variable (PV) value, and a classification between 1&4 per line. I have run the first PV and graduated the colour of the bar plot as I want (see my other question for plot example). This generates a colormap index called 'barColorMap' which I want to use on the next PV plot, so the line of data in the second plot, is coloured the same as it was in the first plot - even if the PV value changes so the location of the data line in the new plot could move. What I expect, is that the colours of the bar in the first plot are graduated, but in the second PV plot, the coloured bars become mixed or out of sequence.
I have tried a couple of approaches. Firstly:
for b = 1 : numberofbars
% Plot one single bar as a separate bar series.
handleToThisBarSeries(b) = bar(x(b), pltvar2srt(b));
% Apply the color to this bar series.
set(handleToThisBarSeries(b), 'FaceColor', barColorMap(b,:));
hold on;
end
end
and secondly, trying to access the index via handles:
% Define the custom colour order matrix as created in the first plot
% Needs to be be a 3-column matrix (for RGB) with entries in [0,1]
ColOrd = gca;
barColor = ColOrd.barColorMap;
% Determine the number of colors in
% the matrix
[m,n] = size(barColor);
hold on
for b = 1 : numberofbars
% Determine which row to use in the
% Color Order Matrix
ColRow = rem(k,m);
if ColRow == 0
ColRow = m;
end
% Get the color
Col = barColor(ColRow,:);
% Plot the data
bar(x,pltvar2srt,'Color',Col);
hold on;
end
end
and of course, neither works! The plan is to have 4 subplots to enable the comparison of how the different predictor variables behave with the same data.
So, could anyone point me in the direction of some useful information or shed some light on where I am going wrong here please? I would greatly appreciate your help.
Kind regards,
10B.

回答 (1 件)

Image Analyst
Image Analyst 2017 年 5 月 31 日
The first code works just fine. Here's proof:
numberOfBars = 7;
pltvar2srt = rand(1, numberOfBars);
x = 1 : numberOfBars;
barColorMap = jet(numberOfBars);
for b = 1 : numberOfBars
% Plot one single bar as a separate bar series.
handleToThisBarSeries(b) = bar(x(b), pltvar2srt(b));
% Apply the color to this bar series.
set(handleToThisBarSeries(b), 'FaceColor', barColorMap(b,:));
hold on;
end
Not sure what you did differently.
  2 件のコメント
Image Analyst
Image Analyst 2017 年 5 月 31 日
You need to change the b in barColorMap(b,:). It should not be the loop index but an index that you figure out based on the height of your data. Also set 'edgecolor' to 'none'.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by