浏览器中的Matplotlib动画

时间:2018-11-25 21:27:02

标签: python matplotlib mpld3

我在尝试使用mpld3在浏览器中查看动画图时遇到麻烦。关于我做错了什么的任何提示?如果我能弄清楚如何将其转换为html以便将其嵌入到我的网站中,那会更好。

import matplotlib.pyplot as plt
import mpld3
import matplotlib.animation as animation
from matplotlib import style
import pandas as pd
import matplotlib.dates
from mpldatacursor import datacursor

style.use('fivethirtyeight')

fig = plt.figure()
ax = plt.subplot2grid((1,1), (0,0))

dateparse = lambda x: pd.datetime.strptime(x, 
                                       '%m/%d/%y %I:%M:%S %p')
def animate (i):
    df = pd.read_csv('arp.csv', 
                     index_col='Number',
                     parse_dates=['Date'], 
                     date_parser=dateparse, 
                     delimiter =',')

    e = i+1440
    df_tfvolts = df.iloc[i:e, 1]
    df_tftime = df.iloc[i:e, 0]
    b = matplotlib.dates.date2num(df.iloc[i:e, 0])
    ax.clear()
    ax.plot(df_tftime, df_tfvolts, 'r')
    ax.fill_between(b, df_tfvolts, facecolor='r', alpha =0.3)

    for label in ax.xaxis.get_ticklabels():
        label.set_rotation(30)

    plt.title('Lghtbug 7')
    plt.subplots_adjust(top=0.893, 
                        bottom=0.2, 
                        right=0.962, 
                        left=0.11)

    plt.xlabel('Date')
    plt.ylabel('Volt')


    datacursor(hover=True, bbox = None, point_labels = None, 
               draggable = True)

ani = animation.FuncAnimation (fig, 
                               animate, 
                               interval=10)

mpld3.show()

1 个答案:

答案 0 :(得分:0)

我真的想制作动画,可以从Bokeh库(代码为the surface3d example)中浏览here

否则,如果您只是想逐步查看数据中发生的情况,建议您使用滑块。可以在mpld3中执行此操作,但是demo无法正常工作……您可以使用Bokeh来检查this other example