2017-02-02 102 views
2

matplotlibの図を生成するための単純なPython関数を書いた。別のスクリプトからplotDataを複数回呼び出しますが、新しいプロットが生成されるたびに呼び出されます。私が望むのは、常にデータ変更の間にサブプロットをクリアするために、subplot.clear()のようなものを1つだけ持つことです。図のサブプロットをクリアするmatplotlib python

plotDataの外部から図を識別する方法が必要です。新しいデータのプロットをクリアすることができます。これを達成する最良の方法は何でしょうか?

## Plot Data Function 
def plotData(self): 

     # Setup figure to hold subplots 
     f = Figure(figsize=(10,8), dpi=100) 

     # Setup subplots 
     subplot1=f.add_subplot(2,1,1) 
     subplot2=f.add_subplot(2,1,2) 

     # Show plots 
     dataPlot = FigureCanvasTkAgg(f, master=app) 
     dataPlot.show() 
     dataPlot.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1) 

答えて

5

を使用することができます。 プロットを更新するには、これを行う関数が必要です。私はこの関数をplotDataと呼ぶでしょう。その前にプロットを設定する必要もあります。これはあなたが現在持っているものですplotData。それで、名前をgeneratePlotに変更しましょう。

class SomeClass(): 
    ... 

    def generatePlot(self): 
     # Setup figure to hold subplots 
     f = Figure(figsize=(10,8), dpi=100) 

     # Setup subplots 
     self.subplot1=f.add_subplot(2,1,1) 
     self.subplot2=f.add_subplot(2,1,2) 

     # Show plots 
     dataPlot = FigureCanvasTkAgg(f, master=app) 
     dataPlot.show() 
     dataPlot.get_tk_widget().pack(side=RIGHT, fill=BOTH, expand=1) 

    ## Plot Data Function 
    def plotData(self, data, otherdata): 
     #clear subplots 
     self.subplot1.cla() 
     self.subplot2.cla() 
     #plot new data to the same axes 
     self.subplot1.plot(data) 
     self.subplot2.plot(otherdata) 

generatePlotを最初に1回だけ呼び出す必要があります。その後、いつでも新しいデータでプロットを更新することができます。

3

私はどこに問題がある、私は完全に理解している場合はわからない

subplot.cla() # which clears data but not axes 
subplot.clf() # which clears data and axes 
+0

また、figureとsubplotsをself.figとself.subplotsなどとして定義すると、それらが含まれているクラスインスタンスからアクセスできます。 –

+0

「self」が定義されている場合、どのようにFigureを検索しますか?修正コードが適用される前に、何らかの形で再配置する必要があります。 –

関連する問題