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()