File Exchange

image thumbnail

hfill and vfill

version 1.2.1 (226 KB) by Chad Greene
Simply create horizontal or vertical shaded sections of a plot.


Updated 20 May 2019

View License

h = vfill(xbounds,ColorSpec,varargin) creates fill objects bounded by the values xbounds. ColorSpec defines the color of the fill objects. Optional varargin can be used to set edgecolor, transparency, etc.


hfill([ystart yend])
h = hfill(...)


hfill(scalarValue) places a horizontal line along y = scalarValue.

hfill([ystart yend]) fills a horizontal shaded region bounded by ystart and yend.

hfill([ystart1,yend1,ystart2,yend2,...,ystartn,yendn]) fills multiple horizontal regions.

hfill(...,ColorSpec) defines the face color of the patch(es) created by hfill. ColorSpec can be one of the Matlab color names (e.g. 'red'), abbreviations (e.g. 'r', or rgb triplet (e.g. [1 0 0]). ColorSpec may also be 'gray'.

hfill(...,ColorSpec,'PatchProperty','PatchValue') defines patch properties such as 'EdgeColor' and 'FaceAlpha'.

hfill(...,'bottom') places the newly created patch(es) at the bottom of the uistack.

h = hfill(...) returns handle(s) of newly created patch objects.

Cite As

Chad Greene (2019). hfill and vfill (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


Very nice! Just a suggestion. When you check whether you have a sequence of [yStart, yEnd] endpoints with even number of elements, you use fprintf. Within the strings passed to fprintf, add the '\n' (sans quotes) at the end to force newlines whenever the message is thrown at the user. Also consider explicitly throwing an error or warning.

Great program. However, I am noticing a persistent edgecolor when I set the fill to 'bottom' despite calling the edgecolor 'none'. How would I go about removing the bottom edge color? Thanks!

Chad Greene

Tip: If you like the example image, but you'd prefer the gray box to be behind your data instead of on top of it, reorder the stack like this:

h = vfill([80 90],'gray');



Added live scripts

Simplified syntax for plain vertical or horizontal lines, added an example file.

Added hfill and the stacking command.

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

Inspired by: hline and vline, Improved 'vline'