S
S
Stepan2020-10-30 14:30:20
Python
Stepan, 2020-10-30 14:30:20

How to redraw a plot in matplotlib?

There is a program that draws the trajectory of a projectile. When the program starts, everything works fine, but I need to redraw this graph. I tried to clear the graph, delete it - nothing helped . Tell me

, how to redraw the graph? The draw1 function should just redraw it

plt.ion()
V0=10 #Нач скорость
a=45 #Угол
m=1 #Масса
k=1 #Коэфф сопротивления воздуха

class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self) 

        m = PlotCanvas(self, width=7, height=5.5)
        m.move(400,10)

        self.pushButton.clicked.connect(self.draw1)

    def draw1(self):
        global V0
        V0=100
        PlotCanvas()
        
        
class PlotCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)
        rect = fig.patch 
        rect.set_facecolor('#19232d')
        fig.clear()
        FigureCanvas.setSizePolicy(self,
                QSizePolicy.Expanding,
                QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()

    def Lenght(self,V0, a, m, k): #Расчёт траектории (совокупность координат (x,y) )
        ...
        return x_m, y
    def plot(self):
        global V0, a, m, k,line1
        x_m, y = self.Lenght(V0, a, m, k)  #Передача данных в функцию,которая вернёт данные для построения графика
        ax = self.figure.add_subplot(111) 

       #Отрисовка
        ax.plot(x_m, y, color="#ffab03")
       
        self.draw()
        print(1)

def main():
    app = QtWidgets.QApplication(sys.argv)  
    window = ExampleApp()  
    app.setStyleSheet(qdarkstyle.load_stylesheet(qt_api='pyqt5'))
    window.show()  
    app.exec_()  

if __name__ == '__main__':  
    main()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2020-10-30
@dmshar

What you want to do is called "animated graphics".
An animated plot in Matplotlib is created using FuncAnimation from the matplotlib.animation animation module. So use it, and do not reinvent your wheel.

V
Viktor T2, 2020-10-30
@Viktor_T2

# --------- plot drawing -------------
figure.clf() # overwrite old plots
ax = figure.add_subplot(111) # new plot drawing area

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question