[matplotlib-devel] Pgfplots (TikZ) backend: implementation strategy

2010-01-08 Thread Nico Schlömer
Hi,

I'm looking into replacing my MATLAB(R) plotting routines by something
slicker, and quite naturally found matplotlib. It has all the
capabilities that I would need, except that I can't yet transform my
plots into TikZ.
For MATLAB(R), I used this rather elaborate script
.

Well, I thought I can just go ahead and start writing and equivalent
backend; the documentation is really nice and clear (quite unlike
MATLAB's!) so it was no big problem to get into the concepts of the
backend. I played around a little and thought about how I could
implement this and that, and some questions arose which can maybe best
answered here.

For the sake of clarity, let me just give a snippet of Pgfplots (TikZ)
code that I would like the backend to produce

= *snip* =
[...]
\begin{semilogyaxis}
[axis on top,
xtick={2,4,6,8,10,12,14,16},
ytick={1e-15,1e-10,1e-05,1,10},
xmin=0.00e+00,xmax=1.70e+01,
ymin=1.00e-15,ymax=1.00e+05,
xmajorgrids,
ymajorgrids,
title={$\norm{F(\psi)}_2$},
xlabel={$k$},
width=\figurewidth,
height=\figureheight,
scale only axis
]
% Line plot
\addplot [color=red,only marks,mark=*,mark options={solid,fill=red}]
coordinates{
 (1.00e+00,3.206000e+01) (2.00e+00,3.86e+01)
(3.00e+00,1.421000e+03) (4.00e+00,4.143000e+02)
(5.00e+00,1.445000e+02) (6.00e+00,3.775000e+01)
(7.00e+00,7.455000e+00) (8.00e+00,7.228000e-01)
(9.00e+00,2.275000e-02) (1.00e+01,4.953000e-05)
(1.10e+01,9.718000e-10) (1.20e+01,5.534000e-07)
(1.30e+01,4.217000e-11) (1.40e+01,3.93e-03)
(1.50e+01,2.067000e-07) (1.60e+01,7.231000e-12)
};
\end{semilogyaxis}
[...]
= *snap* =

This yields a purely marker plot (without lines) on a coordinate
system where the y-coordinate is log-scaled. You see that the code is
rather semantic and can easily be edited (which is think is the whole
point of Pgfplots as opposed to pure TikZ).

Now, if for a matplotlib plot I had query functions for the the axes
ranges, ticks, grids, titles, data values, and so on and so forth, it
would be a more or less complicated parsing of those and creating what
we see above.

However, it seems to me that the concept of backends is different.
draw_path() would be called to plot the graph itself, the coordinate
axes, and basically everything that resembles a line, giving its
outline (color, shape, markers, start and end points) but *no*
semantic information, that is, whether the path is part of an axis, an
arrow or whatever.
Is that correct?

Considering this, what do matplotlob masterbrains :) think would be a
good way to extract Pgfplots code out of a matplotlib figure? Is a
backend feasible at all? Would a function as "matplotlib2tikz(
myFigure )" be more advisable, making use of all sorts of query
functions? (Such as myFigure.axes.get_xlim() -- Does something like
that exist at all?)

Cheers,
Nico

--
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] Pgfplots (TikZ) backend: implementation strategy

