Home > Net >  How to draw a colorful 1D histogram in matlab
How to draw a colorful 1D histogram in matlab

Time:03-09

I have a 1D array X which contains the statistical data of correlation coefficients, and I want to plot a colorful histogram. I use the following code:

histogram(X,10)

which can create a histogram with single color, now I want to plot a histogram with each single bar having different colors. But the 'FaceColor' option can only adjust the total color of the entire plot. Can anyone tells me how to achieve that?

CodePudding user response:

You don't have that level of control over the histogram object, instead you have to use a bar plot, where the height of each bar is best calculated with histcounts.

[h,edges] = histcounts(x,10); % calculate the histogram data
b = bar( (edges(1:end-1) edges(2:end))/2, h ); % plot the bar chart
b.BarWidth = 1; % make the bars full width to look the same as 'histogram'
b.CData = parula( 10 ); % generate colours as a 10x3 array (columns are RGB), can 
                        % do this manually if you want
b.FaceColor = 'flat';   % Make 'bar' use the CData colours

bar charts

  • Related