![]() 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. 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. Therefore, when specifying bbox_to_anchor, always specify loc as well. In many cases pie charts are not the best way to convey information. Step 2: Making sure, a pie chart is needed. ![]() The default for loc is loc="best" which gives unpredictable results when the bbox_to_anchor argument is used. In many cases no legend is needed at all and the information can be inferred by the context or the color directly: If indeed the plot cannot live without a legend, proceed to step 2. 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. Update: The code and graphs have been updated to reflect the introduction of group functionality in the legend.I am aware of the bbox_to_anchor keyword and this thread, which very helpfully suggests how to manually place the legend: ![]() Similarly for y0 and y1 along the height of the plot. ![]() Note that the x and y coordinates here are relative, meaning that x0 is the left most point in the plot and x1 is the rightmost point in the plot. Text='name %d' %(row+2), showarrow=False))įig.update_layout(legend_tracegroupgap=50) to set the legend's lower left corner to the specified (x, y) position. You suggested annotations there as an alternative, so I added them to your code. When I looked into it, the legend for each subplot seems to correspond from this information. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |