【亲测有效】Linux/Ubuntu远程服务器使用plt.show()没有反应,vscode ssh 远程ubuntu,plt.show不显示图片问题

【亲测有效】Linux/Ubuntu远程服务器使用plt.show没有反应,vscode ssh 远程ubuntu,plt.show不显示图片问题

plt.show()在linux或者ubuntu系统中不会有显示,这是因为系统没有图形界面。

例如:

		from matplotlib import pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D
        from mpl_toolkits.mplot3d.art3d import Poly3DCollection

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        mesh = Poly3DCollection(vertices[model.faces], alpha=0.1)
        face_color = (1.0, 1.0, 0.9)
        edge_color = (0, 0, 0)
        mesh.set_edgecolor(edge_color)
        mesh.set_facecolor(face_color)
        ax.add_collection3d(mesh)
        ax.scatter(joints[:, 0], joints[:, 1], joints[:, 2], color='r')

        ax.view_init(azim=-90, elev=100) #  y轴朝上
        if plot_joints:
            ax.scatter(joints[:, 0], joints[:, 1], joints[:, 2], alpha=0.1)
        plt.show()
        #这里是要保存的路径/home/img_save_folder/和保存文件名Picture.png

例如,我这一段是加载我自己处理的数据时候,然后plt.show(),不可视化出我的图片。

解决方法:保存成png图片然后在程序运行后查看,如下:

只需要使用plt.savefig(savepath)。savepath为你要保存该图到哪个路径,比如下面的’./ok.png’,是一个相对路径,就是将plt.show()的图片保存到当前路径下,名称为ok.png.

from matplotlib import pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D
        from mpl_toolkits.mplot3d.art3d import Poly3DCollection

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        mesh = Poly3DCollection(vertices[model.faces], alpha=0.1)
        face_color = (1.0, 1.0, 0.9)
        edge_color = (0, 0, 0)
        mesh.set_edgecolor(edge_color)
        mesh.set_facecolor(face_color)
        ax.add_collection3d(mesh)
        ax.scatter(joints[:, 0], joints[:, 1], joints[:, 2], color='r')

        ax.view_init(azim=-90, elev=100) #  y轴朝上
        if plot_joints:
            ax.scatter(joints[:, 0], joints[:, 1], joints[:, 2], alpha=0.1)
        plt.show()
        plt.savefig("./outputs_zzk/ok"+".png") # 保存观看
        plt.close()

如果要同时保存多个图片,为防止覆盖,可以添加plt.close()以及为图片名编号:

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 12:16:05       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 12:16:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 12:16:05       4 阅读
  4. Python语言-面向对象

    2024-07-10 12:16:05       5 阅读

热门阅读

  1. 【maya插件开发】vscode debug python 代码

    2024-07-10 12:16:05       9 阅读
  2. 【AI应用探讨】—主成分分析(PCA)应用场景

    2024-07-10 12:16:05       11 阅读
  3. 基数排序算法Python实现

    2024-07-10 12:16:05       8 阅读
  4. qt todoapp

    2024-07-10 12:16:05       8 阅读
  5. 如何减少开发过程中的bug-数据库篇

    2024-07-10 12:16:05       9 阅读
  6. 驻场运维的前途在哪里,这里有金玉良言

    2024-07-10 12:16:05       9 阅读
  7. 认字之 刬

    2024-07-10 12:16:05       7 阅读
  8. lvs集群

    lvs集群

    2024-07-10 12:16:05      8 阅读
  9. AUTOSAR:汽车软件架构的未来

    2024-07-10 12:16:05       9 阅读
  10. 相机光学(二十九)——显色指数(Ra)

    2024-07-10 12:16:05       8 阅读
  11. 贪心算法合集一

    2024-07-10 12:16:05       8 阅读