shading an area of a plot

Hi

I'm trying to shade a couple of areas of a plot I'm creating, I need
to shade the area above one line and the area below another.
According to the documentation it looks like I need to use the fill()
method but I can't get it to work, the code I use for creating the
plot is below:

# import required modules from matplotlib
import matplotlib
from matplotlib import figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

# set matplotlib plot parameters
matplotlib.rcParams.update({
  "font.size": 8.0,
  "axes.titlesize": 10.0,
  "axes.labelsize": 10.0,
  "xtick.labelsize": 8.0,
  "ytick.labelsize": 8.0,
  "legend.fontsize": 8.0,
  "figure.dpi": 300,
  "savefig.dpi": 300,
  "text.usetex": True
  })

def parameter_space_plot(options):
  # setup figure
  fig = figure.Figure()
  FigureCanvas(fig)
  fig.set_size_inches(5, 5)
  axes = fig.gca()
  axes.grid(True)
  axes.set_xlabel("Mass 1 / $M_\odot$")
  axes.set_ylabel("Mass 2 / $M_\odot$")

  # setup mass array
  mass = numpy.arange(0, options.max_mass + 1, 1)

  # plot min/max mass lines
  axes.plot(mass, minimum_mass(options, mass), 'b-')
  axes.plot(mass, maximum_mass(options, mass), 'b-')

  # plot equal mass line
  axes.plot(mass, mass, 'k--')

  # set axes limits
  axes.set_xlim([0, options.max_mass])
  axes.set_ylim([0, options.max_mass])

  # return plot
  return fig

could anyone give me pointers as to how I could shade the region of
the plot about the maximum mass line and the area below the minimum
mass line?

Cheers

Adam

The cookbook entry

http://www.scipy.org/Cookbook/Matplotlib/SigmoidalFunctions

illustrates how to fill below lines -- it is a little more
complicated, because it fills below the intersection of two lines, but
it should help. See also
http://matplotlib.sf.net/examples/fill_demo.py

JDH

···

On 7/14/07, Adam Mercer <ramercer@...287...> wrote:

I'm trying to shade a couple of areas of a plot I'm creating, I need
to shade the area above one line and the area below another.
According to the documentation it looks like I need to use the fill()
method but I can't get it to work, the code I use for creating the
plot is below:

I found these examples whilst trying to get the fill() method to work
but couldn't get anything working. I added the line

  axes.fill(mass, minimum_mass(options, mass), facecolor='red', alpha=0.5)

which, if I'm following the example correctly, should fill the area
under the minimum mass line red, but it has no effect on the plot.

Cheers

Adam

···

On 14/07/07, John Hunter <jdh2358@...287...> wrote:

On 7/14/07, Adam Mercer <ramercer@...287...> wrote:

> I'm trying to shade a couple of areas of a plot I'm creating, I need
> to shade the area above one line and the area below another.
> According to the documentation it looks like I need to use the fill()
> method but I can't get it to work, the code I use for creating the
> plot is below:

The cookbook entry

http://www.scipy.org/Cookbook/Matplotlib/SigmoidalFunctions

illustrates how to fill below lines -- it is a little more
complicated, because it fills below the intersection of two lines, but
it should help. See also
http://matplotlib.sf.net/examples/fill_demo.py

OK, you'll probably need to give us a complete, free standing example
for us to debug this.

···

On 7/14/07, Adam Mercer <ramercer@...287...> wrote:

I found these examples whilst trying to get the fill() method to work
but couldn't get anything working. I added the line

  axes.fill(mass, minimum_mass(options, mass), facecolor='red', alpha=0.5)

which, if I'm following the example correctly, should fill the area
under the minimum mass line red, but it has no effect on the plot.

OK, you'll probably need to give us a complete, free standing example
for us to debug this.

I've attached the complete code

./params.py --min-mass 4 --max-mass 100 --output test.png

Cheers

Adam

params.py (3.18 KB)

···

On 14/07/07, John Hunter <jdh2358@...287...> wrote:

OK, the problem with this code is fill expects the vertices of the
polygon you want filled and you are only providing the top part, not
the bottom. The modified version of your code fills between your line
and the bottom of zero

We do need to provide some helper functions to make this easier

params.py (3.22 KB)

···

On 7/14/07, Adam Mercer <ramercer@...287...> wrote:

I've attached the complete code

./params.py --min-mass 4 --max-mass 100 --output test.png

Thanks John, that works great!

Cheers

Adam

···

On 14/07/07, John Hunter <jdh2358@...287...> wrote:

OK, the problem with this code is fill expects the vertices of the
polygon you want filled and you are only providing the top part, not
the bottom. The modified version of your code fills between your line
and the bottom of zero

You're welcome. If you are a svn user, I added a more efficient
poly_between to matplotlib.mlab and updated the fill_between.py
example, which shows filling below, above and between.

def poly_between(x, ylower, yupper):
    """
    given a sequence of x, ylower and yupper, return the polygon that
    fills the regions between them. ylower or yupper can be scalar or
    iterable. If they are iterable, they must be equal in length to x

    return value is x, y arrays for use with Axes.fill
    """
    Nx = len(x)
    if not iterable(ylower):
        ylower = ylower*npy.ones(Nx)

    if not iterable(yupper):
        yupper = yupper*npy.ones(Nx)

    x = npy.concatenate( (x, x[::-1]) )
    y = npy.concatenate( (yupper, ylower[::-1]) )
    return x,y

···

On 7/14/07, Adam Mercer <ramercer@...287...> wrote:

On 14/07/07, John Hunter <jdh2358@...287...> wrote:

> OK, the problem with this code is fill expects the vertices of the
> polygon you want filled and you are only providing the top part, not
> the bottom. The modified version of your code fills between your line
> and the bottom of zero

Thanks John, that works great!

Thanks John, I'll take a look.

Cheers

Adam

···

On 14/07/07, John Hunter <jdh2358@...287...> wrote:

> Thanks John, that works great!

You're welcome. If you are a svn user, I added a more efficient
poly_between to matplotlib.mlab and updated the fill_between.py
example, which shows filling below, above and between.