addcopyfighandler: Add a Ctrl+C / Cmd+C handler to matplotlib figures for copying the figure to the clipboard
Importing this module (after importing matplotlib or pyplot) will add a handler to all subsequently-created matplotlib figures so that pressing Ctrl+C (or Cmd+C on MacOS) with a matplotlib figure window selected will copy the figure to the clipboard as an image. The copied image is generated through matplotlib.pyplot.savefig(), and thus is affected by the relevant rcParams settings (savefig.dpi, savefig.format, etc.).
Uses code & concepts from:
- https://stackoverflow.com/questions/31607458/how-to-add-clipboard-support-to-matplotlib-figures
- https://stackoverflow.com/questions/34322132/copy-image-to-clipboard-in-python3
- addcopyfighandler should work regardless of which graphical backend is being used by matplotlib (tkagg, gtk3agg, qtagg, etc.).
- If
matplotlib.rcParams['savefig.format']
is'svg'
, the figure will be copied to the clipboard as an SVG. - If Pillow is installed, all non-SVG format specifiers will be overridden, and the figure will be copied to the clipboard as a Device-Independant Bitmap.
- If Pillow is not installed, the supported format specifiers are
'png'
,'jpg'
,'jpeg'
, and'svg'
. All other format specifiers will be overridden, and the figure will be copied to the clipboard as PNG data.
- Requires either Qt or GTK libraries for clipboard interaction. Automatically detects which is being used from
matplotlib.get_backend()
.- Qt support requires
PyQt5
,PyQt6
,PySide2
orPySide6
. - GTK support requires
pycairo
,PyGObject
andPIL
orpillow
to be installed.- Only GTK 3 is supported, as GTK 4 has totally changed the way clipboard data is handled and I can't figure it out. I'm totally open to someone else solving this and submitting a PR if they want. I don't use GTK.
- Qt support requires
- The figure will be copied to the clipboard as a PNG, regardless of
matplotlib.rcParams['savefig.format']
. Alas, SVG output is not currently supported. Pull requests that enable SVG support would be welcomed.
- Requires Qt, whether PyQt5/6 or PySide2/6.
- The figure will be copied to the clipboard as a PNG, regardless of matplotlib.rcParams['savefig.format'].
- Fix issue when using the Agg backend on Windows, which doesn't support get_window_title().
- Thanks to @rgw5267 for finding and fixing a bug related to the event handler being registered multiple times.
- Made backend checks case-insensitive due to undocumented changes in matplotlib 3.9.
- Added MacOS support (thanks @orlp!). No SVG support, same as Linux.
- Wrap matplotlib.pyploy.figure appropriately to maintain docstring (thanks @eendebakpt!)
- Add support for PyQt6 and PySide6 on Linux (already supported on Windows)
- Add Linux support (tested on Ubuntu). Requires PyQt5, PySide2, or PyObject libraries; relevant library chosen based on matplotlib graphical backend in use. No SVG support.
- On Windows, non SVG-formats will now use the Pillow library if installed, storing the figure to the clipboard as a device-indepenent bitmap (as previously handled in v2.0). This is compatible with a wider range of Windows applications.
- Remove Pillow.
- Add support for png & svg file formats.
- Remove Qt requirement. Now use Pillow to grab the figure image, and win32clipboard to manage the Windows clipboard.
- Force use of Qt4Agg or Qt5Agg. Some installs will default to TkAgg backend, which this module doesn't support. Forcing the backend to switch when loading this module saves the user from having to manually specify one of the Qt backends in every analysis.
- Improve setup.py: remove need for importing module, add proper installation dependencies
- Change readme from ReST to Markdown
- Initial release