Reputation: 427
I am trying to save a matplotlib figure as a JPEG file and am getting the following error. I'm not sure if it has to do with my install or with my code. therefore I have as well attached a sample program to perform that produces the error below. I have no issue with saving the figure as a .png or.pdf only an issue with .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
Sample Code:
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_())
Version Info:
Matplotlib 2.1.0
Pyqt5
Python 3.5
Pillow-3.1.2
Ubuntu 16.04
Upvotes: 3
Views: 2504
Reputation: 427
The issue was corrected by updating Pillow. The following command was run: sudo pip3 pillow --upgrade
Upvotes: 4