我在 matplotlib 中制作了一个图形,并希望将其制作成图像并在我的 pyqt5 应用程序中使用它。有人建议我为此使用 BytesIO。到目前为止,这是我的代码:
绘制我的图形:
...
plt.axis('equal')
buff = io.BytesIO()
plt.savefig(buff, format="png")
print(buff)
return buff
然后在另一个脚本中调用它:
def minionRatioGraphSetup(self, recentMinionRatioAvg):
image = minionRatioGraph(recentMinionRatioAvg)
label = QtWidgets.QLabel()
pixmap = QtGui.QPixmap(image)
label.setPixmap(pixmap)
label.setGeometry(QtCore.QRect(0,0,200,200))
它停止工作,pixmap = QtGui.QPixmap(image)
我不确定为什么。另外:我怎么能把它放在我的主窗口中?因为我怀疑那里的代码会起作用,哈哈
我确定有使用缓冲区的解决方案。然而,让字节格式正确似乎相当复杂。所以另一种方法是将图像保存到磁盘,然后从那里加载它。
import sys
from PyQt4 import QtGui
import matplotlib.pyplot as plt
import numpy as np
def minionRatioGraph():
plt.plot([1,3,2])
plt.savefig(__file__+".png", format="png")
class App(QtGui.QWidget):
def __init__(self):
super(App, self).__init__()
self.setGeometry(300, 300, 250, 150)
self.setLayout(QtGui.QVBoxLayout())
label = QtGui.QLabel()
label2 = QtGui.QLabel("Some other text label")
minionRatioGraph()
qimg = QtGui.QImage(__file__+".png")
pixmap = QtGui.QPixmap(qimg)
label.setPixmap(pixmap)
self.layout().addWidget(label)
self.layout().addWidget(label2)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication([])
ex = App()
sys.exit(app.exec_())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句