## Re: [Matplotlib-users] Legend in X coordinates

 Re: [Matplotlib-users] Legend in X coordinates From: Christopher Brown - 2008-12-31 22:48:47 ```Hi Jae-Joon, Your simpler suggestion of drawing the fig twice is just fine for my needs. Thank you. JL> The exact location of the legend is known at drawing time. Thus, the JL> location of the text needs to be calculated at the drawing. I may JL> help you with this but it is quite tricky and you need some JL> knowledge on internals of the mpl. JL> JL> Or, you may simply draw the figure twice (with same renderer), where JL> the first one is just to calculate the location of the text. JL> Here is an example. JL> JL> ------------------ JL> import matplotlib.pyplot as plt JL> plt.plot([1, 3, 2], label="test2") JL> l = plt.legend() JL> plt.draw() # the location of the legend is now known JL> JL> ax = gca() JL> bbox = l.legendPatch.get_bbox().inverse_transformed(ax.transAxes) # JL> bbox in axes coordinate. JL> x = bbox.x0+bbox.width/2. # center JL> JL> ax.text(x, 0, "test", va="bottom", ha="center", JL> transform=ax.transAxes) plt.draw() JL> --------------------- JL> JL> Note that if you want save the figure, you need to save it with a JL> same dpi, or save the figure twice as above. JL> JL> -JJ -- Christopher Brown, Ph.D. Department of Speech and Hearing Science Arizona State University ```

 [Matplotlib-users] Legend in X coordinates From: Christopher Brown - 2008-12-30 22:16:14 ```Hi, How can I get the position, in x coordinates/units, of the legend? Specifically, I'd like to get the center of the legend box, because I want to add some text that is centered-aligned with the legend. Thanks. -- Christopher Brown, Ph.D. Department of Speech and Hearing Science Arizona State University ```
 Re: [Matplotlib-users] Legend in X coordinates From: Jae-Joon Lee - 2008-12-31 07:33:43 ```The exact location of the legend is known at drawing time. Thus, the location of the text needs to be calculated at the drawing. I may help you with this but it is quite tricky and you need some knowledge on internals of the mpl. Or, you may simply draw the figure twice (with same renderer), where the first one is just to calculate the location of the text. Here is an example. ------------------ import matplotlib.pyplot as plt plt.plot([1, 3, 2], label="test2") l = plt.legend() plt.draw() # the location of the legend is now known ax = gca() bbox = l.legendPatch.get_bbox().inverse_transformed(ax.transAxes) # bbox in axes coordinate. x = bbox.x0+bbox.width/2. # center ax.text(x, 0, "test", va="bottom", ha="center", transform=ax.transAxes) plt.draw() --------------------- Note that if you want save the figure, you need to save it with a same dpi, or save the figure twice as above. -JJ On Tue, Dec 30, 2008 at 5:16 PM, Christopher Brown wrote: > Hi, > > How can I get the position, in x coordinates/units, of the legend? > Specifically, I'd like to get the center of the legend box, because I > want to add some text that is centered-aligned with the legend. Thanks. > > -- > Christopher Brown, Ph.D. > Department of Speech and Hearing Science > Arizona State University > > ------------------------------------------------------------------------------ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > ```
 Re: [Matplotlib-users] Legend in X coordinates From: Christopher Brown - 2008-12-31 22:48:47 ```Hi Jae-Joon, Your simpler suggestion of drawing the fig twice is just fine for my needs. Thank you. JL> The exact location of the legend is known at drawing time. Thus, the JL> location of the text needs to be calculated at the drawing. I may JL> help you with this but it is quite tricky and you need some JL> knowledge on internals of the mpl. JL> JL> Or, you may simply draw the figure twice (with same renderer), where JL> the first one is just to calculate the location of the text. JL> Here is an example. JL> JL> ------------------ JL> import matplotlib.pyplot as plt JL> plt.plot([1, 3, 2], label="test2") JL> l = plt.legend() JL> plt.draw() # the location of the legend is now known JL> JL> ax = gca() JL> bbox = l.legendPatch.get_bbox().inverse_transformed(ax.transAxes) # JL> bbox in axes coordinate. JL> x = bbox.x0+bbox.width/2. # center JL> JL> ax.text(x, 0, "test", va="bottom", ha="center", JL> transform=ax.transAxes) plt.draw() JL> --------------------- JL> JL> Note that if you want save the figure, you need to save it with a JL> same dpi, or save the figure twice as above. JL> JL> -JJ -- Christopher Brown, Ph.D. Department of Speech and Hearing Science Arizona State University ```