Saving Incanter charts as PDF documents

Incanter charts can be saved as PNG files using the save function, but I had a request earlier today to add the ability to save them as PDF documents.

So I’ve created a new function called save-pdf in a new package called incanter.pdf.

Here’s a basic example.

(use '(incanter core charts pdf))
(save-pdf (function-plot sin -4 4) "./pdf-chart.pdf")

Which outputs the following PDF file.

10 responses to “Saving Incanter charts as PDF documents

  1. Man it was fast! Thanks, am gonna try it, and even add to the paper in the works!

  2. Hi David,
    This is good work. The generated PDF looks good, and I see you were able to save it as a true vector graphic, not a raster image, so it scales up and down nicely. Another great feature for Incanter.

  3. Nice capability. I haven’t run the code, but it looks like there may be some missing characters from the example. Shouldn’t it be something like:
    (use ‘(incanter core charts pdf)) ?

  4. The package isn’t available through lein / yet, if I’m not mistaken? could you add it so it can be automatically installed by people using leiningen?

    • I’m afraid Incanter’s modular build structure isn’t supported in the Clojars repository. We are working on getting it deployed in Sonatype’s public repository (

      You can use Leiningen to build projects with Incanter as a dependency right now though, you just need to install it with Maven first (mvn install), then include the following dependency:

      [org.incanter/incanter-full “1.2.0-SNAPSHOT”]


  5. Hi,

    I tried to save a chart to PDF, and the resulted chart seemed to be cropped in order to fit in the default width and height. Is there any way to scale the chart to fit in the output dimension instead of cropping?

    This also happened when saving a chart to PNG.

    Thank you,
    – Cox

    • That’s very strange, I have never seen the cropping behavior, it normally scales by default. Try using the :width and :height options to save and save-pdf and see if that resolves the issue.


      • Hi, David,

        Yes, it’s very strange. I haven’t change anything but now it works for me. Sorry for the false alarm.

        I have another question now. I tried to add pointers to it chart and save it. But the pointers are all gone in the saved chart. Can you help me on this?

  6. Hi,
    Thanks for the great tool!
    I wonder if it would be more convenient for save to infer output format out of file extension.

    (save (function-plot sin -4 4) "./pdf-chart.pdf"))
    generates pdf and

    (save (function-plot sin -4 4) "./png-chart.png"))
    generates png
    I’ve probably seen this approach implemented in some ruby lib (don’t remember exactly which).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s