matplotlib中具有散点图并使用set_offsets的动画:图形的自动缩放不起作用 [英] Animation in matplotlib with scatter and using set_offsets: Autoscale of figure is not working
问题描述
我正在使用动画在python GUI中使用matplotlib绘图。下面是代码
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_())
我正在使用plot函数中的set_offsets更新图被动画调用。当绘图值保持增加并完成绘图时,绘图将超出范围。所以我用了autoscale()。但是它不起作用。静止轴保持固定,并且图不可见。
请帮助我进行自动缩放。
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. Kindly help me to do autoscale in figure. Thanks in advance.
推荐答案
问题是,自动缩放轴时不会考虑散点偏移。这可能是错误,也可能是所需的功能;在任何情况下,两种解决方法是:
The problem is that the scatter offsets are not taken into account when the axes are autoscaled. This may be a bug, or a desired feature; in any case two workarounds would be:
在许多情况下可以接受的一种解决方法是使用线图 plt.plot
而不是 plt.scatter
。在这种情况下,可以使用 ax.relim
后跟 ax.autoscale_view()
自动缩放轴。
One workaround which may in many cases be acceptable is to use a line plot plt.plot
instead of plt.scatter
. In this case the axes can be autoscaled using ax.relim
followed by ax.autoscale_view()
.
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
fig, ax = plt.subplots()
ax.grid()
data = np.cumsum(np.random.normal(size=100)) #some list of data
sc, = ax.plot(data[::2], data[1::2], marker="o", ls="") # set linestyle to none
def plot(a, data):
data += np.cumsum(np.random.normal(size=100)+3e-2)
sc.set_data(data[::2], data[1::2])
ax.relim()
ax.autoscale_view(True,True,True)
ani = matplotlib.animation.FuncAnimation(fig, plot, fargs=(data,),
frames=4, interval=100, repeat=True)
plt.show()
使用散点
并以编程方式设置限制
如果上述不能使用(例如,因为散点应该具有不同的大小或颜色),则需要根据数据更新限制。
using scatter
and set limits programmatically
If the above cannot be used (e.g. because the scatter points should have different size or color), one would need to update the limits depending on the data.
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
fig, ax = plt.subplots()
data = np.cumsum(np.random.normal(size=100)) #some list of data
ax.grid()
sc = ax.scatter(data[::2], data[1::2], c=data[1::2])
def plot(a, data):
data += np.cumsum(np.random.normal(size=100)+3e-2)
X = np.c_[data[::2], data[1::2]]
sc.set_offsets(X)
# manually relim:
xmin=X[:,0].min(); xmax=X[:,0].max()
ymin=X[:,1].min(); ymax=X[:,1].max()
ax.set_xlim(xmin-0.1*(xmax-xmin),xmax+0.1*(xmax-xmin))
ax.set_ylim(ymin-0.1*(ymax-ymin),ymax+0.1*(ymax-ymin))
ani = matplotlib.animation.FuncAnimation(fig, plot, fargs=(data,),
frames=4, interval=100, repeat=True)
plt.show()
< a href = https://i.stack.imgur.com/VIrPj.gif rel = noreferrer>
这篇关于matplotlib中具有散点图并使用set_offsets的动画:图形的自动缩放不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!