File Exchange

image thumbnail

Hatch fill patterns plus Color & Invert

version 1.1.0.0 (48.7 KB) by Brandon Levey
Updated code hatch with color and inverted hatches

140 Downloads

Updated 03 Jul 2011

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This is an update of Brian Katz's update to Ben Hinkle's original applypatch and makepatch code for replacing colors in figures with hatches as well as David's applypatch_plus. This new version allows you to either make a black and white hatch or use the colors of the fill to create colored hatches. Additionally you can invert the hatch, i.e. it is possible to have a blue hatch with white background or a white hatch with blue background. All of the previous work done in the plus addition should still work, just these are now new options. See help of functions for example usage.

Cite As

Brandon Levey (2019). Hatch fill patterns plus Color & Invert (https://www.mathworks.com/matlabcentral/fileexchange/24021-hatch-fill-patterns-plus-color-invert), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (37)

Ryan Jessup

For everyone mentioning the low resolution in the image text, try the following fix (should work when all text is alone black):

%example data - replace with yours
myRes=400;
x=[1 4; 3 2];
bar(x);
title('Example Hatch','FontSize',14,'FontWeight','Bold');
xlabel('X Axis','FontSize',12,'FontWeight','Bold'); ylabel('Y Axis','FontSize',12,'FontWeight','Bold');

%the new portion
blackImage = print('-RGBImage',['-r' num2str(myRes)]); %first grab your existing figure
im_hatch=applyhatch_pluscolor(gcf,'-\',1,[1 1],[],myRes); %or however you are calling applyhatch_pluscolor
blackThreshold = 60; %everything below this threshold is considered black
newIm = im_hatch; %im_hatch is the output of applyhatch_pluscolor

%now loop through and everywhere it is effectively black in the image we want black from, keep it; otherwise, insure that it instead matches the im_hatch file
for i=1:size(newIm,1)
for j=1:size(newIm,2)
if blackImage(i,j,1) <= blackThreshold && blackImage(i,j,2) <= blackThreshold && blackImage(i,j,3) <= blackThreshold
newIm(i,j,1)=0;
newIm(i,j,2)=0;
newIm(i,j,3)=0;

end
end
end

imwrite(newIm,[pwd '\example_hatch_w_mod.tiff'],'Resolution', myRes); %the modified version - look at the text
imwrite(im_hatch,[pwd '\example_hatch_no_mod.tiff'],'Resolution', myRes); %the original version output by applyhatch_pluscolor

I have not managed to get the legend on the bar plot, can anyone suggest something?
Here is my code>
b = bar(dataall(:,1),dataall(:,2:end));
[im_hatch,colorlist] = applyhatch_pluscolor(1,'|-.x',1,[1 0 1 1]);
grid on
ylabel('Gross Installations in Europe (GW)','fontsize',14 );
xlabel('Year','fontsize',14 );
lgd = legend('onshore', 'offshore', 'total');
lgd.Location = 'northwest';

Hangwei Lu

why my graph is in extreme low resolution. how to change it? Thanks

Dan Hallatt

The code doesn't work quite seamlessly with colour options anymore, and a slight bug fix is required (see Sri Vikram Palagummi's comment below on the hardcopy line issue). But it does produce decent black and white images with different styles of fill. I wish MATLAB would just make an official version of this though.

Huge thanks

Jack Bonoli

Does anyone have an example of their code that produces colored hatched bar graphs? I can't quite figure out how to use with my own (I am new to Matlab). Thank you!

It didnt work with stacked bar

Shuppar

applyhatch_pluscolor(fh, 'x+/.\-|.', 1, [1 1 1 1 1 0 1 1], CT);

Error using makehatch_plus
Too many input arguments.

Error in applyhatch_pluscolor (line 195)
pattern = makehatch_plus(patterns(pati),6*hatchsc,lw);

Why am I getting the above errors?

Ahsan Pasha

only works with black and white for me. If I have a coloured bar plot and want to hatch the colours only it doesnt work.
it takes all the blacks i.e. titles, axis, even the border into account and hatches them

The only problem is that it converts the plot to bitmap (raster). So, if you are considering it for eps, forget it

Cool stuff
Really helpful

Subba Reddy

Fantastic implimentation, very precise for my use, field with all parameters untill u get best results

ea ...

Big Like
Easy to use.
Works for legends.
Everything fine.
Nice job!
Thank you very much!

Nice work!

I use errorbars to my bar plots and they get almost invisible after using this function. Any workaround for this? Thanks

@Ben Oeveren I think the following line of code is a better replacement of the hardcopy line.

bits = print('-RGBImage',['-r' num2str(dpi)]);

le yu

nice

Ben Oeveren

Thanks!

I had a bug in applyhatche() with
bits = hardcopy(h,'-dzbuffer','-r0'); %telling me to use the fcn 'print' instead.

Witch didn't work for me. So I solved this by replacing the line with:
bits = getframe(gca); bits = bits.cdata;

%using matlab 2015a on a mac

Ander Biguri

Yongqiang

Awesome!!! Changed my figure in no time!!!

Brian Katz

I would like to acknowledge this work by Brendon. I would like however like to note that the development of "applyhatch_plusC" and this routine have diverged somewhat, with different approaches for creating custom hatch patterns and resolving image resolution questions. As such, cross-compatibility can no longer be assured.

Elizabeth

I've played around with the options a bit, but I cannot figure out how to create colored bars with a black pattern on top (the white patterns definitely do not show up well enough). How can I do this? Thank you!

fadela

Nice work!

fadela

joanna

It's helpful to me. It's works.

Jawad

Hi,

I tried it with bar but i am having problem with resizing the image. If i keep the image at a very large size then lines are visible but if i resize the image to a relatively smaller size, lines change and eventually become garbage. It is quite sensitive to size change, and even i don't know what is the best size of the figure at which it works. It also is overwriting the bar borders so some sides get disappear. Would you please tell if it is possible to keep the hatch in the bar as well as to resize figure so that it fits the paper? Lines in the bar move as i resize the image. What is the best size at which it works?

fan kaibo

It is very good and helpful for my work!

I tried to embed the hatched figures in postscript (ps) http://www.cs.wayne.edu/xliu/imgs/ps.jpg, but they look much worse than in pdf http://www.cs.wayne.edu/xliu/imgs/pdf.jpg. Do you know how I can fix this issue?

I tried to embed the hatched figures in postscript (ps) http://www.cs.wayne.edu/xliu/ps.jpg, but they look much worse than in pdf http://www.cs.wayne.edu/xliu/pdf.jpg. Do you know how I can fix this issue?

Sungeun Lee

Peter Costa

Stefan

Does just what I was looking for!

Bug Report :
the image generated has twice the number of rows, and columns (thus 4 times the weight) than expected by the defined size and image position.
as a result, Fonts in the result files are half the desired size. See at the end of my comment a file that demonstrates this bug.

Other comments :
This is a convenient workaround to create hatched graphichs.
input is a color graphic (either a matalb figure or an image file).
Output is a hatched graphic where each color area was changed into hatches.

I do not feel comfortable with the last two parameters (scales) because they are relative. Absolute line width and spacing would be better imho (perhaps this is possible and I missed something).

bug demo file :

txt = {'Police 6 1a23b45c67d89e0. ' ; 'Police 8 1a23b45c67d89e0. '; 'Police 10 1a23b45c67d89e0. '; 'Police 12 1a23b45c67d89e0. '} ;

% définit figure vide
fghl = figure(1) ;
clf % clear figure
hold off
set (fghl, 'PaperUnits', 'centimeters') % unité de papier
set (fghl, 'Units', 'centimeters') % unités d'écran (marche pas vraiment ...
figure_margins = [1 1] ; % En cm : position = 1 cm du coin BG de l'image,
figure_size = [8.5 5] ;% taille = largeur x hauteur, en cm.
set (fghl, 'PaperType', 'A4', 'PaperPosition', [figure_margins figure_size])
set (fghl, 'Position', [figure_margins 2*figure_size])
whitebg(fghl, 'white') ; % white back ground

% Les traits et les polices
ligne.hair = 0.5 ;
ligne.normal = 1 ;
ligne.semibold = 1.5 ;
ligne.bold = 2 ;

fontsize = [6 8 10 12] ;

hold on
plot([0 1 2], [0 1 4], 'k', 'linewidth', ligne.normal) ;
t = (1/16:1/8:1)'*2*pi;
x = sin(t);
y = cos(t);
fill(x,y,'r')
axis equal

x0a = [0 0 0 0] ;
y0a = 1.5:-0.2:0 ;
x0b = -1 ;
y0b = 3 ;
for i = 1:4
text(x0a(i), y0a(i), txt(i), 'fontsize', fontsize(i))
end

txt2 = [] ;
for i=1:4
t = txt{i} ;
txt2 = [txt2 '\fontsize{' num2str(fontsize(i)) '}' t] ;
end
text(x0b, y0b, txt2) ;

drawnow

% Transforme les couleurs en hachurage
dpi = 72

% Sauve la figure
dpitag = ['-r' num2str(dpi)] ;
print('-dtiffn',dpitag ,'test1.tiff') ; % This file is correct

[im_hatch, colorlist] = applyhatch_pluscolor(gcf,'\/',0,0,[],dpi,5,5);
imwrite(im_hatch,'test2.tiff','tiff', 'Resolution', dpi) % this file is too large.

This is a convenient workaround to create hatched graphichs.
input is a color graphic (either a matalb figure or an image file).
Output is a hatched graphic where each color area was changed into hatches.

I do not feel comfortable with the last two parameters (scales) because they are relative. Absolute line width and spacing would be better imho (perhaps this is possible and I missed something).

Updates

1.1.0.0

Adding to the acknowledgements for the entire parent-child relationship of the function.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux