2017-06-20 10 views
0

パラメータ値を変更したり定期的にグラフを更新したい。私は定期的に再実行し、すべてのA/B/Cが更新される時間やにプロット機能を取得するにはどうすればよいパラメータの変更に基づいてグラフを自動的に更新する

a=0` 
b=50 
c=100 

def sine(x,y,l): 
    A=numpy.zeros(l) 
    for i in range(l): 
     A[i]=numpy.sin(2*math.pi*(i+x)/y) 
    return A 


def plot(l): 
    matplotlib.pyplot.clf() 
    matplotlib.pyplot.plot(l) 

plot(sine(a,b,c))` 

`

:私は、次のコードを持っていますか?

答えて

2

ここだから、いくつかのこと、あなたがそれらをループすることなく、ndarrays上で動作numpyのufuncsの適切な使用について学ぶ必要があります。

https://docs.scipy.org/doc/numpy-1.12.0/reference/ufuncs.html

第二に、あなたは場所を持っている必要がありますイベントは、例えば、トリガーされる更新:

http://openbookproject.net/thinkcs/python/english3e/events.html

このコードには、このような例が存在しないので、私はちょうどそれが起こっていますが、知っていると仮定します。それが起こるところでは、データを更新するために行のハンドルが必要です。ここで

https://matplotlib.org/api/lines_api.html#matplotlib.lines.Line2D.set_xdata https://matplotlib.org/api/lines_api.html#matplotlib.lines.Line2D.set_ydata

私はあなたがやろうとしていると考えているものを強調し、いくつかのサンプルコードです:

import numpy as np 
import matplotlib.pyplot as plt 

l = 100 
x = np.linspace(0, 2 * np.pi, l) 
y = np.sin(x) 

fig, ax = plt.subplots(1, 1) # generate figure and axes objects so that we have handles for them 
curve = ax.plot(x, y)[0] # capture the handle for the lines object so we can update its data later 

# now some kind of event happens where the data is changed, and we update the plot 
y = np.cos(x) # the data is changed! 
curve.set_ydata(y) 

# necessary if you are just executing this as a script 
# this example is a little more clear if executed stepwise in ipython 
plt.show(block=True) 
+0

はufuncsがループするよりも効率的ですか。 – Dole

+0

はい。より洗練された、より簡潔なコードを除いて、計算全体では、1つの関数呼び出しで計算全体が処理されるため、おそらくそれ以上の速さはありません。実際には、配列が数多くある場合のループは、ループで行う多くの関数呼び出しのオーバーヘッドのために、Pythonリストをループするよりも遅くなる可能性があります –