LaurentRDC/pandoc-pyplot

Add Additional Parameters for saveFig

Closed this issue · 6 comments

Sometimes you'd like to be able to pass in additional parameters to saveFig. The usual one here, for me at least, is bbox_inches='tight' to get around issues with plots being cut off. Here's one example on SO to that effect.

It would be nice if, like dpi, there were configuration options for most (or all?) of the parameters that a user might want to pass to saveFig.

I don't think there's currently a way to do this, judging by how the saveFig call is constructed.

Unrelated, thanks for this library. 👍 Making the transition from "LaTeX + Makefiles running miscellaneous figure scripts" to Pandoc a lot easier.

Hey @seldridge ,

That's a good idea. Initially, that's why I had the DPI option.

What you can do right now

Add the bbox_inches="tight" option to pandoc-pyplot plots by using a preamble of the following kind:

style.py:

import matplotlib.pyplot as plt

plt.rcParams['savefig.bbox']='tight' # Sets the default value in plt.savefig(...)

In your Markdown/Latex:

{.pyplot include=style.py}
...

You can do that with most arguments to plt.savefig, e.g. facecolor, edgecolor, figsize, etc. See here for a guide to Matplotlib customization.

In the future

I can see two paths forward:

  1. Allow arbitrary arguments as a string that is directly passed to plt.savefig;
  2. Add relevant parameters to the Haskell interface, like dpi right now.

Looking at all the supported arguments in the latest Matplotlib release, there are a lot of potential that might never be used.

Beyond bbox_inches, are there any other savefig parameters you want to change?

Thank you for your kind words as well.

Ah, cool! rcParams does the trick. Thanks for the pointer to Matplotlib customization. (I had gone ahead and locally, crudely patched the saveFig call to always use my bbox_inches parameter.) I guess I'm torn as this approach is really sufficient and future-proof for API changes to saveFig...

Of your future ideas, I'd prefer (2) just because there's some notion of type safety as opposed to passing a string directly to saveFig.

Of the available parameters, I only use bbox_inches and Transparent.

I can see why you would want to use the transparent parameter often, especially on the web. I've often had to use bbox_inches for more advanced plots as well. I will add both right now.

pandoc-pyplot version 2.1.5.0 now supports two additional parameters, in the global config-file only: tight_bbox: true|false and transparent: true|false. See the updated README for instructions.

I didn't add those parameters for individual figures, but if you want that, don't hesitate to re-open this issue!

Fix looks great, thanks!