Skip to content

Instantly share code, notes, and snippets.

@mjkramer
Created October 8, 2020 02:16
Show Gist options
  • Save mjkramer/4effe6f0d605e78c86827e7464fbeb58 to your computer and use it in GitHub Desktop.
Save mjkramer/4effe6f0d605e78c86827e7464fbeb58 to your computer and use it in GitHub Desktop.
# Cobbled together from https://stackoverflow.com/questions/31607458
import io
import matplotlib.pyplot as plt
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import QApplication
def add_clipboard_to_figures():
oldfig = plt.figure
def newfig(*args, **kwargs):
fig = oldfig(*args, **kwargs)
def add_figure_to_clipboard(event):
if event.key == "ctrl+c":
with io.BytesIO() as buffer:
fig.savefig(buffer)
image = QImage.fromData(buffer.getvalue())
QApplication.clipboard().setImage(image)
fig.canvas.mpl_connect('key_press_event', add_figure_to_clipboard)
return fig
plt.figure = newfig
add_clipboard_to_figures()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment