<https://github.com/matplotlib/matplotlib/commit/44d3cf8cb883a85c325459165b6ed120219c7451>Below
 
is the new function that was recently added to the development version 
(that Benjamin was referring to).  It should work to include it in your 
own code and should work for the past few releases of matplotlib.  It 
gets around the need for any external tools for cropping etc since the 
tight bounding box is calculated within matplotlib.

Mike

def math_to_image(s, filename_or_obj, prop=None, dpi=None, format=None):
     """
     Given a math expression, renders it in a closely-clipped bounding
     box to an image file.

     *s*
        A math expression.  The math portion should be enclosed in
        dollar signs.

     *filename_or_obj*
        A filepath or writable file-like object to write the image data
        to.

     *prop*
        If provided, a FontProperties() object describing the size and
        style of the text.

     *dpi*
        Override the output dpi, otherwise use the default associated
        with the output format.

     *format*
        The output format, eg. 'svg', 'pdf', 'ps' or 'png'.  If not
        provided, will be deduced from the filename.
     """
     from matplotlib import figure
     # backend_agg supports all of the core output formats
     from matplotlib.backends import backend_agg

     if prop is None:
         prop = FontProperties()

     parser = MathTextParser('path')
     width, height, depth, _, _ = parser.parse(s, dpi=72, prop=prop)

     fig = figure.Figure(figsize=(width / 72.0, height / 72.0))
     fig.text(0, depth/height, s, fontproperties=prop)
     backend_agg.FigureCanvasAgg(fig)
     fig.savefig(filename_or_obj, dpi=dpi, format=format)

     return depth


