不将图形保存到指定文件夹

时间:2018-06-27 18:23:45

标签: python python-3.x matplotlib

我正在生成图形并将其保存到指定的文件夹,但是在该特定目标中没有任何图形被保存。该图形仅生成一次,此后出现错误。这是我的代码

 import numpy as np
import matplotlib.pyplot as plt

final_obj=np.array([[  8.31907313 ,  0.17300669],
 [  8.50432396 ,  0.19566612],
 [  8.50432396 ,  0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396   ,0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396  , 0.19566612],
 [  8.50432396   ,0.19566612],
 [  8.50432396   ,0.19566612],
 [  8.50432396   ,0.19566612]])

print(final_obj)
x1=[]
y1=[]
for z in range(0,5):
     for i in range(0,len(final_obj)):
          X=final_obj[i][0]
          x1.append(X)
          Y=final_obj[i][1]
          y1.append(Y)


     plt.plot(x1, y1, label = "line 1",marker='o',linestyle = 'None')
     plt.xlabel("X-axis")
     plt.ylabel("Y-axis")
     plt.legend()
     plt.show()
     save_results_to = "C:\\Users\hp\PycharmProjects\SRMMEA\venv\IRIS_GRAPH"
     plt.savefig(save_results_to+'GRAPH_{}.png'.format(z), format="PNG")

它显示了以上代码的错误

Traceback (most recent call last):
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\silvspbm.py", line 35, in <module>
    plt.savefig(save_results_to+'GRAPH_{}.png'.format(z), format="PNG")
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py", line 710, in savefig
    res = fig.savefig(*args, **kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\figure.py", line 2035, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backend_bases.py", line 2263, in print_figure
    **kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
    with cbook.open_file_cm(filename_or_obj, "wb") as fh:
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 624, in open_file_cm
    fh, opened = to_filehandle(path_or_file, mode, True, encoding)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 609, in to_filehandle
    fh = io.open(fname, flag, encoding=encoding)
OSError: [Errno 22] Invalid argument: 'C:\\Users\\hp\\PycharmProjects\\SRMMEA\x0benv\\IRIS_GRAPHGRAPH_0.png'

1 个答案:

答案 0 :(得分:1)

save_results_to = "C:\\Users\hp\PycharmProjects\SRMMEA\venv\IRIS_GRAPH"

用上述代码替换上面的代码以获得所需的结果

save_results_to = "C:\\Users\\hp\\PycharmProjects\\SRMMEA\\venv\\IRIS_GRAPH\\"