![]() We can put the legend ouside by resizing the box and puting the legend relative to that:Īx.set_position()Īx.legend(loc= 'upper center', bbox_to_anchor=( 1.45, 0.8), shadow= True, ncol= 1)Īx.legend(loc= 'upper center', bbox_to_anchor=( 1.45, 0. To put the legend on top, change the bbox_to_anchor values:Īx.legend(loc= 'upper center', bbox_to_anchor=( 0.5, 1.00), shadow= True, ncol= 2)Īx.legend(loc= 'upper center', bbox_to_anchor=( 0.5, 1.00), shadow= True, ncol= 2) ![]() Take into account that we set the number of columns two ncol=2 and set a shadow. To place the legend on the bottom, change the legend() call to:Īx.legend(loc= 'upper center', bbox_to_anchor=( 0.5, - 0.05), shadow= True, ncol= 2) Y2 = Īx.plot(x, y2, label= '$y2 = other numbers') To place the legend inside, simply call legend(): Python3 import numpy as np import matplotlib.pyplot as plt x np.linspace (-3, 3, 1000) y1 np.sin (x) y2 np.cos (x) fig, ax plt. Method 2: Using setvisible () Example 1: By using ax.getlegend ().setvisible (False) method, legend can be removed from figure in matplotlib. Data Visualization with Matplotlib and Python The first subplot will still have a legend.In this article we will show you some examples of legends using matplotlib. The legend() method adds the legend to the plot. Legends can be placed in various positions: A legend can be placed inside or outside the chart and the position can be moved. It may not make too much sense to use data coordinates, but since you asked for it this would be done via bbox_transform=ax.transData.Matplotlib has native support for legends. If may make sense to use figure coordinates ax.legend(bbox_to_anchor=(1,0), loc="lower right", bbox_transform=fig.transFigure) If you want to specify the legend position in other coordinates than axes coordinates, you can do so by using the bbox_transform argument. See especially this answer for a detailed explanation and the question What does a 4-element tuple argument for 'bbox_to_anchor' mean in matplotlib?. Because the legend extents outside the bounding box, the loc parameter may be interpreted as "which corner of the legend shall be placed at position given by the 2-tuple bbox_to_anchor argument". If a different bounding box is specified, is is usually sufficient to use the first two values, which give (x0, y0) of the bounding box.īelow is an example where the bounding box is set to position (0.6,0.5) (green dot) and different loc parameters are tested. The default for bbox_to_anchor is (0,0,1,1), which is a bounding box over the complete axes. ![]() However the legends always overlap with the plots and I can't figure out how to add a spacing between legend and plot or how to make them not overlap automatically in the first place. ![]() Therefore, when specifying bbox_to_anchor, always specify loc as well. I have subplots with legends that contain a lot of elements. The default for loc is loc="best" which gives unpredictable results when the bbox_to_anchor argument is used. The loc parameter specifies in which corner of the bounding box the legend is placed. But maybe I am getting something else wrong? 102) in order to make the legend appear on coordinates (0.02, 0.83). I think what confuses me is that the legend does not actually start at 0.82, and indeed for my larger plot (with 5 subplots of this type), I need to use legend coordinates bbox_to_anchor=(0.04, 1.15, 1. 102),labelspacing=0.1, handlelength=0.1, handletextpad=0.1,frameon=False, ncol=4, columnspacing=0.7) Is this possible?Įdit: A small example is here: import numpy as nĪot(,n.array(), label='test1')Īxarr.errorbar(,n.array(),n.array(), label='test2')Īot(,n.array())Īxarr.errorbar(,n.array(),n.array())Īxarr.legend(bbox_to_anchor=(0.04, 0.82, 1. However, I'd like to use the coordinates of my x- and y-axis in the graph to specify the legend position (inside the plot), as I might need to move the figure into a large figure with a different axis environment, and I don't want to manually play around with those coordinates every time I do this. I am aware of the bbox_to_anchor keyword and this thread, which very helpfully suggests how to manually place the legend: ![]()
0 Comments
Leave a Reply. |