|
From: Jon Roadley-B. <jon...@gm...> - 2012-09-06 20:37:34
|
On 6 September 2012 14:55, Jon Roadley-Battin
<jon...@gm...> wrote:
> On 6 September 2012 14:20, Benjamin Root <ben...@ou...> wrote:
>>
>>
>> On Thu, Sep 6, 2012 at 5:29 AM, Jon Roadley-Battin
>> <jon...@gm...> wrote:
>>>
>>> Good morning,
>>>
>>> I have an odd problem with saving plot images via the navigation bar
>>> (unsure if it is unique to the navigation bar) if I have added custom
>>> text.
>>>
>>> BACKGROUND.
>>> I have a python gui which is used to connect to some hardware as a
>>> diagnosis tool. Its a pyGTK program and on one ui tab there is an
>>> embeded matplotlib plot.
>>> Now some of the signals I plot rather than being a waveform is more of
>>> a collection of flags (16bit but thats a by and by)
>>> eg:
>>>
>>>
>>>
>>>
>>> fault
>>> 0 = fault1
>>> 0 = fault2
>>> 1 = fault3
>>> 0 = fault4
>>>
>>>
>>>
>>> so for this I can plot this and it will show 2 (possibly changing w.r.t.
>>> time).
>>> I then hook in via a onpick event such that if I click on a plot I
>>> essentially do this:
>>>
>>> self.figtxt =
>>> self.fig.text(0.79,0.92,'\n'.join(txt),va='top',
>>> bbox=dict(boxstyle='round', facecolor='white',alpha=0.7))
>>> self.plot_marker =
>>> self.plot_area.plot(xdata,ydata,'x',color=event.artist.get_color(),ms=7)
>>>
>>> ie I put a cross where someone clicked (for indication) and I also
>>> create a texxbox which lists human readable version of what bits are
>>> set.
>>> Great, really helpful in debugging.
>>>
>>> The issue is if you click on the save image icon on the navigation
>>> toolbar it saves the waveform and legend BUT not the additional
>>> content (the cross and the textbox).
>>>
>>>
>>> any idea as to how todo this?
>>>
>>
>> What you are describing should work as expected. Is it possible that you
>> could make a simple, self-contained version (hopefully it doesn't need to be
>> embedded to reproduce the problem)? Maybe a modification of one of the
>> existing examples in the online docs might be able to reproduce your issue?
>>
>> Ben Root
>>
>
> Good afternoon,
> I agree it should work and the examples i have run allow this so this
> is why I am looking over my specifics.
> The only thing I can see is the examples call to add txt via an axes
> entity, I have been doing it via a figure entity.
>
> I am going to re-write to draw on the axis (last time I did this it
> didn't display hence going for a figure).
>
> If this doesn't work I shall strip the code down to a minimum example
> demonstrating what I am describing
Well I have narrowed down exactly what causes it and produced a
stripped down piece of code that reproduces the effect.
It is related to events. I use mpl_connect to connect two functions
#1 onpick - when a plot is picked annotate plot (in practice IF it is
a binary array then annotate)
#2 onpress - click anywhere else and it clears said annotation.
Works well (probably not the best way todo this, but it works). Thing
is it seems my onpress method is called when I click on the save on
the toolbar
I guess I should add an if statement ( if event.inaxis ...) so that
the remove method isn't called outside of the axes area
#!/usr/bin/env python
from matplotlib.pyplot import figure, show
import numpy as np
fig = figure()
text = None
x = np.arange(0,10,0.2)
y = np.arange(10,20,0.2)
ax = fig.add_subplot(111)
ax.plot(x,y,picker=5)
def onpick(event):
global text
x = event.artist.get_xdata()[event.ind][0]
y = event.artist.get_ydata()[event.ind][0]
text = ax.annotate('foo',xy=(x,y),xytext=(0.8,
0.9),textcoords='axes fraction')
fig.canvas.draw()
def onpress(event):
global text
try:
text.remove()
except:
fig.canvas.draw()
fig.canvas.mpl_connect('button_press_event', onpress)
fig.canvas.mpl_connect('pick_event', onpick)
show()
|