import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.patches import Polygon
np.random.seed(1977)
def main():
for _ in range(5):
gradient_fill(*generate_data(100))
plt.show()
def generate_data(num):
x = np.linspace(0, 100, num)
y = np.random.normal(0, 1, num).cumsum()
return x, y
def gradient_fill(x, y, fill_color=None, ax=None, **kwargs):
"""
Plot a line with a linear alpha gradient filled beneath it.
Parameters
----------
x, y : array-like
The data values of the line.
fill_color : a matplotlib color specifier (string, tuple) or None
The color for the fill. If None, the color of the line will be used.
ax : a matplotlib Axes instance
The axes to plot on. If None, the current pyplot axes will be used.
Additional arguments are passed on to matplotlib's ``plot`` function.
Returns
-------
line : a Line2D instance
The line plotted.
im : an AxesImage instance
The transparent gradient clipped to just the area beneath the curve.
"""
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, **kwargs)
if fill_color is None:
fill_color = line.get_color()
zorder = line.get_zorder()
alpha = line.get_alpha()
alpha = 1.0 if alpha is None else alpha
z = np.empty((100, 1, 4), dtype=float)
rgb = mcolors.colorConverter.to_rgb(fill_color)
z[:,:,:3] = rgb
z[:,:,-1] = np.linspace(0, alpha, 100)[:,None]
xmin, xmax, ymin, ymax = x.min(), x.max(), y.min(), y.max()
im = ax.imshow(z, aspect='auto', extent=[xmin, xmax, ymin, ymax],
origin='lower', zorder=zorder)
xy = np.column_stack([x, y])
xy = np.vstack([[xmin, ymin], xy, [xmax, ymin], [xmin, ymin]])
clip_path = Polygon(xy, facecolor='none', edgecolor='none', closed=True)
ax.add_patch(clip_path)
im.set_clip_path(clip_path)
ax.autoscale(True)
return line, im
main()
3条答案
按热度按时间13z8s7eq1#
对于类似的问题,以前有一些答案(例如https://stackoverflow.com/a/22081678/325565),但他们推荐了一种次优方法。
前面的大多数答案都建议在
pcolormesh
填充上绘制一个白色多边形。这并不理想,原因有两个:1.轴的背景不能是透明的,因为有一个填充的多边形覆盖它
pcolormesh
的绘制速度相当慢,而且插值不平滑。这是一个触摸更多的工作,但有一个方法,绘制速度更快,并提供了更好的视觉效果:设置与
imshow
绘制的图像的剪辑路径。例如:
c2e8gylq2#
zfunc
。* 他的方法为许多渐变/模糊/阴影效果打开了大门。例如,为了使线条具有均匀模糊的底面,您可以使用PIL构建一个alpha层,该层靠近线条为1,靠近底部边缘为0。收益率
wmomyfyw3#
我试过了:
结果是
当然,通过改变
feel_between
函数的范围,梯度可以下降到0。