switching output formats within a function

But there is no reason you shouldn't be able to create a PS

    >> figure or a GD figure to save. I've been meaning to add a 'PS'
    >> extension checker in the savefig command that would enable you
    >> to save to PS from any backend.
    >>
    >> Is this primarily what you need to switch backends for?

    > Yes that's the sweet spot.

    > Presumably the Save button on the GTK/WX GUI just calls
    > savefig()? In which case you'd be able to save postscript
    > from there too, which would be popular too I think.

I've made some changes to the GTK backend that enable save to a ps
figure, either by calling

  savefig('somefile.ps')

or using a file with the ps extension from the save figure dialog.

It's not too pretty internally but it works (more or less). Consider
this a preliminary functional implementation with known bugs that will
be hammered out later.

The problem in implementing this is that the AxisText instances (axis
and tick labels, title) are backend dependent. As Jeremy noted when
he did the wx backend, this is different than the way other objects
(lines, patches) are handled. With some refactoring this can be made
more elegant.

The other problem is that the default fonts are different between the
backends, so you'll get a lot of warnings like "Falling back on
default font". This is another problem we need to clear up -- ie, we
need a set of shared fontnames between backends.

Finally, a 'gotcha' that you need to watch out for is that text
references in scripts will be destroyed by calling a postscript
savefig (because of the way text instance conversions are handled).

So if you did

  ax = subplot(111)
  plot(something)
  labels = ax.get_xticklabels()
  savefig('somefile.ps')
  set(labels, 'color', 'r')
  savefig('somefile.png')

The color change would not take effect because the text references
have been changed. Moral of story: do not change figure text
properties after calling savefig for a ps figure with text instances
obtained before the savefig call.

Other than that it should work. Let me know. I've updated CVS but be
forewarned: CVS mirrors sometime take a while to update.

JDH

That's great.

It's interesting to read your discussion of backend switching issues. It's
something that gnuplot deals with very poorly indeed, which is what
motivated me to seek out matplotlib. I.e. in gnuplot you can freely switch
backends, but the line styles are set differently (and sometimes very
awkwardly: X resources for instance!) for each backend.

Anyway, I tried out what's in CVS. As you say, it mostly works. I had
trouble though with my (rather complicated) 2x2 subplot script. The worst
problem is that when I use the Save button to save as .ps, the output is
sized too large to fit on the page: only the lower left subplot is fully
visible.

There are various other layout problems, which I managed to reproduce in
the attached hacked version of subplot_demo.py, but unfortunately I
couldn't reproduce the problem above outside of my script.

Other problems:

* when saving from the GTK window into a .ps file, the lines are not
clipped by the edge of the plot area: see the top left plot in the
attached code

* xaxis and yaxis labels often land on top of adjacent subplot titles and
plot areas in savefig('blah.ps') output (I've had trouble with this in the
-dPS output too)

Also, I tried to save as file.eps, but the save dialog complained and only
accepts .ps. Presumably this is an easy fix?

Cheers,
Matthew.

subplot_demo.py (1.22 KB)

···

On Wed, 7 Jan 2004, John Hunter wrote:

I've made some changes to the GTK backend that enable save to a ps
figure, either by calling

  savefig('somefile.ps')

or using a file with the ps extension from the save figure dialog.

It's not too pretty internally but it works (more or less). Consider
this a preliminary functional implementation with known bugs that will
be hammered out later.

The problem in implementing this is that the AxisText instances (axis
and tick labels, title) are backend dependent. As Jeremy noted when
he did the wx backend, this is different than the way other objects
(lines, patches) are handled. With some refactoring this can be made
more elegant.

The other problem is that the default fonts are different between the
backends, so you'll get a lot of warnings like "Falling back on
default font". This is another problem we need to clear up -- ie, we
need a set of shared fontnames between backends.

Finally, a 'gotcha' that you need to watch out for is that text
references in scripts will be destroyed by calling a postscript
savefig (because of the way text instance conversions are handled).

So if you did

  ax = subplot(111)
  plot(something)
  labels = ax.get_xticklabels()
  savefig('somefile.ps')
  set(labels, 'color', 'r')
  savefig('somefile.png')

The color change would not take effect because the text references
have been changed. Moral of story: do not change figure text
properties after calling savefig for a ps figure with text instances
obtained before the savefig call.

Other than that it should work. Let me know. I've updated CVS but be
forewarned: CVS mirrors sometime take a while to update.

JDH