One possible solution is to add Line2D objects for use in the legend, also known as using proxy artists. To do this, you need to add from matplotlib.lines import Line2D to your script, and then you can replace this code:
ax.legend(loc = 2) plt.colorbar(p) print p.get_label()
with this:
circ1 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.4, markersize=10, markerfacecolor="red") circ2 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.3, markersize=10, markerfacecolor="blue") circ3 = Line2D([0], [0], linestyle="none", marker="o", alpha=0.4, markersize=10, markerfacecolor="yellow") plt.legend((circ1, circ2, circ3), ("Cat 1", "Cat 2", "Cat 3"), numpoints=1, loc="best")

hooy source share