import matplotlib.pyplot as pltimport numpy as np
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):ax.plot(x, i * x, label='$y = %ix$' % i)
ax.legend()
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):ax.plot(x, i * x, label='$y = %ix$' % i)
ax.legend(bbox_to_anchor=(1.1, 1.05))
plt.show()
同样,使图例更水平和/或把它放在图的顶部(我也打开圆角和一个简单的阴影):
import matplotlib.pyplot as pltimport numpy as np
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):line, = ax.plot(x, i * x, label='$y = %ix$'%i)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),ncol=3, fancybox=True, shadow=True)plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):ax.plot(x, i * x, label='$y = %ix$'%i)
# Shrink current axis by 20%box = ax.get_position()ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axisax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
以类似的方式,垂直缩小情节,并在底部放置一个水平图例:
import matplotlib.pyplot as pltimport numpy as np
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):line, = ax.plot(x, i * x, label='$y = %ix$'%i)
# Shrink current axis's height by 10% on the bottombox = ax.get_position()ax.set_position([box.x0, box.y0 + box.height * 0.1,box.width, box.height * 0.9])
# Put a legend below current axisax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),fancybox=True, shadow=True, ncol=5)
plt.show()
import matplotlib.pylab as pltimport numpy as np#define the figure and get an axes instancefig = plt.figure()ax = fig.add_subplot(111)#plot the datax = np.arange(-5, 6)ax.plot(x, x*x, label='y = x^2')ax.plot(x, x*x*x, label='y = x^3')ax.legend().draggable()plt.show()
import matplotlib.pyplot as pltimport numpy as np
plt.ion()
x = np.arange(10)
fig = plt.figure()ax = plt.subplot(111)
for i in xrange(5):ax.plot(x, i * x, label='$y = %ix$'%i)
# Put a legend to the right of the current axisleg = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.draw()
# Get the ax dimensions.box = ax.get_position()xlocs = (box.x0,box.x1)ylocs = (box.y0,box.y1)
# Get the figure size in inches and the dpi.w, h = fig.get_size_inches()dpi = fig.get_dpi()
# Get the legend size, calculate new window width and change the figure size.legWidth = leg.get_window_extent().widthwinWidthNew = w*dpi+legWidthfig.set_size_inches(winWidthNew/dpi,h)
# Adjust the window size to fit the figure.mgr = plt.get_current_fig_manager()mgr.window.wm_geometry("%ix%i"%(winWidthNew,mgr.window.winfo_height()))
# Rescale the ax to keep its original size.factor = w*dpi/winWidthNewx0 = xlocs[0]*factorx1 = xlocs[1]*factorwidth = box.width*factorax.set_position([x0,ylocs[0],x1-x0,ylocs[1]-ylocs[0]])
plt.draw()
fig = pylab.figure()ax = fig.add_subplot(111)ax.plot(x, y, label=label, color=color)# Make the legend transparent:ax.legend(loc=2, fontsize=10, fancybox=True).get_frame().set_alpha(0.5)# Make a transparent text boxax.text(0.02, 0.02, yourstring, verticalalignment='bottom',horizontalalignment='left',fontsize=10,bbox={'facecolor':'white', 'alpha':0.6, 'pad':10},transform=self.ax.transAxes)
figurex = 0:.2:12;plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));hleg = legend('First','Second','Third',...'Location','NorthEastOutside')% Make the text of the legend italic and color it brownset(hleg,'FontAngle','italic','TextColor',[.3,.2,.1])