SourceForge logo
SourceForge logo
Menu

matplotlib-users

From: Christopher B. <c-...@as...> - 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
From: Jae-Joon L. <lee...@gm...> - 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 <c-...@as...> 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
> Mat...@li...
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
From: Christopher B. <c-...@as...> - 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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /