【问题标题】:3D graphic clean image in loop in PythonPython循环中的3D图形清洁图像
【发布时间】:2017-10-08 10:39:00
【问题描述】:

我在循环中有这个图,问题是它为每次迭代打开一个新图像,我的 plt.clf() 似乎没有工作,我该如何解决这个问题?

plt.ion()

for k in range(1,10):

    for i in range(0,len(x)):

        for j in range(0,len(x[i])-1):

       plt.clf()
       fig = plt.figure()
       ax = fig.add_subplot(111, projection='3d')
       ax.set_title('Iteration %s\n' % (str(k)))
       ax.set_xlabel('X')
       ax.set_ylabel('Y')
       ax.set_zlabel('Z')
       ax.set_xlim(-1, 1)
       ax.set_ylim(-1, 1)
       ax.set_zlim(-1, 1)

      plt.show()
      plt.pause(0.05)

我隐藏了代码,因为我认为问题出现在我在这里发布的代码中

P.S.:我正在使用:

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

【问题讨论】:

    标签: python python-2.7 matplotlib mplot3d


    【解决方案1】:

    这里的问题是设置fig = plt.figure() 每次迭代,然后使用plt.gcf().clear() 清理绘图,您应该这样做:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    plt.ion()
    
    x = [[1,5],[2,4]]
    fig = plt.figure()
    
    for k in range(1,10):
    
        for i in range(0,len(x)):
    
            for j in range(0,len(x[i])-1):    
    
                ax = fig.add_subplot(111, projection='3d')
                ax.set_title('Iteration %s\n' % (str(k)))
                ax.set_xlabel('X')
                ax.set_ylabel('Y')
                ax.set_zlabel('Z')
                ax.set_xlim(-1, 1)
                ax.set_ylim(-1, 1)
                ax.set_zlim(-1, 1)
                plt.show()
                plt.pause(0.5)
                plt.gcf().clear()
    

    【讨论】:

    • 您介意展示更多代码吗?特别是plt.show() 的循环
    • 我按要求编辑了代码,再看问题
    • @QuestionsOverflow 我编辑了我的答案,它确实没有像我想象的那样工作。
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多