我正在尝试将 matplotlib 图保存为 JPEG 文件,但出现以下错误。我不确定它是否与我的安装或我的代码有关。因此,我还附上了一个示例程序来执行,该程序会产生以下错误。我没有将图形保存为 .png 或 .pdf 的问题,只有 .jpg 的问题

Error:
Traceback (most recent call last):
  File "simpleissue.py", line 36, in updateplot
    self.figure.savefig(savename)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1814, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_qt5agg.py", line 172, in print_figure
    super(FigureCanvasQTAggBase, self).print_figure(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backend_bases.py", line 2259, in print_figure
    **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 584, in print_jpg
    return background.save(filename_or_obj, format='jpeg', **options)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1675, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python3/dist-packages/PIL/JpegImagePlugin.py", line 708, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
  File "/usr/lib/python3/dist-packages/PIL/ImageFile.py", line 480, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 431, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float

示例代码:
import sys

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

class plot(QWidget):
    def __init__(self):
        super().__init__()

        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)

        self.Layout = QVBoxLayout()

        self.xarray = [1,2,3,4,5,6]
        self.yarray = [6,7,5,4,2,1]

        update_btn = QPushButton("Save Plot", self)
        self.Layout.addWidget(update_btn, 1)
        update_btn.clicked.connect(self.saveplot)

        self.createplot()

        self.setLayout(self.Layout)

    def saveplot(self):
        filename = QFileDialog.getSaveFileName(self, "Save Plot As", "plot.jpg", "*.jpg ;; *.png ;; *.pdf")
        savename = filename[0]
        if savename:
            self.figure.savefig(savename)

    def createplot(self):
        self.ax = self.figure.add_subplot(1,1,1)
        self.ax.grid()
        self.plot = self.ax.plot(self.xarray, self.yarray,'o', marker = 'o', c= 'b')[0]

        self.Layout.addWidget(self.canvas, 2)
        self.canvas.draw()

if __name__ == '__main__':
    appl = QApplication(sys.argv)
    main = plot()
    main.show()
    sys.exit(appl.exec_())

版本信息:

Matplotlib 2.1.0

Pyqt5

python 3.5

枕头-3.1.2

Ubuntu 16.04

最佳答案

该问题已通过更新 Pillow 得到纠正。运行了以下命令:sudo pip3 pillow --upgrade

关于python - Matplotlib 将文件保存为 JPEG 编码器错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46898063/

10-12 20:35