动画matplotlib与日期不起作用?

发布于 2025-02-01 08:18:29 字数 2394 浏览 3 评论 0原文

我正在尝试添加x轴中的随机变量的动画图。 我已经尝试了不同的事情,但是代码只是使用静态X轴数组。

我制作了一个小功能来更新日期数组t和随机var Array y i叫Shift()。

  • 要查看基本代码(无日期)的行为,您需要删除每一行,其后是“#uncomment 1”。 viceversa uncommoment每行都有“#uncomment 0”。

我不知道为什么我不能绘制X轴中的日期。

以下是代码:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
import time
import datetime


plt.rcParams.update({'axes.facecolor':'lightgray'})
plt.rcParams.update({'figure.facecolor':'gray'})
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
# ax = plt.axes()
ax = plt.axes(xlim=(0, 2), ylim=(-8, 8))
line, = ax.plot([], [], lw=2)


def shift(y_arr,y_i,cont_cascata):
    print("cont_cascata:",cont_cascata)
    if type(y_arr)==list:
        y_arr.pop(0)
        y_arr = y_arr+[y_i]
    if type(y_arr) is np.ndarray:
        print("np.array..")
        y_arr = np.delete(y_arr, 0)  # togliamo primo
        y_arr = np.append(y_arr, y_i)  # aggiungiamo ultimo
    return y_arr

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
# pd._libs.tslibs.timestamps.Timestamp
def ts_array(n):
    t0 = pd.Timestamp(2018,1,1,12,30)
    T = []
    for i in range(n):
        t_i = t0+pd.Timedelta(minutes=i)
        T = T+[t_i]
    return T

# tarr = ts_array(n=100)


def animate(i):
    global y,x,T
    n = 100
    if i==0:
        y = np.round(np.random.normal(loc=0, scale=2, size=n), decimals=2)
        x = np.linspace(0, 2, n)  # uncomment 0
        T = ts_array(n)   # uncomment 1

    y_i = np.round(np.random.normal(loc=0,scale=2),decimals=2)
    t_i = T[-1]+pd.Timedelta(minutes=1)   # uncomment1

    y = shift(y_arr=y,y_i=y_i, cont_cascata=i)
    T = shift(y_arr=T,y_i=t_i,cont_cascata=i)  # uncomment 1
    T = pd.DatetimeIndex(T)   # uncomment 1
    T = T.to_pydatetime()    # uncomment 1

    # line.set_data(x, y)  # uncomment 0
    line.set_data(T,y)   # uncomment 1
    time.sleep(0.5)
    return line,

print("animate")

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()

我该怎么做才能使此代码正常工作?谢谢

I'm trying to add to an animated plot of a random variable the dates in the x axes.
I've tried different things but the code is working just with a static x-axes array..

I made a small function to update the dates array T and random var array y the I called shift().

  • To see how the basic code (no dates) is behaving you need to uncomment every line that is followed by "# uncomment 1". Viceversa uncomment every line that has "# uncomment 0".

I don't know why I can't plot the dates in the x-axes.

This below is the code:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pandas as pd
import time
import datetime


plt.rcParams.update({'axes.facecolor':'lightgray'})
plt.rcParams.update({'figure.facecolor':'gray'})
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
# ax = plt.axes()
ax = plt.axes(xlim=(0, 2), ylim=(-8, 8))
line, = ax.plot([], [], lw=2)


def shift(y_arr,y_i,cont_cascata):
    print("cont_cascata:",cont_cascata)
    if type(y_arr)==list:
        y_arr.pop(0)
        y_arr = y_arr+[y_i]
    if type(y_arr) is np.ndarray:
        print("np.array..")
        y_arr = np.delete(y_arr, 0)  # togliamo primo
        y_arr = np.append(y_arr, y_i)  # aggiungiamo ultimo
    return y_arr

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
# pd._libs.tslibs.timestamps.Timestamp
def ts_array(n):
    t0 = pd.Timestamp(2018,1,1,12,30)
    T = []
    for i in range(n):
        t_i = t0+pd.Timedelta(minutes=i)
        T = T+[t_i]
    return T

# tarr = ts_array(n=100)


def animate(i):
    global y,x,T
    n = 100
    if i==0:
        y = np.round(np.random.normal(loc=0, scale=2, size=n), decimals=2)
        x = np.linspace(0, 2, n)  # uncomment 0
        T = ts_array(n)   # uncomment 1

    y_i = np.round(np.random.normal(loc=0,scale=2),decimals=2)
    t_i = T[-1]+pd.Timedelta(minutes=1)   # uncomment1

    y = shift(y_arr=y,y_i=y_i, cont_cascata=i)
    T = shift(y_arr=T,y_i=t_i,cont_cascata=i)  # uncomment 1
    T = pd.DatetimeIndex(T)   # uncomment 1
    T = T.to_pydatetime()    # uncomment 1

    # line.set_data(x, y)  # uncomment 0
    line.set_data(T,y)   # uncomment 1
    time.sleep(0.5)
    return line,

print("animate")

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()

What should I do to make this code work properly? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2025-02-08 08:18:29

