Jun-02-2017, 10:00 PM
Hi guys,
I wrote a function that plot rectangles filled with colors defined by a colormap. Here's a part of the function:
Now I need to plot a legend to this colormap, but I don't know how to do it.
I appreciate the help.
I wrote a function that plot rectangles filled with colors defined by a colormap. Here's a part of the function:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib import cm
def tabularS(X,dx,p,q,prism):
vm = 3*max(p[:,3])
unit = dx/len(X)
norm = plt.Normalize()
colors = plt.cm.rainbow(norm(p[:,0]))
for i in range(prism):
xl = p[i,2] - unit
cor = colors[i]
rect = Rectangle((xl,p[i,3]),unit,vm-p[i,3],color=cor)
ax.add_patch(rect)
plt.xlim(X[0], X[-1])
plt.ylim([0, vm])
ax = plt.gca()
ax.invert_yaxis()
plt.title('Prisms')
plt.xlabel('Position(m)')
plt.ylabel('Depth(m)')
plt.show()
returnThe function works and I can plot a figure composed of rectangles filled with the colors defined by the colormap.Now I need to plot a legend to this colormap, but I don't know how to do it.
I appreciate the help.