On 05/16/2011 12:36 PM, Benjamin Root wrote:
>
>
> On Mon, May 16, 2011 at 11:22 AM, Johannes Radinger <jradin...@gmx.at 
> <mailto:jradin...@gmx.at>> wrote:
>
>
>     -------- Original-Nachricht --------
>     > Datum: Mon, 16 May 2011 10:59:34 -0500
>     > Von: Benjamin Root <ben.r...@ou.edu <mailto:ben.r...@ou.edu>>
>     > An: Johannes Radinger <jradin...@gmx.at <mailto:jradin...@gmx.at>>
>     > CC: matplotlib-users@lists.sourceforge.net
>     <mailto:matplotlib-users@lists.sourceforge.net>
>     > Betreff: Re: [Matplotlib-users] use matplotlib to produce
>     mathathematical expression only
>
>     > On Mon, May 16, 2011 at 10:21 AM, Johannes Radinger
>     > <jradin...@gmx.at <mailto:jradin...@gmx.at>>wrote:
>     >
>     > >
>     > > -------- Original-Nachricht --------
>     > > > Datum: Mon, 16 May 2011 09:49:24 -0500
>     > > > Von: Benjamin Root <ben.r...@ou.edu <mailto:ben.r...@ou.edu>>
>     > > > An: "matplotlib-users@lists.sourceforge.net
>     <mailto:matplotlib-users@lists.sourceforge.net>" <
>     > > matplotlib-users@lists.sourceforge.net
>     <mailto:matplotlib-users@lists.sourceforge.net>>
>     > > > Betreff: Re: [Matplotlib-users] [SciPy-User] use matplotlib
>     to produce
>     > >      mathathematical expression only
>     > >
>     > > > On Monday, May 16, 2011, Johannes Radinger <jradin...@gmx.at
>     <mailto:jradin...@gmx.at>> wrote:
>     > > > >
>     > > > > -------- Original-Nachricht --------
>     > > > >> Datum: Mon, 16 May 2011 08:28:49 -0500
>     > > > >> Von: Robert Kern <robert.k...@gmail.com
>     <mailto:robert.k...@gmail.com>>
>     > > > >> An: SciPy Users List <scipy-u...@scipy.org
>     <mailto:scipy-u...@scipy.org>>
>     > > > >> CC: matplotlib-users@lists.sourceforge.net
>     <mailto:matplotlib-users@lists.sourceforge.net>
>     > > > >> Betreff: Re: [Matplotlib-users] [SciPy-User] use
>     matplotlib to
>     > produce
>     > > >        mathathematical expression only
>     > > > >
>     > > > >> On Mon, May 16, 2011 at 08:21, Johannes Radinger
>     <jradin...@gmx.at <mailto:jradin...@gmx.at>>
>     > > > wrote:
>     > > > >> > Hello,
>     > > > >> >
>     > > > >> > I want to produce a eps file of following mathematical
>     > expression:
>     > > > >> >
>     > > > >>
>     > > >
>     > >
>     >
>     
> r'$F(x)=p*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}+(1-p)*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}$'
>     > > > >> >
>     > > > >> > is it possible to somehow missuse matplotlib for that
>     to produce
>     > > only
>     > > > >> the function without any other plot things? Or is there a
>     better
>     > > python
>     > > > >> library within scipy? I don't want to install the
>     complete latex
>     > > > libraries just
>     > > > >> for producing this single eps file.
>     > > > >>
>     > > > >> Check out mathtex. It is matplotlib's TeX parsing engine and
>     > renderer
>     > > > >> broken out into a separate library:
>     > > > >>
>     > > > >> http://code.google.com/p/mathtex/
>     > > > >
>     > > > > I also thought about mathtex but don't know how to use my
>     > mathematical
>     > > > expression without a plot of axis etc. any suggestions? I
>     just want to
>     > > have
>     > > > the formated math expression as eps and I don't know how to
>     do it,
>     > still
>     > > > after reading in the matplotlib-manual.
>     > > > >
>     > > > > /johannes
>     > > > >
>     > > > >
>     > > > >>
>     > > > >> Also, please send matplotlib questions just to the
>     matplotlib list.
>     > > > >> Thanks.
>     > > > >>
>     > > > >> --
>     > > > >> Robert Kern
>     > > > >>
>     > > > >> "I have come to believe that the whole world is an enigma, a
>     > harmless
>     > > > >> enigma that is made terrible by our own mad attempt to
>     interpret it
>     > as
>     > > > >> though it had an underlying truth."
>     > > > >>   -- Umberto Eco
>     > > > >>
>     > > > >>
>     > > >
>     > >
>     >
>     
> ------------------------------------------------------------------------------
>     > > > >> Achieve unprecedented app performance and reliability
>     > > > >> What every C/C++ and Fortran developer should know.
>     > > > >> Learn how Intel has extended the reach of its
>     next-generation tools
>     > > > >> to help boost performance applications - inlcuding clusters.
>     > > > >> http://p.sf.net/sfu/intel-dev2devmay
>     > > > >> _______________________________________________
>     > > > >> Matplotlib-users mailing list
>     > > > >> Matplotlib-users@lists.sourceforge.net
>     <mailto:Matplotlib-users@lists.sourceforge.net>
>     > > > >> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>     > > > >
>     > > > > --
>     > > > > NEU: FreePhone - kostenlos mobil telefonieren und surfen!
>     > > > > Jetzt informieren: http://www.gmx.net/de/go/freephone
>     > > > > _______________________________________________
>     > > > > SciPy-User mailing list
>     > > > > scipy-u...@scipy.org <mailto:scipy-u...@scipy.org>
>     > > > > http://mail.scipy.org/mailman/listinfo/scipy-user
>     > > > >
>     > > >
>     > > > We have added a new feature to do just that in the
>     development branch,
>     > > > but it should be fairly trivial to do with existing releases of
>     > > > matplotlib.  Just create a figure object and use its
>     figtitle to hold
>     > > > the expression and then save the figure.
>     > >
>     > > It might be trivial but how to remove the axis/plot then and
>     crop the
>     > > extend of the eps?
>     > >
>     > > I tried:
>     > >
>     > > plt.figure()
>     > >
>     >
>     
> plt.title(r'$F(x)=p*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}+(1-p)*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}$',
>     > > fontsize=20)
>     > > plt.show()
>     > >
>     > > /j
>     > >
>     > >
>     > Use figtext instead.  I did the following and it looked fine to me:
>     >
>     > plt.figure()
>     > plt.figtext(0.1, 0.5,
>     >
>     
> r'$F(x)=p*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}+(1-p)*\frac{1}{s1\sqrt{2\pi}}*e^{-\frac{1}{2}*(\frac{x-m}{s1})}$',
>     > fontsize=20)
>     > plt.show()
>     >
>
>
>     thats working nearly perfect, I would just need to crop the
>     display extend resp. the white space from the eps around...any
>     option/idea?
>
>
>     /j
>
>
> Try setting bbox_inches='tight' in the call to savefig.  With 
> bbox_inches='tight', you can then specify the 'pad_inches' kwarg to 
> indicate how much padding to put around the tight bounding box.  This 
> should work, however some older version of matplotlib might not check 
> the figure text objects for calculating the tightest bounding box.  In 
> that case, the way that I typically autocrop my eps files is to 
> convert it into a pdf file and use pdfcrop and then convert it back to 
> eps (assuming you have a standard linux install).  Here is the chain 
> of commands I typically use on my Fedora machine:
>
> epstopdf mathtext.eps --outfile=mathtext.temp.pdf
> pdfcrop --margins '15 2 15 2' --clip mathtext.temp.pdf 
> mathtext.cropped.pdf
> pdftops mathtext.cropped.pdf mathtext.cropped.eps
>
> You can adjust margins to your tastes, and the names of the files are 
> fairly arbitrary.
>
> I hope that helps!
> Ben Root
>
>
> ------------------------------------------------------------------------------
> Achieve unprecedented app performance and reliability
> What every C/C++ and Fortran developer should know.
> Learn how Intel has extended the reach of its next-generation tools
> to help boost performance applications - inlcuding clusters.
> http://p.sf.net/sfu/intel-dev2devmay
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>    


-- 
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA


------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to