【问题标题】:Matplotlib unable to save the file correctly or completelyMatplotlib 无法正确或完全保存文件
【发布时间】:2017-11-10 10:32:20
【问题描述】:

我一直在尝试构建机器数据的可视化,我正在使用 pandas 数据框来存储数据。我正在使用 for 循环来选择数据集的各个部分。我构建的可视化在 X 轴上有时间,因为可视化以毫秒为单位,所以图的宽度很长。

def jobwise_plotter(prog_no):
    df = single_df[single_df['PRG_NO']==prog_no]

    #fig1 = plt.figure()

    #jobs_list = list(df['Job_no'].unique())
    #tools_list = list(df['TOOL_No.'].unique())

    for job in list(df['Job_no'].unique()):

        df_temp = df[df['Job_no']==job]

        time_axis_max = 0

        for tool in list(df_temp['TOOL_No.'].unique()):

            plt.plot(df_temp['Time'][df_temp['TOOL_No.']==tool], 
                     df_temp['Spindle'][df_temp['TOOL_No.']==tool], label=str(tool))



            if int(df_temp['Time'][df_temp['TOOL_No.']==tool].max()) > time_axis_max:
                time_axis_max = int(df_temp['Time'][df_temp['TOOL_No.']==tool].max())




        plt.title("Prog No. : "+ str(prog_no) + ", Job no : "+ str(job) + ", All Tools")

        plt.xlabel("Time (in ms.)")
        plt.ylabel("Spindle Load")

        name = "prog_"+str(prog_no)+"_"+"JOB_"+str(job)+"_All_tools"
        plt.xticks(np.arange(0, time_axis_max+10000, 5000))
        plt.grid(linewidth = 2, linestyle = '-', color='#eff3f9')


        lgd = plt.legend(loc='upper center', bbox_to_anchor=(0.5, 0.98), ncol=10)

        plt.tight_layout()
        fig1 = plt.gcf()

        fig1.set_size_inches(100, 10)
        #fig1 = plt.gcf()
        print(name)
        fig1.savefig(name, dpi=500, bbox_extra_artists=(lgd,))

        plt.show()

代码运行良好,并在 IPython 笔记本中构建了必要的可视化,但无法查看保存的图像(现在三次冻结我的系统)。 可以存储的图像是否有任何限制?该图可在笔记本中查看,我们甚至可以从浏览器下载。

【问题讨论】:

  • 如果显示预期和实际的结果会更好。此外,minimal, complete, and verifyable example 会更好,因为它无法复制。
  • 我已经添加了预期的图像。无法查看代码生成的图片。

标签: python python-3.x pandas matplotlib


【解决方案1】:

Jupyter notebook 中显示的图形是使用fig.savefig(<some buffer>, bbox_inches="tight") 保存的。

因此,为了在保存到光盘时获得相同的数字,您还需要使用bbox_inches="tight" 参数进行保存,

fig.savefig("output.png", bbox_inches="tight")

【讨论】:

  • 好点。我在执行的代码中更改了它,但我仍然无法查看保存的图像。
  • 你的意思是图片保存了,但是打不开?您当然需要指定保存格式,fig.savefig("output.png", bbox_inches="tight")fig.savefig("output.whatever", format="png", bbox_inches="tight")
  • 在 windows 和 Linux 系统中,文件系统预览显示它是一个图像,但是当我单击它时,它会冻结系统,就像查看器无法解压缩它一样。
  • 对于文件格式,我认为即使没有提及格式,图像也会以 png 格式发布。这不是我用来发布图像的代码的唯一部分。其余的工作顺利,可以保存到可以正常打开的文件中。我怀疑图像的尺寸可能与它有关。请检查我在编辑中包含的图像。
  • 好吧,我可以想象一些图像查看器无法显示 50000 x 5000 像素的图像。 (这样的图像可能会占用 >500 MB 的内存)。
猜你喜欢
  • 2014-12-08
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 2018-01-22
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多