File Exchange

image thumbnail

Add a background gradient to plots

version (119 KB) by Michelle Hirsch
Add a background gradient to plot axes to make them look a little fancy.


Updated 17 Aug 2015

From GitHub

View Version History

View license on GitHub

The picture pretty much says it all. This is a simple function for adding a nice looking background gradient on your plots. It's useful for making things look a little fancy.
Note: The author does not condone the fancification of plots to make up for a complete lack of substance!

Good luck :)

Cite As

Michelle Hirsch (2021). Add a background gradient to plots (, GitHub. Retrieved .

Comments and Ratings (17)

Egor Buiko

Heather Gorr

Michelle Hirsch

You are right, Ali. I didn't actually have useful use cases like yours in mind when I created this :)
Here's a simple example that I think does what you want (but not packed into a convenient function). You would change the value of the variables colors and breakpoints to match your data.

ax = gca;

% Define colors. One row per region
colors = [1 0 0
0 1 0
0 0 1
.5 .5 .5];

% Define breakpoints - where each color changes. These are in x dimension
breakpoints = [1 2 3 4 5];

% Set things up
Npatches = length(breakpoints)-1;
p = zeros(1,Npatches); % Store handle to each patch
yl = ylim;
ydata = [yl(1) yl(1) yl(2) yl(2)]; % Span the y axis

% Add each patch
for ii=1:Npatches
xdata = [breakpoints(ii) breakpoints(ii+1) breakpoints(ii+1) breakpoints(ii)];

p(ii) =patch(xdata,ydata,colors(ii,:),'Parent',ax,'EdgeColor','none');

% Clean up graphics
uistack(p,'bottom') % Put gradient underneath everything else

Ali Sarafraz

I have four regions in my plot which are not so distinct. I mean that for example, I have (0<x<a), (a<x<b), (b<x<c), (c<x<d), but a,b,c, and d are not fixed. I wanted to have a color gradient thus, with four major colors as a background. However, it seems that this function produces color in the y-direction. Am I right?

Michelle Hirsch

Thanks for the fix, Benjamin. I just integrated it and pushed this up to GitHub.

Benjamin Bachmann

if you don't want your grid to be covered by the background patch, add this line at the end of the code:

Michelle Hirsch

Cool idea, Weidong. I don't have time to work on this but I welcome a pull request on GitHub from anybody who wants to take a stab at it.

weidong Li

It is wonderful. Is it likely to make the gradient varying diagonally from the origin to the max. X and Y?

Rodrigo Botelho

Luca Broggi

Really useful!

Michelle Hirsch

Interesting, Yasser. I'm having a hard time picturing how this would work for 3D plots. Do you envision gradients along the box boundaries, or perhaps a transparent cube with a gradient, or maybe something else?



would it be possible to make it for 3D plots?


Michelle Hirsch

All - Jan pointed out the issue to me, where the gradient covered everything else in your plot. I posted the update to GitHub.

Michelle Hirsch

Hmmm, this is working fine for me. Are you using the version from GitHub? I updated it last June.

If you are using the latest version and still having issues, please let me know what error message you get.

Here's a direct link to the GitHub repository:

Jan de Wilde

Please can you adapt your function for R2014b? Your example doesn't work:

Jan de Wilde

Jan de Wilde

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!