Plotting curves filled with nonuniform color patch

Hi,

I'm having hard time understanding some of the differences between functions used to plot color patches (not sure what to call them).

I'm trying to fill a curve with a nonuniform color patch (like fill or fill_between but the color in the patch varies). I've attached code that almost does what I want; my question concerns the color patch (which is created by the call to plt.pcolor in the code below). I'd like to have nonuniform grid spacing in the color values, and also shading (i.e. interpolation of color between points). Here's what I understand:

pcolor: allows nonuniform grid spacing, but it doesn't do shading.

imshow: allows color shading, but requires uniform spacing

pcolormesh: allows color interpolation and nonuniform grid spacing

pcolormesh seems like the ideal candidate, but when I replace pcolor with pcolormesh (code commented out below pcolor call), the path doesn't get clipped by set_clip_path (but no errors are raised); in other words, the color shading fills the entire plot area. Is this a bug?

Is there a way of making this plot work that I've overlooked?

Thanks!
-Tony

#~~~~ example code

import matplotlib.pyplot as plt
import numpy as np

def plot_filled_curve(x, y, c):
    """Plot curve filled with color patch
    
    Parameters

···

----------
    x, y : arrays
        points describing curve
    c : array
        value of describing color gradient filling the curve. Must match the
        lengths of `x` and `y`.
    """
    # add end points so that fill extends to the x-axis
    x_closed = np.concatenate([x[:1], x, x[-1:]])
    y_closed = np.concatenate([[0], y, [0]])
    # fill between doesn't work here b/c it returns a PolyCollection, plus it
    # adds the lower half of the plot by adding a Rect with a border
    patch, = plt.fill(x_closed, y_closed, facecolor='none')
    X, Y = np.meshgrid(x, [0, y.max()])
    # take average since C specifies color in between X, Y points
    C = [((c[:-1] + c[1:]) / 2.)]
    im = plt.pcolor(X, Y, C, cmap=plt.cm.gray, vmin=0, vmax=1)
    # C = np.vstack((c, c))
    # im = plt.pcolormesh(X, Y, C,
    # cmap=plt.cm.gray, vmin=0, vmax=1, shading='gouraud')
    im.set_clip_path(patch)

if __name__ == '__main__':
    x0 = .45
    x = np.linspace(0, 1, 11)
    x = np.insert(x, (5, 5), (x0,)*2)
    y = np.hstack(([2]*7, 2*np.linspace(0.9, 0, 6)**2))
    c = np.hstack(([0]*6, [1], np.linspace(0.9,0,6)))
    plot_filled_curve(x, y, c)
    plt.show()

Nevermind, I found NonUniformImage after some digging. The working code is attached below if anyone is interested.

If anyone knows the answer, I'm still curious if the clipping behavior for pcolormesh is a bug.

Thanks,
-Tony

#~~~~ example code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage

def nonuniform_imshow(x, y, C, **kwargs):
    """Plot image with nonuniform pixel spacing.
    
    This function is a convenience method for calling image.NonUniformImage.
    """
    ax = plt.gca()
    im = NonUniformImage(ax, **kwargs)
    im.set_data(x, y, C)
    ax.images.append(im)
    return im

def plot_filled_curve(x, y, c):
    """Plot curve filled with linear color gradient
    
    Parameters

···

On Dec 2, 2009, at 3:53 PM, Tony S Yu wrote:

Hi,

I'm having hard time understanding some of the differences between functions used to plot color patches (not sure what to call them).

I'm trying to fill a curve with a nonuniform color patch (like fill or fill_between but the color in the patch varies). I've attached code that almost does what I want; my question concerns the color patch (which is created by the call to plt.pcolor in the code below). I'd like to have nonuniform grid spacing in the color values, and also shading (i.e. interpolation of color between points). Here's what I understand:

pcolor: allows nonuniform grid spacing, but it doesn't do shading.

imshow: allows color shading, but requires uniform spacing

pcolormesh: allows color interpolation and nonuniform grid spacing

pcolormesh seems like the ideal candidate, but when I replace pcolor with pcolormesh (code commented out below pcolor call), the path doesn't get clipped by set_clip_path (but no errors are raised); in other words, the color shading fills the entire plot area. Is this a bug?

Is there a way of making this plot work that I've overlooked?

Thanks!
-Tony

    ----------
    x, y : arrays
        points describing curve
    c : array
        color values underneath curve. Must match the lengths of `x` and `y`.
    """
    # add end points so that fill extends to the x-axis
    x_closed = np.concatenate([x[:1], x, x[-1:]])
    y_closed = np.concatenate([[0], y, [0]])
    # fill between doesn't work here b/c it returns a PolyCollection, plus it
    # adds the lower half of the plot by adding a Rect with a border
    patch, = plt.fill(x_closed, y_closed, facecolor='none')
    im = nonuniform_imshow(x, [0, y.max()], np.vstack((c, c)),
                           interpolation='bilinear', cmap=plt.cm.gray)
    im.set_clip_path(patch)

