Hi Logan,

Something like this *may* work:

   import yt

   ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')

   plot = yt.SlicePlot(ds, 2, 'density')



   fig = plot.plots['gas','density'].figure

   with yt.funcs.matplotlib_style_context():
       fig.savefig('my_plot.pdf', transparent=True)

I say *may* because when I try locally I can't even make a regular matplotlib plot that doesn't use yt at all save as a transparent PDF. It works with png though. I have no idea why transparent PDFs aren't working for me, perhaps it's related to https://github.com/matplotlib/matplotlib/issues/3841.

Note that when you do stuff like this you need to run _setup_plots() after you've done all of the yt-related plot customizations. After _setup_plots() runs the plot should be fully set up. The usage of matplotlib_style_context() is only there to get matplotlib to use fonts matching the rest of the plot and not dejavu sans. Unfortunately matplotlib doesn't have an API to set that option on the plot itself so we need to use matplotlib's style context manager to override the defaults.

We could probably make generating a transparent plot easier, for example by adding a new method like plot.set_transparent(False) that then sets the transparency on the appropriate matplotlib patch objects manually. Unfortunately we need to use canvas.print_figure() to save the images because we're not using pyplot under the hood so we need to do things at a bit more of a lower level in matplotlib than you might be used to.

On Wed, Jul 25, 2018 at 4:40 PM, Logan Harbour <loganhharbour@gmail.com> wrote:
I'm having some trouble saving pdfs that are transparent using .save() with any of the PlotContainer inherited classes.

Typically when I have access to the entire figure, plt.savefig has an optional argument Transparent=True that returns the desired result. I've tried to get the underlying figure with my SlicePlot for example, but I also have additional annotations (contours) and I'm not sure how to add these to the underlying figure in order to use plt.savefig.

I see that the save function in PlotContainer creates a FigureCanvasPdf object which then calls print_figure (which calls print_pdf). This function does not have an argument for transparency.

yt-users mailing list -- yt-users@python.org
To unsubscribe send an email to yt-users-leave@python.org