Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create pdf with tooltips in python

This a python copy of the popular and highly upvoted Create pdf with tooltips in R .

Simple question: Is there a way to plot a graph from python in a pdf file and include tooltips?

like image 337
graffe Avatar asked Feb 25 '14 19:02

graffe


1 Answers

You can use the matplotlib pgf backend to do that. Then you can use different packages at the preamble. In this case I am using the pdfcomment.

This is a very simple example, but I think you can go from here!

import matplotlib as mpl
mpl.use("pgf")
pgf_with_pdflatex = {
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
        r"\usepackage[author={me}]{pdfcomment}",
    ]
}
mpl.rcParams.update(pgf_with_pdflatex)

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.plot(range(5))

for i in range(5):
    plt.text(i,i,r"\pdftooltip{o}{(%d,%d)}"%(i,i))

plt.savefig("tooltips.pdf")

There is a slight misplacement of the character "o", but that can be fixed with few tweaks. It is also much simpler than the R case.

PS: The tooltips can only be visualised with Acrobat Reader.

Hope it helps you.

like image 180
mrcl Avatar answered Oct 01 '22 14:10

mrcl