File Exchange Pick of the Week

Creating Hatched Patches 1

Posted by Jiro Doke,

Jiro's picks this week are hatchfill by Neil Tandon, applyhatch_pluscolor by Brandon Levey, and applyhatch_plusC by Brian Katz.

In graduate school, I used to spend some time preparing my plots for publication. One of the requirements was that the plots needed to be legible and comprehensible in black and white. For line plots, I made use of various line styles, thicknesses, and markers. For patches, I would use gray scale to represent the different colors. In the days of monochrome screens and low resolution dot-matrix printers, hatches used to be the way to represent various patch regions. With functions like the ones by Neil, Brandon, and Brian, you too can create hatches easily in MATLAB.

Contents

hatchfill

Neil's function allows you to pass in patch handles and convert them into hatches that are customizable. Let's look at this contour plot which contains several sections that I want to replace with hatches.

% Create two data sets
mData = membrane(1, 50);
pData = peaks(101)-10;

% Set colormap
colormap([0 0 0; summer; 1 1 1])
contourf(mData);
caxis([min(mData(:)), max(mData(:))]);

hold on
[c1, h1] = contourf(pData, [2.5 2.5]-10);
[c2, h2] = contourf(-pData, [2.5 2.5]+10);
hold off;

I can make one of them a diagonal hatch and the other one a cross hatch.

% Get patch objects from CONTOURGROUP
hPatch1 = findobj(h1, 'Type', 'patch');
hPatch2 = findobj(h2, 'Type', 'patch');

% Apply Hatch Fill
hh1 = hatchfill(hPatch1, 'single', -45, 3);
hh2 = hatchfill(hPatch2, 'cross', 45, 3);

% Remove outline
set([h1, h2], 'LineStyle', 'none')

% Change the cross hatch to white
set(hh2, 'Color', 'white')

applyhatch_pluscolor and applyhatch_plusC

Both Brandon's and Brian's entries convert the distinct colors in the whole figure into hatch patterns. The nice thing about this is that they will also take care of legends.

% Create original plot
fH = gcf; colormap(jet(4));
h = bar(rand(3, 4));
legend('Apple', 'Orange', 'Banana', 'Melon', 'Location', 'EastOutside');

% Apply Brandon's function
tH = title('Brandon''s applyhatch');
applyhatch_pluscolor(fH, '\-x.', 0, [1 0 1 0], jet(4));

% Apply Brian's function
set(tH, 'String', 'Brian''s applyhatch');
applyhatch_plusC(fH, '\-x.', 'rkbk');

set(tH, 'String', 'Original');

Both of these functions were derived and inspired by some of the same existing entries, so the functionalities are quite similar, with some differences in syntax. For example, Brandon's function has a couple of convenience syntaxes for inverting foreground/background colors and specifying different hatch line width. Brian's function can assign arbitrary mix of colors to the hatches regardless of the original colors used.

I just highlighted three entries from File Exchange for creating hatches, but there are a few other entries that you may want to check out.

Comments

Let us know what you think here or leave a comment for Neil, Brandon, and Brian.


Get the MATLAB code

Published with MATLAB® 7.12

1 CommentsOldest to Newest

I’ve seen this post some time ago, and thought it was pretty cool. Today, I needed to make some nice black and white bar figures for my paper, so I thought I would give it a try. They look not bad on screen, but they are limited to be bitmaps, so I ended up using Illustrator’s pattern fill in the end.

These postings are the author's and don't necessarily represent the opinions of MathWorks.