2016-11-03 30 views
0

私は、パラメータが変更されたときの相違を表示するために時間の経過とともに変化するプロットを扱うために使用されています。ここで私は簡単な例を提供しますMatplotlibでヒストグラムを削除する方法

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.grid(True) 

x = np.arange(-3, 3, 0.01) 

for j in range(1, 15): 
    y = np.sin(np.pi*x*j)/(np.pi*x*j) 
    line, = ax.plot(x, y) 
    plt.draw() 
    plt.pause(0.5) 
    line.remove() 

あなたは明らかに、パラメータが増加するほど狭く狭くなることが分かります。 カウンタプロットで仕事をしたいのであれば、 "ライン"の後にカンマを削除するだけでいいです。私の理解では、この小さな変更は、カウンタプロットがもうタプルの要素ではないという事実から来ていますが、カウンタープロットとしての属性は利用可能なすべてのスペースを完全に「満たす」だけです。

しかし、ヒストグラムを削除(およびプロット)する方法がないようです。 Infact if type

import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.grid(True) 

x = np.random.randn(100) 

for j in range(15): 
    hist, = ax.hist(x, 40)*j 
    plt.draw() 
    plt.pause(0.5) 
    hist.remove() 

カンマを入力するかどうかは関係ありません。エラーメッセージが表示されます。 これで助けてくれませんか?

答えて

1

ax.histあなたの考えを返すものではありません。

hist(ipythonシェルでax.hist?を経由してアクセス)のドキュメンテーション文字列のリターンセクションは述べて:

Returns 
------- 
n : array or list of arrays 
    The values of the histogram bins. See **normed** and **weights** 
    for a description of the possible semantics. If input **x** is an 
    array, then this is an array of length **nbins**. If input is a 
    sequence arrays ``[data1, data2,..]``, then this is a list of 
    arrays with the values of the histograms for each of the arrays 
    in the same order. 

bins : array 
    The edges of the bins. Length nbins + 1 (nbins left edges and right 
    edge of last bin). Always a single array even when multiple data 
    sets are passed in. 

patches : list or list of lists 
    Silent list of individual patches used to create the histogram 
    or list of such list if multiple input datasets. 

ですから、あなたの出力を展開する必要があります。

ここ
counts, bins, bars = ax.hist(x, 40)*j 
_ = [b.remove() for b in bars] 
+0

私たちはほとんどそこにいる:もし私が「p = [b.remove()for b for bars]」という行を「hist.remove()」という行に置きます。エラーのメッセージは表示されず、1つのループでのみ動作します。つまり、「range (1,15)」、「範囲(1,2)」である。しかし、実際のループのためには何かを変える必要があります。 –

+1

@StefanoFedele 'ax.hist(...)* j'は私には意味がありません。あなたはそれで何をしようとしていますか? –

0

への正しい方法matplotlibでヒストグラムを繰り返し描画し、削除する

import matplotlib.pyplot as plt 
import numpy as np 


fig = plt.figure(figsize = (20, 10)) 
ax = fig.add_subplot(111) 
ax.grid(True) 


for j in range(1, 15): 
    x = np.random.randn(100) 
    count, bins, bars = ax.hist(x, 40) 
    plt.draw() 
    plt.pause(1.5) 
    t = [b.remove() for b in bars]