2017-07-18 120 views
0

我試圖創建一個更新時,給定一組點([x,y])的情節,但該圖卡住了第一個情節點,並贏得了不會繪製其餘的數據。我打了一個函數調用,但它在第一次調用時卡住了。我需要能夠給函數多組單個x和y值,並將它們繪製成圖形。使用matplotlib繪圖不會刷新繪製新點

這是我到目前爲止的代碼。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 
from numpy import * 
from time import sleep 
import random as rd 

class graphUpdater(): 

    def __init__(self): 
     # Initialize arrays to be plotted 
     self.xs = [] 
     self.ys = [] 

     style.use('fivethirtyeight') # Figure Style 
     self.fig = plt.figure() # Initialize figure 
     self.ax1 = self.fig.add_subplot(111) # Create a subplot 
     # Ensure the figure auto-scales to fit all points. Might be overkill 
     self.ax1.set_autoscalex_on(True) 
     self.ax1.set_autoscaley_on(True) 
     self.ax1.set_autoscale_on(True) 
     self.ax1.autoscale(enable = True, axis = 'both', tight = False) 
     self.ax1.autoscale_view(False, True, True) 

    # Function that plots the arrays xs and ys. Also plots a linear regression of the data 
    def plotPoint(self): 
     self.ax1.clear() # Clears previous values to save memory 
     xp = linspace(min(self.xs), max(self.xs)) # x-range for regression 
     if(len(self.xs) > 1): # Conditional for regression, can't linearise 1 point 
      p1 = polyfit(self.xs, self.ys, 1) # Get the coefficients of the polynomial (slope of line) 
      self.ax1.plot(xp, polyval(p1, xp)) # Plot the line 
     self.ax1.plot(self.xs, self.ys, "+") # Plot the raw data points 
     self.ax1.set_xlabel('(L/A)*I') # Axis and title labels 
     self.ax1.set_ylabel('V') 
     self.ax1.set_title('DC Potential Drop') 

    def appendPlot(self, x, y): 
     self.xs.append(float(x)) # Append xs with x value 
     self.ys.append(float(y)) # Append ys with y value 
     self.plotPoint() # Call the plotPoint function to plot new array values 
     plt.show(block=False) # Plot and release so graphs can be over written 

# Call the function 
plsWork = graphUpdater() # I'm very hopeful 
i = 0 
while(i < 50): 
    plsWork.appendPlot(i, rd.randint(0, 20)) 
    i += 1 
    sleep(0.1) 
quit_case = input("Hit 'Enter' to Quit") # Conditional so the plot won't disappear 

它不能正常工作。如果你在quit_case行放置了一個斷點並在pycharm上的調試器上運行它,它會「適當地」繪製圖形。

回答

1

請勿使用plt.show(block=False)而且請勿使用time.sleep。相反,matplotlib提供了一個animation module,它可以用來避免這裏的問題。

import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
from matplotlib import style 
from numpy import * 
from time import sleep 
import random as rd 
#%matplotlib notebook use in case of running this in a Jupyter notebook 

class graphUpdater(): 

    def __init__(self): 
     # Initialize arrays to be plotted 
     self.xs = [] 
     self.ys = [] 

     style.use('fivethirtyeight') # Figure Style 
     self.fig = plt.figure() # Initialize figure 
     self.ax1 = self.fig.add_subplot(111) # Create a subplot 
     # Ensure the figure auto-scales to fit all points. Might be overkill 
     self.ax1.set_autoscalex_on(True) 
     self.ax1.set_autoscaley_on(True) 
     self.ax1.set_autoscale_on(True) 
     self.ax1.autoscale(enable = True, axis = 'both', tight = False) 
     self.ax1.autoscale_view(False, True, True) 

    # Function that plots the arrays xs and ys. Also plots a linear regression of the data 
    def plotPoint(self): 
     self.ax1.clear() # Clears previous values to save memory 
     xp = linspace(min(self.xs), max(self.xs)) # x-range for regression 
     if(len(self.xs) > 1): # Conditional for regression, can't linearise 1 point 
      p1 = polyfit(self.xs, self.ys, 1) # Get the coefficients of the polynomial (slope of line) 
      self.ax1.plot(xp, polyval(p1, xp)) # Plot the line 
     self.ax1.plot(self.xs, self.ys, "+") # Plot the raw data points 
     self.ax1.set_xlabel('(L/A)*I') # Axis and title labels 
     self.ax1.set_ylabel('V') 
     self.ax1.set_title('DC Potential Drop') 

    def appendPlot(self, x, y): 
     self.xs.append(float(x)) # Append xs with x value 
     self.ys.append(float(y)) # Append ys with y value 
     self.plotPoint() # Call the plotPoint function to plot new array values 

# Call the function 
plsWork = graphUpdater() # I'm very hopeful 

f = lambda i: plsWork.appendPlot(i, rd.randint(0, 20)) 

ani = animation.FuncAnimation(plsWork.fig, f, frames=50, interval=100, repeat=False) 
plt.show() 
+0

我得到一個運行時錯誤 RuntimeWarning:在true_divide遇到無效值 LHS/=規模 –

+0

**我固定它。謝謝 –