I have to draw a triangle in Python using mathplotlib.
This is how it should eventually look like:
My objective is, once drawn the triangle, to plot some points on it.
At the moment I can draw the triangle just fine:
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.add_patch(Polygon([[0,0],[0,1],[1,0]], closed=True,fill=True))
ax.set_xlim((0,1))
ax.set_ylim((0,1))
plt.show()
But I can only fill it with a solid color. How do I add a gradient like shown in the picture?
Can some one help me?
There is an example on the matplotlib page showing how to use a clip path for an image.
Adapting this to your case would give this:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import PathPatch
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
path = Path([[0,0],[0,1],[1,0],[0,0]])
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,1))
ax.set_ylim((0,1))
plt.show()