if __name__ == '__main__':
    line = np.linspace(0, 1, 6)
    x = np.hstack((line, [1, 2]))
    y = np.hstack((line**2, [1, 1]))
    c = np.hstack((line, [0, 0]))
    plot_filled_curve(x, y, c)
    plt.show()

line 1486 of _backend_agg.cpp says

  /* TODO: Support clip paths */

So, it seems that, apparently, clipping with arbitrary path has not
been implemented yet for gouraud shading (pcolormesh will be properly
clipped if shading is not used).
I hope Michael pick this up some time soon.

Meanwhile, you may open a feature request ticket on this.

Regards,

-JJ

···

On Wed, Dec 2, 2009 at 5:13 PM, Tony S Yu <tsyu80@...287...> wrote:

On Dec 2, 2009, at 3:53 PM, Tony S Yu wrote:

Hi,

I'm having hard time understanding some of the differences between functions used to plot color patches (not sure what to call them).

I'm trying to fill a curve with a nonuniform color patch (like fill or fill_between but the color in the patch varies). I've attached code that almost does what I want; my question concerns the color patch (which is created by the call to plt.pcolor in the code below). I'd like to have nonuniform grid spacing in the color values, and also shading (i.e. interpolation of color between points). Here's what I understand:

pcolor: allows nonuniform grid spacing, but it doesn't do shading.

imshow: allows color shading, but requires uniform spacing

pcolormesh: allows color interpolation and nonuniform grid spacing

pcolormesh seems like the ideal candidate, but when I replace pcolor with pcolormesh (code commented out below pcolor call), the path doesn't get clipped by set_clip_path (but no errors are raised); in other words, the color shading fills the entire plot area. Is this a bug?

Is there a way of making this plot work that I've overlooked?

Thanks!
-Tony

Nevermind, I found NonUniformImage after some digging. The working code is attached below if anyone is interested.

If anyone knows the answer, I'm still curious if the clipping behavior for pcolormesh is a bug.

Thanks,
-Tony

#~~~~ example code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage

def nonuniform_imshow(x, y, C, **kwargs):
"""Plot image with nonuniform pixel spacing.

This function is a convenience method for calling image.NonUniformImage.
"""
ax = plt.gca()
im = NonUniformImage(ax, **kwargs)
im.set_data(x, y, C)
ax.images.append(im)
return im

def plot_filled_curve(x, y, c):
"""Plot curve filled with linear color gradient

Parameters
----------
x, y : arrays
points describing curve
c : array
color values underneath curve. Must match the lengths of `x` and `y`.
"""
# add end points so that fill extends to the x-axis
x_closed = np.concatenate([x[:1], x, x[-1:]])
y_closed = np.concatenate([[0], y, [0]])
# fill between doesn't work here b/c it returns a PolyCollection, plus it
# adds the lower half of the plot by adding a Rect with a border
patch, = plt.fill(x_closed, y_closed, facecolor='none')
im = nonuniform_imshow(x, [0, y.max()], np.vstack((c, c)),
interpolation='bilinear', cmap=plt.cm.gray)
im.set_clip_path(patch)

if __name__ == '__main__':
line = np.linspace(0, 1, 6)
x = np.hstack((line, [1, 2]))
y = np.hstack((line**2, [1, 1]))
c = np.hstack((line, [0, 0]))
plot_filled_curve(x, y, c)
plt.show()

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing.
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hey Jae-Joon,

Thanks for digging into this. Feature request added.

By the way, I was looking through the feature requests and noticed an open feature request (ID: 2112292) that was satisfied by the addition of spines in the last major release. Just an FYI, if someone with privileges wants to close the request.

Thanks,
-Tony

···

On Dec 3, 2009, at 3:58 PM, Jae-Joon Lee wrote:

line 1486 of _backend_agg.cpp says

/* TODO: Support clip paths */

So, it seems that, apparently, clipping with arbitrary path has not
been implemented yet for gouraud shading (pcolormesh will be properly
clipped if shading is not used).
I hope Michael pick this up some time soon.

Meanwhile, you may open a feature request ticket on this.

Regards,

-JJ