I'm using matplotlib plotting in python GUI using animation. And below is the code
import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.toolbar = NavigationToolbar(self.canvas, self)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
self.setLayout(layout)
self.ax=self.figure.add_subplot(111)
plt.autoscale(enable=True, axis='both', tight=None #for auto scaling
self.data = [500, -500, 501, -502,.... 623] #some list of data
self.ax = plt.gca()
self.ax.grid()
self.sc = self.ax.scatter(self.data[::2], self.data[1::2]
def plot(self, a):
for i in range(len(self.data)):
self.data[i] = int(self.data[i])+5
self.sc.set_offsets(np.c_[self.data[::2], self.data[1::2]])
self.canvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
ani = matplotlib.animation.FuncAnimation(main.figure, main.plot,
frames=4, interval=100, repeat=True)
main.show()
sys.exit(app.exec_())
I'm updating the plot using set_offsets inside the plot function which is called by animation. When plot values is kept increasing and plotting is done, the plot goes out of figure. So i used autoscale(). But its not working. Still axes remains fixed and plots goes out of view.