【问题标题】:How to show progressive curve in matplotlib如何在 matplotlib 中显示渐进曲线
【发布时间】:2018-04-08 00:30:09
【问题描述】:

我现在有这个代码:

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

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(1,6)
y = np.arange(5,11)
z = np.arange(3,9)

for i in range(4):
    ax.plot([x[i], x[i+1]], [y[i],y[i+1]],[z[i],z[i+1]])
    plt.show()

我想展示曲线是如何前进(移动)的。但这不起作用。它显示了第一行,无法移动到下一个绘图,当我关闭窗口时,程序就结束了,没有显示接下来的 3 行。

最后我也尝试添加plt.waitforbuttonpress(),但没有帮助。

我是 python 新手,所以我可能会遗漏一些简单的东西。感谢您的帮助!

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

    听起来您需要3D animation。希望这是您想要的:

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    
    def move_curve(i, line, x, y, z):
        line.set_data([[x[i], x[i+1]], [y[i],y[i+1]]])
        line.set_3d_properties([z[i],z[i+1]])
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    x = np.arange(1,6)
    y = np.arange(5,11)
    z = np.arange(3,9)
    
    i = 0
    line = ax.plot([x[i], x[i+1]], [y[i],y[i+1]], [z[i],z[i+1]])[0]
    ax.set_xlim3d([1, 5])
    ax.set_ylim3d([5, 10])
    ax.set_zlim3d([3, 8])
    
    line_ani = animation.FuncAnimation(fig, move_curve, 4, fargs=(line, x, y, z))
    

    编辑:显示线条增长而不是线条移动。

    基本思想是在每一帧循环的线上添加一个点,而不是改变线的起点和终点:

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    
    def move_curve(i, line, x, y, z):
        # Add points rather than changing start and end points.
        line.set_data(x[:i+1], y[:i+1])
        line.set_3d_properties(z[:i+1])
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    x = np.arange(1,6)
    y = np.arange(5,11)
    z = np.arange(3,9)
    
    i = 0
    line = ax.plot([x[i], x[i+1]], [y[i],y[i+1]], [z[i],z[i+1]])[0]
    ax.set_xlim3d([1, 5])
    ax.set_ylim3d([5, 10])
    ax.set_zlim3d([3, 8])
    
    line_ani = animation.FuncAnimation(fig, move_curve, 5, fargs=(line, x, y, z))
    

    编辑2:更新轴限制;用不同的颜色画线;跳过偶数行。

    基本思路是:

    1. ax 作为fargs 之一传递,以便您可以使用ax 更新轴限制。
    2. 将您的行设置为 4 个空行,并在每一帧中显示(或跳过)相应的行。默认情况下,不同的线条会有不同的颜色。

    这是一些开始的代码。为了清楚起见,以下代码在设计上并不是最好的,可能符合您的需求,也可能不符合您的需求。但我认为这是一个很好的起点。

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    
    def move_curve(num, ax, lines, x, y, z):
        for i in range(num+1):
            if i % 2 == 1:
                continue
            lines[i].set_data([[x[i], x[i+1]], [y[i],y[i+1]]])
            lines[i].set_3d_properties([z[i],z[i+1]])
        ax.set_xlim3d([1, x[i+1]])
        ax.set_ylim3d([5, y[i+1]])
        ax.set_zlim3d([3, z[i+1]])
        return lines
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    x = np.arange(1,6)
    y = np.arange(5,11)
    z = np.arange(3,9)
    
    lines = [ax.plot([], [], [])[0] for i in range(4)]
    
    line_ani = animation.FuncAnimation(fig, move_curve, 4, fargs=(ax, lines, x, y, z), repeat=False)
    

    【讨论】:

    • 不,不是。我想在循环第一次运行时显示第一行,当循环第二次运行时显示第一行和第二行,依此类推...
    • 是否可以在循环本身中显示 plt?因为实际上,当循环运行时,我得到了下一个点,所以我想将它与之前的点连接起来。
    • @Ank 你能看看我的编辑,看看这是否是你想要的吗?这条线将增长而不是移动。您不必在循环中显示 plt,因为“绘图”将为每一帧显示 interval 毫秒。
    • 是否可以随着线的增长也更新轴?
    • 不同的线条可以使用不同的颜色吗?
    猜你喜欢
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2012-04-20
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多