用渐变填充路径

时间:2019-07-04 15:12:28

标签: python matplotlib

我使用matplotlib在绘图上绘制了一条路径,我想用渐变填充形状。

enter image description here

我遇到了this个示例:

Z, Z2 = np.meshgrid(np.linspace(0,1), np.linspace(0,1))
im = plt.imshow(Z-Z2, interpolation='bilinear', cmap=plt.cm.RdYlGn,
                origin='lower', extent=[0, 1, 0, 1],
                clip_path=patch, clip_on=True)
im.set_clip_path(patch)

  [1]: https://stackoverflow.com/questions/42063542/mathplotlib-draw-triangle-with-gradient-fill

我将我认为相关的代码片段复制并粘贴到我的代码中:

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
import numpy as np

verts = [
    (200, 10), # 
    (125,110), # Control Point - half way c
    (50,110), # Control Point
    (50,110), # 
    (50,120), #
    (25,100), # 
    (50,80),  # 
    (50,90),  #
    (50,90),  # Control Point
    (125,90), # Controil Point half way x
    (200,10), # 
    (0., 0.), # 
    ]

codes = [Path.MOVETO,
         Path.CURVE4,
         Path.CURVE4,
         Path.CURVE4,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.CURVE4,
         Path.CURVE4,
         Path.CURVE4,
         Path.CLOSEPOLY,
         ]

path = Path(verts, codes)

fig = plt.figure()
ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='orange', lw=0, alpha=0.5)
ax.add_patch(patch)

Z, Z2 = np.meshgrid(np.linspace(0,1), np.linspace(0,1))
im = plt.imshow(Z-Z2, interpolation='bilinear', cmap=plt.cm.RdYlGn,
                origin='lower', extent=[0, 1, 0, 1],
                clip_path=patch, clip_on=True)
im.set_clip_path(patch)

ax.set_xlim(-0,225)
ax.set_ylim(-0,125)

plt.show()

但是,箭头仍然是橙色。

如何用颜色1到颜色2(例如红色到蓝色)的渐变填充绘图上的箭头?

0 个答案:

没有答案