2016-03-27 13 views
0

ライブグラフを実装しようとしていますが、x軸とy軸が表示されません。 Iveはコードの複数の領域に配置しようとしましたが、いずれの方法も成功しませんでした。ここでxとyのラベルをPythonのライブグラフに表示するにはどうすればよいですか?

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 



style.use('fivethirtyeight') 

fig = plt.figure() 
plt.xlabel('Time (s)') 
plt.ylabel('Temperature (F)') 
plt.title("Your drink's current temperature") 
plt.xlabel('xlabel', fontsize=10) 
plt.ylabel('ylabel', fontsize=10) 

ax1 = fig.add_subplot(1,1,1) 

def animate(i): 
    plt.xlabel('xlabel', fontsize=10) 
    plt.ylabel('ylabel', fontsize=10) 

    graph_data = open('plot.txt', 'r').read() 
    lines = graph_data.split('\n') 
    xs = [] 
    ys = [] 
    for line in lines: 
     if len(line) > 1: 
     x, y = line.split(',') 
     xs.append(x) 
     ys.append(y) 
    ax1.clear() 
    ax1.plot(xs,ys) 

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

plt.show() 

は、私は(plot.txt)から読んでいたデータファイルである:

0,27.00 
1,68.85 
2,69.30 
3,69.30 
4,69.30 
5,69.75 
6,70.20 
7,69.75 
8,69.75 
9,69.30 
10,69.75 
11,69.75 
12,69.75 
13,69.75 
14,70.20 
15,69.75 
16,69.75 
17,69.75 
18,69.75 
19,68.85 
20,69.75 
21,69.75 
22,69.75 
23,69.30 

答えて

1

問題は、あなたのアニメーション機能でclearを呼び出しているということです。あなたはそれを読んで、あなたが別のデータソース(例、ファイル)から読み込む必要がある場合だけ

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as manimation 

fig, ax = plt.subplots() 
ax.set_xlabel('Time (s)') 
ax.set_ylabel('Temperature (F)') 
ax.set_title("Your drink's current temperature") 

xs = np.linspace(0, 2*np.pi, 512) 
ys = np.sin(xs) 

ax.set_xlim(0, 2*np.pi) 
ax.set_ylim(-1, 1) 

ln, = ax.plot([], [], lw=3) 


def init(*args, **kwargs): 
    ln.set_data([], []) 
    return ln, 


def animate(i): 
    ln.set_data(xs[:i], ys[:i]) 
    return ln, 

ani = manimation.FuncAnimation(fig, animate, 
           frames=512, blit=True, 
           init_func=init) 
plt.show() 

(必要に応じてそれを再描画のアニメーションコードの世話をしてみましょう)既存のアーティストを更新したいですあなたの行にデータを設定します。

関連する問題