2010-01-08 Thread Michael Droettboom
Nico Schlömer wrote:
> Hi,
>
> I'm looking into replacing my MATLAB(R) plotting routines by something
> slicker, and quite naturally found matplotlib. It has all the
> capabilities that I would need, except that I can't yet transform my
> plots into TikZ.
> For MATLAB(R), I used this rather elaborate script
> .
>
> Well, I thought I can just go ahead and start writing and equivalent
> backend; the documentation is really nice and clear (quite unlike
> MATLAB's!) so it was no big problem to get into the concepts of the
> backend. I played around a little and thought about how I could
> implement this and that, and some questions arose which can maybe best
> answered here.
>
> For the sake of clarity, let me just give a snippet of Pgfplots (TikZ)
> code that I would like the backend to produce
>
> = *snip* =
> [...]
> \begin{semilogyaxis}
> [axis on top,
> xtick={2,4,6,8,10,12,14,16},
> ytick={1e-15,1e-10,1e-05,1,10},
> xmin=0.00e+00,xmax=1.70e+01,
> ymin=1.00e-15,ymax=1.00e+05,
> xmajorgrids,
> ymajorgrids,
> title={$\norm{F(\psi)}_2$},
> xlabel={$k$},
> width=\figurewidth,
> height=\figureheight,
> scale only axis
> ]
> % Line plot
> \addplot [color=red,only marks,mark=*,mark options={solid,fill=red}]
> coordinates{
>  (1.00e+00,3.206000e+01) (2.00e+00,3.86e+01)
> (3.00e+00,1.421000e+03) (4.00e+00,4.143000e+02)
> (5.00e+00,1.445000e+02) (6.00e+00,3.775000e+01)
> (7.00e+00,7.455000e+00) (8.00e+00,7.228000e-01)
> (9.00e+00,2.275000e-02) (1.00e+01,4.953000e-05)
> (1.10e+01,9.718000e-10) (1.20e+01,5.534000e-07)
> (1.30e+01,4.217000e-11) (1.40e+01,3.93e-03)
> (1.50e+01,2.067000e-07) (1.60e+01,7.231000e-12)
> };
> \end{semilogyaxis}
> [...]
> = *snap* =
>
> This yields a purely marker plot (without lines) on a coordinate
> system where the y-coordinate is log-scaled. You see that the code is
> rather semantic and can easily be edited (which is think is the whole
> point of Pgfplots as opposed to pure TikZ).
>
> Now, if for a matplotlib plot I had query functions for the the axes
> ranges, ticks, grids, titles, data values, and so on and so forth, it
> would be a more or less complicated parsing of those and creating what
> we see above.
>
> However, it seems to me that the concept of backends is different.
> draw_path() would be called to plot the graph itself, the coordinate
> axes, and basically everything that resembles a line, giving its
> outline (color, shape, markers, start and end points) but *no*
> semantic information, that is, whether the path is part of an axis, an
> arrow or whatever.
> Is that correct?
>   
Yes -- the backend interface is designed for "dumber" output formats 
that don't know anything about the semantics of plots -- PDF, PS, SVG 
etc. are basically "draw a line here, put some text there" sorts of 
things.  Of course, you could probably write a TikZ backend without much 
semantic detail fairly easily.
> Considering this, what do matplotlob masterbrains :) think would be a
> good way to extract Pgfplots code out of a matplotlib figure? Is a
> backend feasible at all? Would a function as "matplotlib2tikz(
> myFigure )" be more advisable, making use of all sorts of query
> functions? (Such as myFigure.axes.get_xlim() -- Does something like
> that exist at all?)
>   
That sounds more feasible -- however, keep in mind that anything without 
a public interface (a get_* method) is free to change in a future 
version of matplotlib.  I suspect (though haven't thought it all the way 
through) that you may be required to dig into private members to get 
everything you need.  I also suspect that this will be a lot of work to 
support all of the kinds of plots that matplotlib supports, and problems 
are likely to arise when the semantics of Pgfplot and/or TikZ don't mesh 
well.  For example, does Pgf plot support the same set of nonlinear 
transformations as matplotlib?

Other people have suggested TikZ support in the past, and I, personally, 
haven't been very convinced of the usefulness of such a thing.  It's 
just as easy to include a PDF or EPS in LaTeX, and the mathtext and/or 
usetex functionality goes a long way to making the plots blend in nicely 
-- though it does take some care to control the size of the plots to 
make the font sizes match, it's by no means impossible.  Jae-Joon's work 
with fancy arrow styles brings in a lot of the finesse features of TikZ 
to matplotlib.  What do you see as the use cases for a TikZ backend?

Mike

-- 
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA


--
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app develo

Re: [matplotlib-devel] Pgfplots (TikZ) backend: implementation strategy

2010-01-08 Thread Nico Schlömer
> That sounds more feasible -- however, keep in mind that anything without a
> public interface (a get_* method) is free to change in a future version of
> matplotlib.  I suspect (though haven't thought it all the way through) that
> you may be required to dig into private members to get everything you need.

That may be possible, particularly I suppose for the actual plot data
(x, y(, z) values). I'm going to have to look into what
matplotlib.get* can offer me. Is there a list with all the methods
available?

>  I also suspect that this will be a lot of work to support all of the kinds
> of plots that matplotlib supports, and problems are likely to arise when the
> semantics of Pgfplot and/or TikZ don't mesh well.  For example, does Pgf
> plot support the same set of nonlinear transformations as matplotlib?

matplotlib and Pgfplots certainly have a different feature set, but
for a couple of common simple plots (e.g., 2D x, y data, log-scaled
axes, lines, markers, and so forth) it should be possible to write a
very basic extendible translator.

> Other people have suggested TikZ support in the past, and I, personally,
> haven't been very convinced of the usefulness of such a thing.

Well, yeah, I guess it is possible to export PDF files in such a way
that it looks sort of nice if you watch out what you do with the font
sizes, and font family in general. Over the years I've become somewhat
tired of that whole tinkering with the fonts, and eventually I'd be
about 80% happy with the result I got. If you don't want to do that
every time, and you want 100%, TikZ/Pgfplots can come in quite handy;
especially if a simple module can be written rather quickly. If you
have a simple 2D plot and you would like to get it cleanly into your
LaTeX file, why not use TikZ if it's available?

Cheers,
Nico

--
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] Pgfplots (TikZ) backend: implementation strategy

2010-01-08 Thread Jae-Joon Lee
Matplotlib, by design, needs to know the exact dimension (height,
width and descent) of texts that the backend will produce (before the
output is produced), and I wonder if that's going to be possible with
TikZ.
Unless you can solve this problem, I don't think tikz backend will be feasible.

Some form of simple translator like "matplotlib2tikz(myFigure)" should
certainly be possible. But things like legends and annotations will be
very tricky, if not impossible.

In my opinion, there is no reason that matplotlib can make user 100%
happy, because matplotlib itself actually runs tex/latex in usetex
mode. If TikZ can do that, I believe matplotlib also can do. If you're
not 100% happy with the output, please report why it is so. If you're
using matplotlib with usetex mode, and somehow the fonts does not
match with other texts in your document, it only means that there is a
room for improvement in matplotlib. At least to me.

Regards,

-JJ

--
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel