

- #Matlab subplot legend being cut off how to#
- #Matlab subplot legend being cut off code#
- #Matlab subplot legend being cut off windows#
Plot windows support standard operations such as axis labels, zoom, export to JPG, etc. These plots can be created from within the results visualizer ("plot in new window" button), or from a script. Simulation results can be visualized using 1D lines, a 2D surface, and 3D plots.
#Matlab subplot legend being cut off how to#
Has to be done BEFORE figure resizing!Īx.This page explains how to use the data visualizer and figure windows to view your simulation data. ,int(round(heightTot*dpi))+2*canvasPadding+canvasBottomįig.canvas._(ipythonWindowSize) IpythonWindowSize = ''.format(int(round(widthTot*dpi))+2*canvasPadding WidthTot = widthAx+padLeft+padRight+padLegend PadLegend = ax.get_legend().get_frame().get_width() / dpi PadRight = ( 1 - ax.get_position().x0 - ax.get_position().width ) * fig.get_size_inches() PadTop = ( 1 - ax.get_position().y0 - ax.get_position().height ) * fig.get_size_inches()

PadBottom = ax.get_position().y0 * fig.get_size_inches() PadLeft = ax.get_position().x0 * fig.get_size_inches() Leg = ax.legend(loc='upper left', bbox_to_anchor=(1.02, 1), borderaxespad=0) # Change padding and margins, insert legend Hope it is of use to somebody.Ĭode: #=ĭf = \n\nasedf\nasdf\nadfs','human\n','ressource\n']Īx.bar(posX+(iiItem-1)*width, df.values(), width, color=colmap, label=df.keys())Īx.set(xticks=posX+width*(0.5*numItems), xticklabels=df) You can define the size of the axis and paddings are considered accordingly (including legend and tickmarks). Convert it to a more scalable image format and use GIMP to edit if necessary, or just crop with the LaTeX viewport option when including graphics. Just play with this until it looks right for you. Here figsize=(M,N) sets the figure window to be M inches by N inches. To lgd = ax.legend(loc=9, bbox_to_anchor=(0.5,-0.02))Īnd it shows up fine on my screen (a 24-inch CRT monitor). I simply changed the line: fig = plt.figure(1) Some combination of setting the figsize and using the subplots_adjust(bottom=.) should produce a quality plot for you. Then play with the offset in the legend bbox_to_anchor part of the legend command, to get the legend box where you want it. Use the subplots_adjust() function to move the bottom of the subplot up: fig.subplots_adjust(bottom=0.2) # <- Change the 0.02 to work for your plot.
#Matlab subplot legend being cut off code#
I have now fixed the issues and tidied the arbitrary text to show how these are also considered within the bbox_extra_artists algorithm.Īdded: I found something that should do the trick right away, but the rest of the code below also offers an alternative. Despite this, numerous edits recently have insisted on putting these in, often in ways that led to the code raising an error. The intent of this question was to completely avoid the use of arbitrary coordinate placements of arbitrary text as was the traditional solution to these problems. Text = ax.text(-0.2,1.05, "Aribitrary text", transform=ax.transAxes)įig.savefig('samplefigure', bbox_extra_artists=(lgd,text), bbox_inches='tight') Lgd = ax.legend(handles, labels, loc='upper center', bbox_to_anchor=(0.5,-0.1)) Handles, labels = ax.get_legend_handles_labels() This did in fact resize the figure box as desired. This is apparently similar to calling tight_layout, but instead you allow savefig to consider extra artists in the calculation. #Note that the bbox_extra_artists must be an iterable The code I am looking for is adjusting the savefig call to: fig.savefig('samplefigure', bbox_extra_artists=(lgd,), bbox_inches='tight') Sorry EMS, but I actually just got another response from the matplotlib mailling list (Thanks goes out to Benjamin Root). I have the (only slightly) longer version of this code on pastebin Is there a historical reason? Is Matlab equally poor on this matter? Notice how the final label 'Inverse tan' is actually outside the figure box (and looks badly cutoff - not publication quality!)įinally, I've been told that this is normal behaviour in R and LaTeX, so I'm a little confused why this is so difficult in python. import matplotlib.pyplot as pltĪx.plot(x, np.arctan(x), label='Inverse tan')

What I would like to be able to do is dynamically expand the size of the figure box to accommodate the expanding figure legend. The example of a complex legend in the documentation demonstrates the need for this because the legend in their plot actually completely obscures multiple data points. Shrinking the axes, however, is not an ideal solution because it makes the data smaller making it actually more difficult to interpret particularly when its complex and there are lots of things going on. It seems that the answers in these questions have the luxury of being able to fiddle with the exact shrinking of the axis so that the legend fits. Matplotlib savefig with a legend outside the plot I'm familiar with the following questions:
