2017-07-18 71 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

**我固定它。谢谢 –