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
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users