python - My matplotlib.pyplot legend is being cut off -
i'm attempting create plot legend side of using matplotlib. can see plot being created, image bounds not allow entire legend displayed.
lines = [] ax = plt.subplot(111) filename in args: lines.append(plt.plot(y_axis, x_axis, colors[colorcycle], linestyle='steps-pre', label=filename)) ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
this produces:
as pointed adam, need make space on side of graph. if want fine tune needed space, may want @ add_axes method of matplotlib.pyplot.artist.
below rapid example:
import matplotlib.pyplot plt import numpy np # data x = np.arange(0, 10, 0.1) y1 = np.sin(x) y2 = np.cos(x) # plot of data fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.6, 0.75]) ax.plot(x, y1,'-k', lw=2, label='black sin(x)') ax.plot(x, y2,'-r', lw=2, label='red cos(x)') ax.set_xlabel('x', size=22) ax.set_ylabel('y', size=22) ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.show()
and resulting image:
Comments
Post a Comment