In MATLAB, there is a very convenient option to copy the current figure to the clipboard. Although Python/numpy/scipy/matplotlib is a great alternative to MATLAB, such an option is unfortunately missing.
Can this option easily be added to Matplotlib figures? Preferably, all MPL figures should automatically benefit from this functionality.
I'm using MPL's Qt4Agg backend, with PySide.
EelkeSpaak's solution was packed in a nice module: addcopyfighandler
Simply install by
pip install addcopyfighandler
, and import the module after importing matplotlib or pyplot.Yes, it can. The idea is to replace the default
plt.figure
with a custom one (a technique known as monkey patching) that injects a keyboard handler for copying to the clipboard. The following code will allow you to copy any MPL figure to the clipboard by pressing Ctrl+C:Note that if you want to use
from matplotlib.pyplot import *
(e.g. in an interactive session), you need to do so after you've executed the above code, otherwise thefigure
you import into the default namespace will be the unpatched version.The last comment is very useful.
Install the package with
pip install addcopyfighandler
Import the module after importing matplotlib, for instance:
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from matplotlib.cm import get_cmap
import addcopyfighandler
Use
ctr + C
to copy the Figure to the clipboardAnd enjoy.