用渐变填充楔形

时间:2019-07-10 15:30:37

标签: python matplotlib annotations annotate

是否可以用颜色渐变填充ax.annotate wedge?例如而不是红色,从蓝色到绿色的颜色渐变。从xy开始,到xytext结束。

import matplotlib.pyplot as plt

plt.figure(1, figsize=(3,3))

ax = plt.subplot()

ax.annotate("Wedge",
            xy=(5, 5), xycoords='data',
            xytext=(30, 20), textcoords='data',
            arrowprops=dict(arrowstyle=("wedge, tail_width=2,shrink_factor=0.5"),
                            connectionstyle="arc3,rad=0.1",fc="#ff0000",ec="#ff0000",shrinkA=10,),
            )

ax.set_xlim(0, 50)
ax.set_ylim(0, 50) 
plt.show()

pyth

更新

我在another post中遇到了一个不错的解决方案,我发现也许可以用path = Path([[1,1],[2,1],[2,3],[1,1]])来替换ax.annotate ...中的Path,但是它不起作用。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import PathPatch

fig = plt.figure() 
ax = fig.add_subplot(111, aspect='equal') 
path = ax.annotate("Wedge",
            xy=(5, 5), xycoords='data',
            xytext=(10, 40), textcoords='data',
            arrowprops=dict(arrowstyle=("wedge, tail_width=2,shrink_factor=0.5"),
                            connectionstyle="arc3,rad=0.2",fc="#ff0000",ec="#ff0000",shrinkA=10,),
            )
patch = PathPatch(path, facecolor='none')
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, 50)
ax.set_ylim(0, 50) 
plt.show()

0 个答案:

没有答案