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. Thoughts?
Hi Logan,
Something like this *may* work:
import yt
ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
plot = yt.SlicePlot(ds, 2, 'density')
plot.annotate_grids()
plot._setup_plots()
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
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.
Thoughts? _______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
The above worked perfectly. For anyone potentially looking at this in the future, I did the following (even with annotations using separate variables and Exodus); ds = yt.load("somefile.e") slc = yt.SlicePlot(ds, 2, 'var1') slc.annotate_contour('var2') slc._setup_plots() fig = slc.plots['var1'].figure with yt.funcs.matplotlib_style_context(): fig.savefig('my_plot.pdf', transparent=True) Also... I definitely goofed when submitting this. I'm not sure how HyperKitty works but the following thread can be deleted if possible as a duplicate: https://mail.python.org/mm3/archives/list/yt-users@python.org/thread/I3R2SHF...
No worries! It’s not a big deal :)
BTW the #help channel on the yt slack is also a great venue for asking for
yt help:
https://yt-project.org/slack.html
On Wed, Jul 25, 2018 at 10:44 PM Logan Harbour
See above... I'm awful at mailing lists. _______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
participants (2)
-
Logan Harbour
-
Nathan Goldbaum