Skip to content Skip to sidebar Skip to footer

Gradient Fill Under Matplotlib Graphs

I've gotten a lot of information from these two posts on SO about putting a gradient fill below a curve in matplotlib. I tried the same thing plotting multiple plots on one axis an

Solution 1:

The problem is in your zfunc. You say you want to fade your alphas to zero by multiplying them with np.linspace(0,10,n).

try:

zalpha[:n] *= np.linspace(0, 1, n)[:, None]

then it works for me...


Solution 2:

It is a different approach than what you have taken, but perhaps you can use an image with varying intensity and a colormap using an alpha value like this:

import numpy as np
import scipy as sc

import matplotlib.pyplot as plt

x = np.linspace (0, 10, 100)
y = .5 * x + 4

plt.figure ()


yres = 100
ymax = np.max (y)
ymin = 0 
yy = np.linspace (ymin, ymax, yres)

fill_n = 10

xres = len(x)

# gradient image
gI = np.zeros ((yres, xres))
for xi,xx in enumerate(x):
  ym = y[xi]

  # find elment closest to curve
  ya = np.argmin (np.abs(yy - ym))

  gI[ya-fill_n:ya, xi] = np.linspace (0, 1, fill_n)

# make alpha cmap out of gray map
bb = np.linspace (0, 1, fill_n)
kk = []
for b in bb:
  kk.append ((b, b, b))

bb = tuple (kk) 
gr = { 'blue' : bb,
       'red' : bb,
       'green': bb,
       'alpha': bb }

plt.register_cmap (name = 'GrayAlpha', data = gr)

gI = np.flipud (gI)
plt.imshow (gI, vmin = 0, vmax = 1, cmap = 'GrayAlpha', interpolation = 'bicubic')
plt.show ()

enter image description here


Post a Comment for "Gradient Fill Under Matplotlib Graphs"