Matplotlib版本 - 1.53
后端 - qt4agg
我使用以下代码实现了复制到剪贴板功能。
fig = self.canvas.figure
fig.set_size_inches(width,height)
buf = io.BytesIO()
fig.savefig(buf,dpi=300,transparent=True,bbox_inches='tight')
image=QtGui.QImage.fromData(buf.getvalue())
mimeData = QMimeData()
mimeData.setData("PNG",buf.getvalue())
QtGui.QApplication.clipboard().setMimeData(mimeData)
buf.close()
我将图形宽度和高度作为用户输入。
Ctrl + C后
现在复制的图形(具有用户输入高度/宽度)显示在原始图像的顶部。这个叠加的图形很有用,因为可以根据复制的图形大小拖动任何重叠的图例,注释等。所以这是一种打印预览。 (我没有实现 - 自动出现)
图像完全复制到剪贴板。我唯一的问题是底层人物(美学)。缩放/平移等仅适用于叠加图形,并且对基础图形没有影响。有没有办法只清除基础数字。
调整窗口大小会移除叠加图形并将控制恢复为基础图形。
示例代码:
import io
from PyQt4.uic import loadUiType
from PyQt4.QtCore import QMimeData
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from matplotlib.backend_bases import key_press_handler
Ui_MainWindow, QMainWindow = loadUiType('window.ui')
class Main(QMainWindow, Ui_MainWindow):
def __init__(self, ):
super(Main, self).__init__()
self.setupUi(self)
self.fig_dict = {}
self.mplfigs.itemClicked.connect(self.changefig)
self.menu()
fig = Figure()
self.addmpl(fig)
def menu(self):
copyimg=QtGui.QAction('&Copy', self)
copyimg.setShortcut('Ctrl+C')
copyimg.setStatusTip('Copy to Clipboard')
copyimg.triggered.connect(self.copy_to_clipboard)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&Edit')
fileMenu.addAction(copyimg)
def copy_to_clipboard(self):
fig = self.canvas.figure
fig.set_size_inches(6,4)
buf = io.BytesIO()
fig.savefig(buf,dpi=300,transparent=True,bbox_inches='tight')
image=QtGui.QImage.fromData(buf.getvalue())
mimeData = QMimeData()
mimeData.setData("PNG",buf.getvalue())
QtGui.QApplication.clipboard().setMimeData(mimeData)
buf.close()
def changefig(self, item):
text = item.text()
text = str(text)
self.rmmpl()
self.addmpl(self.fig_dict[text])
def addfig(self, name, fig):
self.fig_dict[name] = fig
self.mplfigs.addItem(name)
def addmpl(self, fig):
self.canvas = FigureCanvas(fig)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas,
self.mplwindow, coordinates=True)
self.mplvl.addWidget(self.toolbar)
self.canvas.mpl_connect('key_press_event', self.on_key_press)
def on_key_press(self, event):
key_press_handler(event, self.canvas, self.toolbar)
def rmmpl(self,):
self.mplvl.removeWidget(self.canvas)
self.canvas.close()
self.mplvl.removeWidget(self.toolbar)
self.toolbar.close()
if __name__ == '__main__':
import sys
from PyQt4 import QtGui
import numpy as np
fig1 = Figure()
ax1f1 = fig1.add_subplot(111)
ax1f1.plot(np.random.rand(5))
app = QtGui.QApplication(sys.argv)
main = Main()
main.addfig('One plot', fig1)
main.show()
sys.exit(app.exec_())
window.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1280</width>
<height>768</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="mplwindow" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
<layout class="QVBoxLayout" name="mplvl"/>
</widget>
</item>
<item>
<widget class="QListWidget" name="mplfigs">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::ClickFocus</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1280</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
由于