您需要调整轴的xlim以说明日期时间。内部animateline.set_data(t,y)之后,尝试添加以下内容:

ax.set_xlim(T.min(), T.max())

You need to adjust the xlim of the axes to account for date time. Inside animate, just after line.set_data(T,y), try adding this:

ax.set_xlim(T.min(), T.max())
枫以 2025-02-08 08:18:29
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.dates as mdates
import pandas as pd
import time
import datetime

plt.rcParams.update({'axes.facecolor': 'lightgray'})
plt.rcParams.update({'figure.facecolor': 'gray'})
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()  # include

ax = plt.axes()  # include
# ax = plt.axes(xlim=(0, 2), ylim=(-8, 8))
line, = ax.plot([], [], lw=2)

## tilt dates
plt.setp(ax.xaxis.get_majorticklabels(), rotation=35)


def shift(y_arr, y_i, cont_cascata):
    # print("cont_cascata:",cont_cascata)
    if type(y_arr) == list:
        y_arr.pop(0)
        y_arr = y_arr + [y_i]
    if type(y_arr) is np.ndarray:
        # print("np.array..")
        y_arr = np.delete(y_arr, 0)  # togliamo primo
        y_arr = np.append(y_arr, y_i)  # aggiungiamo ultimo
    return y_arr


# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    line.axes.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))  # "%Y-%m-%d %H:%M:%S"
    return line,


# animation function.  This is called sequentially
# pd._libs.tslibs.timestamps.Timestamp
def ts_array(n):
    t0 = pd.Timestamp(2018, 1, 1, 12, 00)
    T = []
    for i in range(n):
        t_i = t0 + pd.Timedelta(minutes=i)
        T = T + [t_i]
    return T




def animate(i):
    global y, x, T
    print("i:", i)
    n = 10

    if i == 0:
        y = np.round(np.random.normal(loc=0, scale=2, size=n), decimals=2)
        x = np.linspace(6, 2, n)  # uncomment 0
        T = ts_array(n)  # uncomment 1

    y_i = np.round(np.random.normal(loc=6, scale=2), decimals=2)
    t_i = T[-1] + pd.Timedelta(minutes=1)  # uncomment1

    y = shift(y_arr=y, y_i=y_i, cont_cascata=i)
    T = shift(y_arr=T, y_i=t_i, cont_cascata=i)  # uncomment 1
    T = pd.DatetimeIndex(T)  # uncomment 1
    T = T.to_pydatetime()    # uncomment 1

    # line.set_data(x, y)  # uncomment 0

    line.set_data(T, y)  # uncomment 1
    ax.relim(visible_only=True)
    ax.autoscale()
    # ax.autoscale_view(True,True,True)

    # time.sleep(0.5)
    return line,


print("animate")

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=500)  # blit = True

plt.show()
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.dates as mdates
import pandas as pd
import time
import datetime

plt.rcParams.update({'axes.facecolor': 'lightgray'})
plt.rcParams.update({'figure.facecolor': 'gray'})
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()  # include

ax = plt.axes()  # include
# ax = plt.axes(xlim=(0, 2), ylim=(-8, 8))
line, = ax.plot([], [], lw=2)

## tilt dates
plt.setp(ax.xaxis.get_majorticklabels(), rotation=35)


def shift(y_arr, y_i, cont_cascata):
    # print("cont_cascata:",cont_cascata)
    if type(y_arr) == list:
        y_arr.pop(0)
        y_arr = y_arr + [y_i]
    if type(y_arr) is np.ndarray:
        # print("np.array..")
        y_arr = np.delete(y_arr, 0)  # togliamo primo
        y_arr = np.append(y_arr, y_i)  # aggiungiamo ultimo
    return y_arr


# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    line.axes.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))  # "%Y-%m-%d %H:%M:%S"
    return line,


# animation function.  This is called sequentially
# pd._libs.tslibs.timestamps.Timestamp
def ts_array(n):
    t0 = pd.Timestamp(2018, 1, 1, 12, 00)
    T = []
    for i in range(n):
        t_i = t0 + pd.Timedelta(minutes=i)
        T = T + [t_i]
    return T




def animate(i):
    global y, x, T
    print("i:", i)
    n = 10

    if i == 0:
        y = np.round(np.random.normal(loc=0, scale=2, size=n), decimals=2)
        x = np.linspace(6, 2, n)  # uncomment 0
        T = ts_array(n)  # uncomment 1

    y_i = np.round(np.random.normal(loc=6, scale=2), decimals=2)
    t_i = T[-1] + pd.Timedelta(minutes=1)  # uncomment1

    y = shift(y_arr=y, y_i=y_i, cont_cascata=i)
    T = shift(y_arr=T, y_i=t_i, cont_cascata=i)  # uncomment 1
    T = pd.DatetimeIndex(T)  # uncomment 1
    T = T.to_pydatetime()    # uncomment 1

    # line.set_data(x, y)  # uncomment 0

    line.set_data(T, y)  # uncomment 1
    ax.relim(visible_only=True)
    ax.autoscale()
    # ax.autoscale_view(True,True,True)

    # time.sleep(0.5)
    return line,


print("animate")

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=500)  # blit = True

plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文