0

Is it possible to fill an ax.annotate wedge with a color gradient? e.g. instead of red a color gradient from blue to green. Starting at the xy and ending at 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

Update

I came across a nice solution in another post and I figured maybe I can simply replace the Path in path = Path([[1,1],[2,1],[2,3],[1,1]]) with my ax.annotate ... but it is not working.

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()
Stücke
  • 868
  • 3
  • 14
  • 41
  • 1
    Did you try anything yet? If `a` is the annotation, `a.arrow_patch.get_path()` should give you the path of the wedge. – ImportanceOfBeingErnest Jul 10 '19 at 15:47
  • Good morning (again) and as always thank you for your answer! I already came across potential sources for a solution e.g. [https://stackoverflow.com/questions/42063542/mathplotlib-draw-triangle-with-gradient-fill], [https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/gradient_bar.html], [https://stackoverflow.com/questions/29321835/is-it-possible-to-get-color-gradients-under-curve-in-matplotlib] and [https://stackoverflow.com/questions/8500700/how-to-plot-a-gradient-color-line-in-matplotlib]. Your hint `a.arrow_patch.get_path()` may already help. I'll take a closer look again later on. – Stücke Jul 11 '19 at 06:36
  • I suppose the closest what I found to that I am trying to do is this: https://stackoverflow.com/questions/42063542/mathplotlib-draw-triangle-with-gradient-fill Apparently, (knowing from your comment) I can get the path of the wedge with `a.arrow_patch.get_path()`. At the same time the other code says `path = Path([[0,0],[0,1],[1,0],[0,0]])`. I figured I can put my `ax.annotate("Wedge"...`, behind `path =` but that is not working. When I just add `a.arrow_patch.get_path()` for testing I receive the name error `name 'a' is not defined`. – Stücke Jul 11 '19 at 13:02
  • 1
    Could you post code inside the question, instead of in the comments where it's too condensed to make any sense? – ImportanceOfBeingErnest Jul 11 '19 at 13:05
  • Sure, I will do that. Thanks. (I am sure it's terribly wrong) – Stücke Jul 11 '19 at 13:23
  • 1
    Yeah, you ignored my first comment it seems. However, that will at the end not work out either, because the path needs to be transformed at draw time. I might find a solution later on, but it may also turn out to be too complex. – ImportanceOfBeingErnest Jul 11 '19 at 18:48
  • 1
    So I came to the comclusion that it would be possible to calculate the path manually by diving deep into the FancyArrowPatch drawing procedure; but it would involve using private attributes and be some 20 lines of code. So I think this is much too complex and instead I'd recommend you use a path that is defined in data coordinates directly. – ImportanceOfBeingErnest Jul 11 '19 at 20:33
  • Another option would be to use this example as a basis https://stackoverflow.com/questions/42063542/mathplotlib-draw-triangle-with-gradient-fill and to draw the wedge myself with Bezier curves https://matplotlib.org/users/path_tutorial.html In fact, that's what I did so far but the curves I draw don't look as nice as the wedge. – Stücke Jul 12 '19 at 06:54

0 Answers0