简体   繁体   English

如何在 matplotlib 和 pyqt5 中使用 BytesIO?

[英]How to use BytesIO with matplotlib and pyqt5?

I made a graph in matplotlib, and wanted to make it in to an image and use it in my pyqt5 application.我在 matplotlib 中制作了一个图形,并希望将其制作成图像并在我的 pyqt5 应用程序中使用它。 Someone suggested I use BytesIO for this.有人建议我为此使用 BytesIO。 This is my code so far:到目前为止,这是我的代码:

Drawing my graph:绘制我的图形:

...
plt.axis('equal')
buff = io.BytesIO()
plt.savefig(buff, format="png")
print(buff)
return buff

This is then called in another script:然后在另一个脚本中调用它:

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))

It stops working at pixmap = QtGui.QPixmap(image) and I'm unsure why.它在pixmap = QtGui.QPixmap(image)停止工作,我不确定为什么。 Also: How could I place this in my MainWindow?另外:我怎么能把它放在我的主窗口中? because I doubt the code there will work lol因为我怀疑那里的代码会起作用,哈哈

I'm sure there is a solution using a buffer.我确定有使用缓冲区的解决方案。 However, it seems rather complicated to get the byte format correct.然而,让字节格式正确似乎相当复杂。 So an alternative is to save the image to disk, and then load it from there.所以另一种方法是将图像保存到磁盘,然后从那里加载它。

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_())

a snippet using pillow, might help avoiding file io使用枕头的片段,可能有助于避免文件 io

 im = PIL.Image.open("filename")
 with BytesIO() as f:
     im.save(f, format='png')
     f.seek(0)
     image_data = f.read()
     qimg = QImage.fromData(image_data)
     patch_qt = QPixmap.fromImage(qimg)

Here's a solution that only uses the buffer, you need to use PIL to create an ImageQT and then load it to the QPixap这里有一个只使用缓冲区的解决方案,需要使用PIL创建一个ImageQT然后加载到QPixap

import matplotlib.pyplot as plt
import io
from PIL.ImageQt import ImageQt
from PIL import Image

...

buff = io.BytesIO()
plt.savefig(buff, format="png")
img = Image.open(buff)
img_qt = ImageQt(img)
return img_qt

Then, in your GUI setup, you call your function to return the ImageQT and generate the QPixmap using QPixmap.fromImage()然后,在您的 GUI 设置中,您调用您的函数以返回 ImageQT 并使用 QPixmap.fromImage() 生成 QPixmap

def minionRatioGraphSetup(self, recentMinionRatioAvg):
    image = minionRatioGraph(recentMinionRatioAvg)
    label = QtWidgets.QLabel()
    pixmap = QtGui.QPixmap.fromImage(image)
    label.setPixmap(pixmap)
    label.setGeometry(QtCore.QRect(0,0,200,200))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM