From: Benjamin R. <ben...@ou...> - 2013-05-28 15:19:18
|
On Tue, May 28, 2013 at 3:50 AM, Nils Wagner <ni...@go...> wrote: > Hi all, > > I would like to add a legend to a 3D plot. > However, the legend ist not visible in the example given below. > Am I missing something ? > > Nils > > import numpy as np > from mpl_toolkits.mplot3d import Axes3D > import matplotlib.pyplot as plt > import random > > def fun(x, y): > return x**2 + y > def fun1(x, y): > return 10+x**2 + y > > fig = plt.figure() > ax = fig.add_subplot(111, projection='3d') > x = y = np.arange(-3.0, 3.0, 0.05) > X, Y = np.meshgrid(x, y) > zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))]) > Z = zs.reshape(X.shape) > zs = np.array([fun1(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))]) > Z1 = zs.reshape(X.shape) > > ax.plot_surface(X, Y, Z, label=r'$f$') > ax.plot_surface(X, Y, Z1,label=r'$f_1$') > > ax.set_xlabel('X Label') > ax.set_ylabel('Y Label') > ax.set_zlabel('Z Label') > ax.legend() > plt.show() > > plot_surface() does not have any default representation in a legend. I have never thought about it before, but what would one want to display for it? It is kind of like wanting a legend entry for an imshow() or pcolor() display. If you have something in mind to display, I would suggest using proxy artists. Cheers! Ben Root |