Can I fill the bars of a histogram with a pattern in MATLAB 7.9 (R2009b)?
22 views (last 30 days)
Show older comments
MathWorks Support Team
on 22 Jun 2010
Edited: MathWorks Support Team
on 6 Apr 2015
I plot two histograms in the same figure, and would like to be able to easily differentiate between the histograms. For example, I would like to add a striped pattern to the bars of one of the histograms. I cannot change the color, because I need to print the figure in black and white.
Accepted Answer
MathWorks Support Team
on 6 Apr 2015
The following example illustrates how you can vary the shading of the bars in the histogram, or change the line style and add markers to the vertices to help differentiate between different histograms.
In the histogram, each bar is a face specified by four vertices. To create a simple pattern across the face, you can specify a different color for each vertex, and then set the 'FaceColor' property to 'interp' to interpolate between the vertex colors.
x = -4:0.1:4;
y = randn(10000,1);
hist(y,x)
%Find the handle to the patch object which is used to create the bars of
%the histogram
h = findobj(gca,'Type','patch');
%Get the location of the vertices and their 'CData'
v = get(h, 'Vertices');
c = get(h, 'FaceVertexCData');
%Change the color of any vertex with y = 0
ind = v(:,2)==0;
c(ind)=50;
set(h, 'FaceVertexCData', c)
%Set the face color of the patch objects to interpolate the color of the
%vertices
set(h, 'FaceColor', 'interp')
colormap gray
%%You can also change the line style, line width, or add markers to the
% vertices
set(h, 'LineStyle', ':', 'Marker', '*')
Unfortunately, it is not possible to add a striped pattern to the histogram. This is because it is only possible to specify a single color for an entire face, or the color of each vertex of a face.
0 Comments
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!