|
From: Jon Roadley-B. <jon...@gm...> - 2012-09-06 09:30:50
|
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?
|
|
From: Mogliii <mo...@gm...> - 2012-09-06 10:54:46
Attachments:
LaTeX_latex_subscript.png
MPL_latex_subscript.png
|
Hi,
I am preparing figures with the following matplotlib preamble:
plt.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman'],
'monospace':['Computer Modern Typewriter']})
params = {'backend': 'ps',
'text.latex.preamble': [r"\usepackage{upgreek}",
r"\usepackage[nice]{units}"],
'axes.labelsize': 12,
'text.fontsize': 12,
'legend.fontsize': 8,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'text.usetex': True,
'figure.figsize': fig_size,
'axes.unicode_minus': True}
plt.rcParams.update(params)
But when using subscript and superscript in math mode the sizes end up
different than in my LaTeX document (my .cls is using "book" as base class).
Attached are two screenshots of it rendered from matplotlib and from
Latex (sorry for the different sizes). The "mean" is clearly of
different size in relation to the "d".
Is there an option to specify a .cls file in rcParams that should be
used for rendering?
I tried:
text.latex.preamble': [r"\documentclass[twoside]{mycls}",
r"\usepackage{upgreek}",
r"\usepackage[nice]{units}"],
but then I get the error:
! LaTeX Error: Two \documentclass or \documentstyle commands.
How to overwrite default matplotlib one? my matplotlibrc has a commented
preamble.
See also my question on tex.stackexchange.com:
http://tex.stackexchange.com/questions/70400/subscript-size-different-in-latex-and-matplotlib
Many thanks
|
|
From: Benjamin R. <ben...@ou...> - 2012-09-06 13:20:42
|
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 |
|
From: Benjamin R. <ben...@ou...> - 2012-09-06 13:50:19
|
On Thu, Sep 6, 2012 at 6:54 AM, Mogliii <mo...@gm...> wrote:
> Hi,
>
> I am preparing figures with the following matplotlib preamble:
>
> plt.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman'],
> 'monospace':['Computer Modern Typewriter']})
> params = {'backend': 'ps',
> 'text.latex.preamble': [r"\usepackage{upgreek}",
> r"\usepackage[nice]{units}"],
> 'axes.labelsize': 12,
> 'text.fontsize': 12,
> 'legend.fontsize': 8,
> 'xtick.labelsize': 10,
> 'ytick.labelsize': 10,
> 'text.usetex': True,
> 'figure.figsize': fig_size,
> 'axes.unicode_minus': True}
> plt.rcParams.update(params)
>
>
> But when using subscript and superscript in math mode the sizes end up
> different than in my LaTeX document (my .cls is using "book" as base class).
>
> Attached are two screenshots of it rendered from matplotlib and from Latex
> (sorry for the different sizes). The "mean" is clearly of different size in
> relation to the "d".
>
>
> Is there an option to specify a .cls file in rcParams that should be used
> for rendering?
> I tried:
> text.latex.preamble': [r"\documentclass[twoside]{mycls}",
> r"\usepackage{upgreek}",
> r"\usepackage[nice]{units}"],
> but then I get the error:
> ! LaTeX Error: Two \documentclass or \documentstyle commands.
>
> How to overwrite default matplotlib one? my matplotlibrc has a commented
> preamble.
>
>
>
> See also my question on tex.stackexchange.com:
> http://tex.stackexchange.com/questions/70400/subscript-size-different-in-latex-and-matplotlib
>
>
>
> Many thanks
>
>
This might have been fixed at one point. Which version of matplotlib are
you using?
Ben Root
|
|
From: mogliii <mo...@gm...> - 2012-09-06 14:59:02
|
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 06/09/2012 14:49, Benjamin Root
wrote:<br>
</div>
<blockquote
cite="mid:CAN...@ma..."
type="cite"><br>
<br>
<div class="gmail_quote">On Thu, Sep 6, 2012 at 6:54 AM, Mogliii <span
dir="ltr"><<a moz-do-not-send="true"
href="mailto:mo...@gm..." target="_blank">mo...@gm...</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF"> Hi,<br>
<br>
I am preparing figures with the following matplotlib
preamble:<br>
<br>
plt.rc('font', **{'family':'serif', 'serif':['Computer
Modern Roman'],<br>
'monospace':['Computer Modern
Typewriter']})<br>
params = {'backend': 'ps',<br>
'text.latex.preamble': [r"\usepackage{upgreek}",<br>
r"\usepackage[nice]{units}"],<br>
'axes.labelsize': 12,<br>
'text.fontsize': 12,<br>
'legend.fontsize': 8,<br>
'xtick.labelsize': 10,<br>
'ytick.labelsize': 10,<br>
'text.usetex': True,<br>
'figure.figsize': fig_size,<br>
'axes.unicode_minus': True}<br>
plt.rcParams.update(params)<br>
<br>
<br>
But when using subscript and superscript in math mode the
sizes end up different than in my LaTeX document (my .cls is
using "book" as base class).<br>
<br>
Attached are two screenshots of it rendered from matplotlib
and from Latex (sorry for the different sizes). The "mean"
is clearly of different size in relation to the "d".<br>
<br>
<br>
Is there an option to specify a .cls file in rcParams that
should be used for rendering? <br>
I tried: <br>
text.latex.preamble': [r"\documentclass[twoside]{mycls}",<br>
r"\usepackage{upgreek}",<br>
r"\usepackage[nice]{units}"],<br>
but then I get the error:<br>
! LaTeX Error: Two \documentclass or \documentstyle
commands.<br>
<br>
How to overwrite default matplotlib one? my matplotlibrc has
a commented preamble.<br>
<br>
<br>
<br>
See also my question on <a moz-do-not-send="true"
href="http://tex.stackexchange.com" target="_blank">tex.stackexchange.com</a>:
<a moz-do-not-send="true"
href="http://tex.stackexchange.com/questions/70400/subscript-size-different-in-latex-and-matplotlib"
target="_blank">http://tex.stackexchange.com/questions/70400/subscript-size-different-in-latex-and-matplotlib</a><br>
<br>
<br>
<br>
Many thanks<br>
</div>
<br>
</blockquote>
<div><br>
This might have been fixed at one point. Which version of
matplotlib are you using?<br>
<br>
Ben Root<br>
<br>
</div>
</div>
</blockquote>
<br>
I found the solution. My LaTeX file uses the package amsmath, which
also loads amstext. And that is invoked when using \text{} or
\textnormal{} inside a formula.<br>
<br>
So the solution in matplotlib is to add the following to the
preamble:<br>
'text.latex.preamble': [r"\usepackage{upgreek}",<br>
r"\usepackage[nice]{units}",<br>
<span class="comment-copy"><code>r"\usepackage{amstext}"</code></span>],<br>
<br>
I was using matplotlib 1.1, now updated to 1.1.1, but that did not
affect it. Also, I am not using the built-in LaTeX typesetting.<br>
<br>
<br>
</body>
</html>
|
|
From: Jon Roadley-B. <jon...@gm...> - 2012-09-06 13:56:13
|
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 |
|
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()
|
|
From: Benjamin R. <ben...@ou...> - 2012-09-06 20:48:18
|
On Thu, Sep 6, 2012 at 4:37 PM, Jon Roadley-Battin < jon...@gm...> wrote: > 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 > > Yup, that would do it. Because of how you structured your try/except statement in onpress(), the canvas is only re-drawn upon any unsuccessful attempts of removing the text. So, after you save, you still see the text objects there because the canvas hasn't been redrawn. Doing a test for "event.inaxes" is the correct way to go there (and making sure you do the draw at the right situation). Cheers! Ben Root |
|
From: Jon Roadley-B. <jon...@gm...> - 2012-09-06 21:06:35
|
Thats the plan. The problem is clicking on a plot calls both onpick and onpress (hence the additional fig.canvas.draw() to work around this). ill put a flag in and do an if/else on that as well |