Deprecated

In favour of

pandoc-pyplot

A Pandoc filter for including figures generated from Matplotlib

https://github.com/LaurentRDC/pandoc-pyplot#readme

Version on this page:2.0.0.0
LTS Haskell 16.31:2.3.0.1
Stackage Nightly 2020-06-15:2.3.0.1
Latest on Hackage:2.3.0.1

See all snapshots pandoc-pyplot appears in

MIT licensed and maintained by Laurent P. René de Cotret
This version can be pinned in stack with:pandoc-pyplot-2.0.0.0@sha256:c0de4ea1d1bb681f6528fe01f2993f665b3f04a9cd316c3a29fa77a2f123503b,2396

Module documentation for 2.0.0.0

pandoc-pyplot

Hackage version Stackage version (LTS) Stackage version (nightly) Build status

A Pandoc filter for generating figures with Matplotlib from code directly in documents

Inspired by sphinx’s plot_directive, pandoc-pyplot helps turn Python code present in your documents to embedded Matplotlib figures.

Usage

The filter recognizes code blocks with the pyplot class present. It will run the script in the associated code block in a Python interpreter and capture the generated Matplotlib figure.

Basic example

Here is a basic example using the scripting matplotlib.pyplot API:

    ```{.pyplot}
    import matplotlib.pyplot as plt

    plt.figure()
    plt.plot([0,1,2,3,4], [1,2,3,4,5])
    plt.title('This is an example figure')
    ```

Putting the above in input.md, we can then generate the plot and embed it:

pandoc --filter pandoc-pyplot input.md --output output.html

or

pandoc --filter pandoc-pyplot input.md --output output.pdf

or any other output format you want. pandoc-pyplot is efficient, too: it will detect which figures should be re-generated, and skip the others.

There are more examples in the source repository, in the \examples directory.

Link to source code

In case of an output format that supports links (e.g. HTML), the embedded image generated by pandoc-pyplot will be a link to the source code which was used to generate the file. Therefore, other people can see what Python code was used to create your figures.

Captions

You can also specify a caption for your image. This is done using the optional caption parameter:

    ```{.pyplot caption="This is a simple figure"}
    import matplotlib.pyplot as plt

    plt.figure()
    plt.plot([0,1,2,3,4], [1,2,3,4,5])
    plt.title('This is an example figure')
    ```

Including scripts

If you find yourself always repeating some steps, inclusion of scripts is possible using the include parameter. For example, if you want all plots to have the ggplot style, you can write a very short preamble style.py like so:

import matplotlib.pyplot as plt
plt.style.use('ggplot')

and include it in your document as follows:

    ```{.pyplot include=style.py}
    plt.figure()
    plt.plot([0,1,2,3,4], [1,2,3,4,5])
    plt.title('This is an example figure')
    ```

Which is equivalent to writing the following markdown:

    ```{.pyplot}
    import matplotlib.pyplot as plt
    plt.style.use('ggplot')

    plt.figure()
    plt.plot([0,1,2,3,4], [1,2,3,4,5])
    plt.title('This is an example figure')
    ```

This include parameter is perfect for longer documents with many plots. Simply define the style you want in a separate script! You can also import packages this way, or define functions you often use.

Installation

Binaries

Windows binaries are available on GitHub. Place the executable in a location that is in your PATH to be able to call it.

Installers

Starting with pandoc-pyplot version 1.1.0.0, Windows installers are also made available thanks to Inno Setup.

From Hackage/Stackage

pandoc-pyplot is available on Hackage. Using the cabal-install tool:

cabal update
cabal install pandoc-pyplot

Similarly, pandoc-pyplot is available on Stackage:

stack update
stack install pandoc-pyplot

From source

Building from source can be done using stack or cabal:

git clone https://github.com/LaurentRDC/pandoc-pyplot
cd pandoc-pylot
stack install # Alternatively, `cabal install`

Running the filter

Requirements

This filter only works with the Matplotlib plotting library. Therefore, you a Python interpreter and at least Matplotlib installed. The Python interpreter is expected to be discoverable using the name "python" (as opposed to "python3", for example)

You can use the filter with Pandoc as follows:

pandoc --filter pandoc-pyplot input.md --output output.html

In which case, the output is HTML. Another example with PDF output:

pandoc --filter pandoc-pyplot input.md --output output.pdf

Python exceptions will be printed to screen in case of a problem.

pandoc-pyplot has a very limited command-line interface. Take a look at the help available using the -h or --help argument:

pandoc-pyplot --help

Usage as a Haskell library

To include the functionality of pandoc-pyplot in a Haskell package, you can use the makePlot :: Block -> IO Block function (for single blocks) or plotTransform :: Pandoc -> IO Pandoc function (for entire documents).

Usage with Hakyll

This filter was originally designed to be used with Hakyll. In case you want to use the filter with your own Hakyll setup, you can use a transform function that works on entire documents:

import Text.Pandoc.Filter.Pyplot (plotTransform)

import Hakyll

-- Unsafe compiler is required because of the interaction
-- in IO (i.e. running an external Python script).
makePlotPandocCompiler :: Compiler (Item String)
makePlotPandocCompiler =
  pandocCompilerWithTransformM
    defaultHakyllReaderOptions
    defaultHakyllWriterOptions
    (unsafeCompiler . plotTransform)

Warning

Do not run this filter on unknown documents. There is nothing in pandoc-pyplot that can stop a Python script from performing evil actions. This is the reason this package is deemed unsafe in the parlance of Safe Haskell.

Changes

Change log

pandoc-pyplot uses Semantic Versioning

Release 2.0.0.0

Many breaking changes in this release:

  • pandoc-pyplot will now determine the filename based on hashing the figure content. Therefore, figures will only be re-generated if necessary.
  • Removed the ability to control the filename and format directly using the plot_target=... attribute.
  • Added the ability to control the directory in which figures will be saved using the directory=... attribute.
  • Added the possibility to control the figures dots-per-inch (i.e. pixel density) with the dpi=... attribute.
  • Added the ability to control the figure format with the format=... attribute. Possible values are currently "png", "svg", "pdf", and "jpg"/"jpeg".
  • The confusing plot_alt=... attribute has been renamed to caption=... for obvious reasons.
  • The plot_include=... attribute has been renamed to include=....
  • Added the generation of a higher resolution figure for every figure pandoc-pyplot understands.

Release 1.1.0.0

  • Added the ability to include Python files before code using the plot_include=script.py attribute.
  • Added a test suite.

Release 1.0.3.0

  • Fixed an issue where pandoc-pyplot would not build with base < 4.9 (#1)

Release 1.0.2.0

  • Added support for captions using the plot_alt=... attribute. For example:

      ```{plot_target=test.png plot_alt="This is a caption"}
      import matplotlib.pyplot as plt
      plt.figure()
      plt.plot([1,2,3,4,5],[1,2,3,4,5])
    
    
    

Release 1.0.1.0

  • Added plotTransform :: Pandoc -> IO Pandoc function to transform entire documents. This makes it easier to integrate pandoc-pyplot into Hakyll-based sites!

Release 1.0.0.1

  • Updated README with fixes and warnings
  • Added top-level package documentation compatible with Haddock
  • Added Unsafe language extension, as this filter will run arbitrary Python scripts.

Release 1.0.0.0

Initial release.

See documentation on Hackage