开发者

Draggable legend with multiple axes problem

I’m wondering if anyone has a solution for a problem I’m having when using a draggable legend in conjunction with multiple axes.

I’m using gridspec and figure (matplotlib 1.0.1, qt4 backend) to create 2 axes – no pyplot. When the legend is dragged from one axes to the other and released, its draggability ceases to work.

If a portion of the legend is outside the boundaries of the axis, the legend can be picked up by clicking on that portion, but not if 开发者_JAVA百科the mouse is clicked on any area of the legend inside the axis.

I tried Adam Fraser’s original code but the legend would slip under the 2nd axes in my app.

My app is really quite large - too large to show here, so to illustrate the problem, I’ve modded some code from the Matplotlib Legend Guide (uses pyplot) as follows:

from matplotlib.pyplot import *

subplot(211)
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
leg=legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
           ncol=2, mode="expand", borderaxespad=0.)
leg.draggable(state=True, use_blit=True)

subplot(223)
plot([1,2,3], label="test1")
plot([3,2,1], label="test2")
leg=legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
leg.draggable(state=True, use_blit=True)

show()

It produces a plot with a couple of axes and two legends.

Run the code and drag the legends around to see the effect (if you don't have any problems, it must be something I ate and I apologize).

I suspect a solution may be fairly low level, but who knows, maybe it’s a simple fix?

Thanks for any help.


I reproduced the problem. The part of the legend that enters the alien figure becomes unclickable, as if the figure would be catching the mouse position events and not resending them to other handlers.

The minimal code that reproduces the problem (to experiment with ipython -pylab) is:

In [16]: f211 = plt.subplot(211)
In [17]: f212 = plt.subplot(212)
In [18]: p1 = plot([1,2], label="test1")
In [20]: leg = legend(loc=(0.5,0.5))
In [21]: d = leg.draggable(state=True, use_blit=True)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