2016-06-13 4 views
-2

私のコードに間違いがあります。前もって感謝します; お願いします。コードは私が望むことをしないで、2つのキャンバスを描画します。python matplotlibアニメマルチライン

from scipy import stats 
import numpy as np 
import random 
import matplotlib.pyplot as plt 
import matplotlib.dates as md 
from matplotlib import animation 
import dateutil 
from datetime import datetime, timedelta 
import time 

X = ['10:00:01', '10:00:02', '10:00:03', '10:00:04', '10:00:05', '10:00:06', '10:00:07', '10:00:08', '10:00:09', '10:00:10', '10:00:11', '10:00:12'] 
XX = X[11] 

DATES = [dateutil.parser.parse(s) for s in X] 
Y1 = [90, 60, 45, 34, 56, 66, 71, 24, 46, 12, 64, 45] 
Y2 = [45, 70, 25, 84, 96, 26, 41, 28, 76, 52, 34, 55] 
Y3 = [40, 82, 46, 87, 03, 49, 78, 24, 65, 48, 97, 46] 
Y4 = [21, 54, 75, 35, 75, 67, 24, 74, 95, 34, 30, 48] 

plt.figure(figsize=(18,10), dpi=100) 
plt.subplot(1, 1, 1) 
plt.rc('font', family='serif', size=13) 
plt_data = range(0, 100) 
plt.subplots_adjust(bottom = 0.0) 
plt.xticks(rotation = 35) 

ax=plt.gca() 
ax.set_xticks(DATES) 
xfmt = md.DateFormatter('%H:%M:%S') 
ax.xaxis.set_major_formatter(xfmt) 
plt.gcf().autofmt_xdate() 

plt.plot(DATES, Y1, color="blue", linewidth=2.0, linestyle="-") 
plt.plot(DATES, Y2, color="red", linewidth=2.0, linestyle="-") 
plt.plot(DATES, Y3, color="green", linewidth=2.0, linestyle="-") 
plt.plot(DATES, Y4, color="cyan", linewidth=2.0, linestyle="-") 

plt.xlabel('$\Delta t$ $(s)$', fontsize=20) 
plt.ylabel('$\Delta p$ $(hPa)$', fontsize=20) 
plt.autoscale(enable = True, axis = u'both', tight = False) 
plt.grid(True) 
plt.show() 
fig = plt.figure() 

def animate(args): 
    global XX 
    global Y1 
    global y2 
    global y3 
    global y4 
    global X 
    global DATES 

    Y1.append(int(random.random() * 100)) 
    Y2.append(int(random.random() * 100)) 
    Y3.append(int(random.random() * 100)) 
    Y4.append(int(random.random() * 100)) 
    del Y1[0] 
    del Y2[0] 
    del Y3[0] 
    del Y4[0] 

    TOP_DT1 = datetime.strptime(XX, '%H:%M:%S') 
    TOP_DT1 = TOP_DT1 + timedelta(seconds=1) 
    XX = TOP_DT1.strftime('%H:%M:%S') 
    X.append(XX) 
    del X[0] 
    print Y1, Y2, Y3, Y4, X 

    lines = [] 
    lines.append(plt.plot(DATES, Y1, color="blue", linewidth=2.0, linestyle="-"),) 
    lines.append(plt.plot(DATES, Y2, color="red", linewidth=2.0, linestyle="-"),) 
    lines.append(plt.plot(DATES, Y3, color="green", linewidth=2.0, linestyle="-"),) 
    lines.append(plt.plot(DATES, Y4, color="cyan", linewidth=2.0, linestyle="-"),) 

    fig.canvas.draw() 
    plt.pause(0.0001) 

    return lines 

ani = animation.FuncAnimation(fig, animate, frames=2000, interval=1, blit=True) 
plt.show() 

