File Exchange

image thumbnail

applyhatch_plusC

version 1.4.0.0 (4.22 KB) by Brian Katz
Modification of APPLYHATCH_PLUS, allowing for color and variable thickness for hatch patterns.

77 Downloads

Updated 21 Apr 2017

View License

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

This is a modification of previously submitted APPLYHATCH_PLUS.
APPLYHATCH_PLUSC creates a bitmap version of the defined figure where colored elements are replaced with specified colored hatch patterns.
APPLYHATCH_PLUSC allows for the definition of colors for the hatch patterns. Color may be defined using char string or RGB values. Various examples are provided showing some possibilities of APPLYHATCH_PLUSC and MAKEHATCH_PLUS.
Note: This function is different from Brandon Levey's APPLYHATCH_PLUSCOLOR, which has slightly different functionality and syntax.

Comments and Ratings (31)

Brian Katz

Hi @Min-He. In your example, this is not possible, because 1 & 6 are the same color from bar. It is the original color that is used to identify elements (see description "colored elements are replaced with specified colored hatch patterns"). They cannot be assigned different hatch info if they are the same color in the original plot. You would need to generate the base plot differently with different colors for the different elements you want to define.

Min He

Hi, @Brain Katz,I am sorry to disturb you.I have a question in using this function and I don't know how to solve it.I want to use bar with style stacked,and the same stack use same hatch and different color.For example:
num = [1 2 3 4 5;6 7 8 9 10];
x = [2,8];
bar(x,num,'stacked');
I want the first stack [1 2 3 4 5] use the same hatch and different color,[6 7 8 9 10] use the same hatch and different color.But when I use applyhatch_plusC,I found 1 and 6 have same hatch.Could you help me solve this problem?Thanks a lot!

RMan!

@Brian, please update when you can. I would love to use this! There doesn't seem to be any other tools of this type available. Thank you for your great work!

Brian Katz

As noted by @liruixinch, you may need to adjust the DPI to get the results you want. MatLab changed the way BMP images are created and I don't like them as much. As such moving the DPI to 300 or 600 provides cleaner text, but you may need to adjust the patterns to be visible. I will think about a way to define different resolutions (for text and hatch), but I do not have time for that soon.
Please test export to TIFF file, in case the poor quality is MatLab image rendering too...

Mei Li

Hi, @Brian Katz,I am a graduate student. I can run the examples in the document using Matlab 2017b, but the figure is not complete. I mean it lost some feature or displayed not matching the code. Moreover, the letters and numbers are very fuzzy. Could you help me solve this problem? Thanks a lot

Brian Katz

@Shuppar, you do not seem to be floolowing the functions syntax, and I do no know what you are trying to do as it is incorrect. Please try the examples and correct this, starting with the simpler cases. PATTERN and PATTERNCOLORS must be the same length.
applyhatch_plusC(h,patterns,patterncolors)

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?

Thank, this version of code can run in MATLAB2018. And we can adjust the 5th inputing parameter to make the number more smooth.

Brian Katz

Note: This fucntion is currently having issues with the latest version of MatLab (starting from 2017b). I am currently trying to resolve the issue... The error is not regular, but results in non-unique colors/patterns being attributed. Please check your output if you continue to use for now.

Brian Katz

Danny, did you try to improve the resolution of the BMP export for printing? This comment was previously raised and the solution is well documented in the function.

Danny

Hello, I am a graduate student using MATLAB 2018a. This code works very well for my bar, but the results are not pretty. The graph I got from this code looks like the low quality figure that I've seen in papers published in the 80s-90s. Especially the letters are not smooth.

Brian Katz

This is something this funtion should do easily. So, sorry, but I don't understand your question. Have you tried to use the function? What is the difficulty?

Tiago Dias

Hello, this work if i have a normal plot, and in a well defined section (x1,y1) -> (x2,y2) instead of filling a blue solid, i woul like to fill with blue diagonal lines?

Ye Xie

Luis Marval

Brian Katz

I don't really understand this comment. If you mean that the resolution of the numbers (tick labels, etc.) is poor, this is due to the fact that this function creates a BMP image. If the resolution is not to your liking, you simply need to increase the DPI. This is what I do for published works, as opposed to screen resolution (the default). Inceasing the DPI will also affect the pattern density, so some playing may be necessary to get exactly what you want.

junmin wang

Thank you. But the number is ugly.

Brian Katz

I have made some very small revisions and the function now works again.
I have tested all examples and there are no more problems as reported.

seems to be outdated, even the example results in a bad quality plot with Matlab R2015a

Hi,
When using "aplyhatch_plusC" function, I found the following error.
PATTERN and PATTERNCOLORS must be the same length.
Would you please guide how to fix this error.

Thanks in advance.

Harsh

Hello,
I used both applyhatch_plus and makehatch_plus as directed in example given inside the applyhatch_plus. However, in both case I am getting an error stating as below:
example:
bar(rand(3,4));
im_hatch = applyhatch_plus(gcf,'\-x.',600);
Error message:
"Attempted to access colorlist(1,2); index out
of bounds because numel(colorlist)=1.

Error in applyhatch_plus>nextnonbw (line 142)
colors = (colorlist(out,1) == bits(:,:,1))
& ...

Error in applyhatch_plus (line 69)
[colors,colori] =
nextnonbw(0,colorlist,bits);"

I am sure as people had used it multiple time, it is working.
But I am not sure what wrong I did.

Thank you for your help.

Brian Katz

This correction has now been included in the recent update.

When you test the length, now on line 95, you use the length function which returns the number of elements in the longest dimension (eg. A = [2x3] then length(A) = 3) This will cause a patterncolor matrix with <3 colors (rows) to fail because length(patterns) <3 and length(patterncolor) = 3.

If you use size(patterncolors,1) instead of length(patterncolors) (in the condition on line 95) this problem will be removed as it only returns the number of elements in the first dimension.

Brian Katz

I am sorry but I don't understand the proposed modification. It is necessary to have an equal number of entries in PATTERNS and PATTERNCOLORS, otherwise there would be too few/many combinations. The PATTERNCOLORS can be either a string array of color chars, or a 3 column RGB value. This cannot be more or less than 3 to be RGB.
If you have a specific example where there is a problem, please send it to me.

I had to make an alteration to line 97 of applyhatch_plusC to handle a matrix with less than 3 rows for patterncolors.

96: [patterncolors_row patterncolors_col] = size(patterncolors);
97: if length(patterns) ~= patterncolors_row
98: error(...)
99: end

Do you think you could make this, or another appropriate alteration, to allow matrices with less than 3 rows.

Brian Katz

applyhatch_plusC applies to the entire figure, subplots included, so I don't understand the question. You cannot, however, use different hatching rules for two subplots. But, if you assure that the original subplots do not have any colors in common, you should still be free to make the results be anything you want.

Hey there,
anyway to use it with subplots?
cheers,
Ricardo

good!

Zuofeng

Nice tool!

Helped me a lot!

Jveer

Updates

1.4.0.0

Small edits to correct reported errors.
All examples function now correctly as before.

1.3.0.0

Correction to account for situations with less than 3 colors, as noted by commentors.

1.1.0.0

Now includes modified makehatch_plus to allow for variable thickness hatches

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

Inspired by: Hatched Fill Patterns Plus

Inspired: stipple