|
From: Andreas H. <li...@hi...> - 2012-07-27 08:17:37
|
> On Thu, Jul 26, 2012 at 06:05:39PM +0200, Andreas Hilboll wrote: >> > Hi Andreas, >> > >> > 2012/7/26 Andreas Hilboll <li...@hi...>: >> >> Hi, >> >> >> >> I would like to create a figure which only contains a legend, and no >> >> axes >> >> at all. I would like to manually assign the colors. I found this >> here: >> >> >> >> http://stackoverflow.com/a/3302666 >> >> >> >> but from there on, I'd like to remove the axes, and put the legend >> into >> >> three columns. >> > >> > If the plot attached it's fine for you it's easy: >> > >> > import matplotlib.pyplot as plt >> > ax = plt.subplot() #create the axes >> > ax.set_axis_off() #turn off the axis >> > .... #do patches and labels >> > ax.legend(patches, labels, ...) #legend alone in the figure >> > plt.show() >> > >> > Cheers, >> > Francesco >> >> That's really easy :) I could live with this solution, applying some >> external tool like pdfcrop to the result. Of course, it would be nicer >> if >> the PDF's page size would be exactly that of the legend (plus some >> margin), so that I wouldn't have to resort to external tools ... >> >> Any ideas? >> > > How about > > plt.savefig('roflcakes.png', bbox_inches='tight', pad_inches=0.1) > > Since the other artists are invisible, that should crop to just your > legend. I'm assuming matplotlib updates the BoundingBox such that it > doesn't include invisible artists. Thanks for your help, guys! I wasn't totally happy with the 'tight' bounding box, as the margins to the legend were not uniform on all four sides. So I did indeed return to a subprocess.call(['pdfcrop', ...]). Cheers, A. |