エラー出力 AttributeError: 'list' object has no attribute 'set_animated' [45, 34, 56, 66, 71, 24, 46, 12, 64, 45, 90, 6] [25, 84, 96, 26, 41, 28, 76, 52, 34, 55, 22, 50] [46, 87, 3, 49, 78, 24, 65, 48, 97, 46, 3, 4] [75, 35, 75, 67, 24, 74, 95, 34, 30, 48, 10, 64] ['10:00:03', '10:00:04', '10:00:05', '10:00:06', '10:00:07', '10:00:08', '10:00:09', '10:00:10', '10:00:11', '10:00:12', '10:00:13', '10:00:14'] Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in call return self.func(*args) File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 282, in resize self.resize_event() File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 1819, in resize_event self.callbacks.process(s, event) File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 563, in process proxy(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/matplotlib/cbook.py", line 430, in call return mtd(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 917, in _handle_resize self._init_draw() File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 1193, in _init_draw self._draw_frame(next(self.new_frame_seq())) File "/usr/lib/python2.7/dist-packages/matplotlib/animation.py", line 1215, in _draw_frame a.set_animated(self._blit) AttributeError: 'list' object has no attribute 'set_animated'

+0

あなたは私たちにあなたのコードから期待しているかについての詳細を伝えることはできますか?また、どのバージョンのPythonを使用していますか? – SunSparc

+0

Python 2.7.11。私はグラフが2ではないことを期待していました。最初のサイズの2番目のサイズですが、右から左へスクロールするxの倍数がyの倍数です。 – Cogumelos

+0

私はMac OS X(10.11.5)でPython 2.7.10を使用していましたが、うまくいきました。 – SunSparc

答えて

0

はアニメーションがまだ動作していないが、少なくとも私は1つのグラフがあります。

 from scipy import stats 
    import numpy as np 
    import random 
    import matplotlib.pyplot as plt 
    import matplotlib.dates as md 
    from matplotlib import animation 
    import dateutil 
    from datetime import datetime, timedelta 
    import time 

    X = ['10:00:01', '10:00:02', '10:00:03', '10:00:04', '10:00:05', '10:00:06', '10:00:07', '10:00:08', '10:00:09', '10:00:10', '10:00:11', '10:00:12'] 
    XX = X[11] 

    DATES = [dateutil.parser.parse(s) for s in X] 
    Y1 = [90, 60, 45, 34, 56, 66, 71, 24, 46, 12, 64, 45] 
    Y2 = [45, 70, 25, 84, 96, 26, 41, 28, 76, 52, 34, 55] 
    Y3 = [40, 82, 46, 87, 03, 49, 78, 24, 65, 48, 97, 46] 
    Y4 = [21, 54, 75, 35, 75, 67, 24, 74, 95, 34, 30, 48] 

    fig = plt.figure(figsize=(18,10), dpi=100) 
    plt.subplot(1, 1, 1) 
    plt.rc('font', family='serif', size=13) 
    plt_data = range(0, 100) 
    plt.subplots_adjust(bottom = 0.0) 
    plt.xticks(rotation = 35) 

    ax=fig.gca() 
    ax.set_xticks(DATES) 
    xfmt = md.DateFormatter('%H:%M:%S') 
    ax.xaxis.set_major_formatter(xfmt) 
    plt.gcf().autofmt_xdate() 

    plt.plot(DATES, Y1, color="blue", linewidth=2.0, linestyle="-") 
    plt.plot(DATES, Y2, color="red", linewidth=2.0, linestyle="-") 
    plt.plot(DATES, Y3, color="green", linewidth=2.0, linestyle="-") 
    plt.plot(DATES, Y4, color="cyan", linewidth=2.0, linestyle="-") 

    plt.xlabel('$\Delta t$ $(s)$', fontsize=20) 
    plt.ylabel('$\Delta p$ $(hPa)$', fontsize=20) 
    plt.autoscale(enable = True, axis = u'both', tight = False) 
    plt.grid(True) 
    fig.show() 

    def init(): 
     global XX 
     global Y1 
     global y2 
     global y3 
     global y4 
     global X 
     global DATES 

     X = ['10:00:01', '10:00:02', '10:00:03', '10:00:04', '10:00:05', '10:00:06', '10:00:07', '10:00:08', '10:00:09', '10:00:10', '10:00:11', '10:00:12'] 
     XX = X[11] 

     DATES = [dateutil.parser.parse(s) for s in X] 
     Y1 = [90, 60, 45, 34, 56, 66, 71, 24, 46, 12, 64, 45] 
     Y2 = [45, 70, 25, 84, 96, 26, 41, 28, 76, 52, 34, 55] 
     Y3 = [40, 82, 46, 87, 03, 49, 78, 24, 65, 48, 97, 46] 
     Y4 = [21, 54, 75, 35, 75, 67, 24, 74, 95, 34, 30, 48] 
     lines = [] 
     lines.append(plt.plot(DATES, Y1, color="blue", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y2, color="red", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y3, color="green", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y4, color="cyan", linewidth=2.0, linestyle="-"),) 
     return tuple(lines) 

    def animate(i): 
     global XX 
     global Y1 
     global y2 
     global y3 
     global y4 
     global X 
     global DATES 

     Y1.append(int(random.random() * 100)) 
     Y2.append(int(random.random() * 100)) 
     Y3.append(int(random.random() * 100)) 
     Y4.append(int(random.random() * 100)) 
     del Y1[0] 
     del Y2[0] 
     del Y3[0] 
     del Y4[0] 

     TOP_DT1 = datetime.strptime(XX, '%H:%M:%S') 
     TOP_DT1 = TOP_DT1 + timedelta(seconds=1) 
     XX = TOP_DT1.strftime('%H:%M:%S') 
     X.append(XX) 
     del X[0] 
     print Y1, Y2, Y3, Y4, X 

     lines = [] 
     lines.append(plt.plot(DATES, Y1, color="blue", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y2, color="red", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y3, color="green", linewidth=2.0, linestyle="-"),) 
     lines.append(plt.plot(DATES, Y4, color="cyan", linewidth=2.0, linestyle="-"),) 

     fig.canvas.draw() 
     plt.pause(0.0001) 

     return tuple(lines) 

    ani = animation.FuncAnimation(fig, animate, interval=50, blit=True) 
    plt.show() 

< \ code>の