更新python2.7的matplotlib后,savefig返回错误

时间:2017-11-20 17:11:35

标签: python-2.7 matplotlib

我在RedHat机器上使用matplotlib v1.5。我最近将matplotlib软件包更新到v2.1.0,以下代码在以前运行良好,在执行savefig命令时返回错误。

import numpy as np;
import matplotlib as mpl;
mpl.use("Agg");
import matplotlib.pyplot as plt;

my_array = np.random.randint(0,100,1000,dtype=int);
fig1 = plt.figure(1, figsize=(20,10));
n, bins, patches = plt.hist(my_array, bins="auto", normed=1, facecolor="blue",alpha=0.8);
plt.ylabel("Freq",fontsize=30);
plt.xlabel("X",fontsize=30);
plt.xticks(fontsize=30);
plt.yticks(fontsize=30);
plt.savefig(fname="./test_pic.jpg",bbox_inches="tight");
plt.close(fig1);

错误是:

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    plt.savefig(fname="./test_pic.jpg",bbox_inches="tight");
  File "/<my_home>/.local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 697, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/<my_home>/.local/lib/python2.7/site-packages/matplotlib/figure.py", line 1814, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/<my_home>/.local/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2259, in print_figure
    **kwargs)
  File "/<my_home>/.local/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 584, in print_jpg
    return background.save(filename_or_obj, format='jpeg', **options)
  File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1477, in save
    save_handler(self, fp, filename)
  File "/usr/lib64/python2.7/site-packages/PIL/JpegImagePlugin.py", line 565, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize)
  File "/usr/lib64/python2.7/site-packages/PIL/ImageFile.py", line 470, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 395, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float

有没有办法解决它?

0 个答案:

没有答案