如何使用Matplotlib更新图 [英] How to update a graph using matplotlib

查看:70
本文介绍了如何使用Matplotlib更新图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Panda 和 matplotlib 在 Python 中绘制图形.我想要一个实时更新的差距.这是我的代码:

I'm using Panda and matplotlib to draw graphs in Python. I would like a live updating gaph. Here is my code:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import numpy as np
import MySQLdb
import pandas

def animate():

    conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="sentiment_index", use_unicode=True, charset="utf8")
    c = conn.cursor()
    query = """ SELECT t_date , score FROM mytable where t_date BETWEEN Date_SUB(NOW(), Interval 2 DAY) AND NOW()"""
    c.execute(query)
    rows=c.fetchall()
    df = pandas.read_sql(query, conn, index_col=['t_date'])

    df.plot()
    plt.show()

animate()

我想过使用 FuncAnimation 但没有得到正确的结果.有什么帮助吗?

I thought about using FuncAnimation but didn't get the right result. Any help please?

推荐答案

文档对如何使用的解释有点少功能动画.但是,其中的示例画廊和博客教程,例如 Jake Vanderplas的 Sam Dolan的PDF .

The documentation is a bit light on explanation of how to use FuncAnimation. However, there are examples in the gallery and blog tutorials, such as Jake Vanderplas's and Sam Dolan's PDF.

Jake Vanderplas教程中的这个示例可能是matplotlib 动画:

This example from Jake Vanderplas's tutorial is perhaps the "Hello World" of matplotlib animation:

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def init():
    return [line]

def animate(i, ax, line):
    x = np.linspace(0, 2*np.pi, N) + i/(N*2)
    ax.set_xlim(x.min(), x.max())
    line.set_data(x, np.sin(x))
    return [line]

N = 100
fig, ax = plt.subplots()
line, = ax.plot([], [])
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=0, frames=int(4*np.pi*N), 
    repeat=True, blit=True, fargs=[ax, line])
plt.show()

更改各种值或代码行,看看会发生什么.看看会发生什么您将 return [line] 更改为其他内容.如果你学习和玩这些示例,您可以了解这些部分是如何组合在一起的.

Change various values or lines of code and see what happens. See what happens if you change return [line] to something else. If you study and play with these examples, you can learn how the pieces fit together.

一旦您了解了此示例,就可以对其进行修改以适合您的目标.

Once you understand this example, you should be able to modify it to fit your goal.

如果遇到问题,请发布代码并描述错误消息或你看到的不当行为.

If you have trouble, post your code and describe what error message or misbehavior you see.

一些提示:

  • 由于动画需要调用 line.set_data ,所以我不认为您可以使用熊猫的 df.plot().事实上,我不确定 Pandas DataFrame 是否是在这里有用.您最好将数据吸到列表或NumPy数组中并将它们传递给 line.set 如上所述,而不涉及 Pandas.

  • Since animation requires calling line.set_data, I don't think you can use Pandas' df.plot(). In fact, I'm not sure if the Pandas DataFrame is useful here. You might be better off sucking the data into lists or NumPy arrays and passing those to line.set as above, without getting Pandas involved.

打开与数据库的连接应该执行一次.animate 获取叫了很多次.所以最好定义 conncquery —— 每次调用 animate-animate 之外,并将它们作为参数传回 animate fargs 参数.

Opening a connection to the database should be done once. animate gets called many times. So it is better to define conn and c and query -- anything that does not change with each call to animate -- outside of animate, and pass them back as arguments to animate via the fargs parameter.

这篇关于如何使用Matplotlib更新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