matplotlib + blit + set_dpi =没有重绘

时间:2016-01-25 10:59:55

标签: python matplotlib

我试图通过替换来加速我的程序:

canvas.draw()

使用:

figure.draw_artist(figure)
canvas.blit(Bbox([[50, 50], [550, 400]])) # (this is a test)

...但是当我使用 set_dpi()时,我没有重绘:

No redraw

有没有办法解决它?

完整源代码:

from __future__ import print_function
from random import uniform
from numpy.random import rand
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
from PyQt4.QtCore import QT_VERSION_STR
from PyQt4.Qt import PYQT_VERSION_STR
from sip import SIP_VERSION_STR
import sys

print("matplotlib version:", matplotlib.__version__) # 1.1.1
print("matplotlib backend:", matplotlib.get_backend()) # Qt4Agg
print("Qt version:", QT_VERSION_STR) # 4.8.2
print("PyQt version:", PYQT_VERSION_STR) # 4.9.4
print("SIP version:", SIP_VERSION_STR) # 4.13.3
print("Python version:", sys.version) # 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]

figure = plt.gcf()
canvas = figure.canvas
axes = plt.gca()

def bounce(c, v):
    c += v
    if c < 0.0:
        v = abs(v)
    elif c > 1.0:
        v = -abs(v)
    return c, v

def move(x, y, vector):
    vx, vy = vector
    x, vx = bounce(x, vx)
    y, vy = bounce(y, vy)
    vector = [vx, vy]
    return x, y, vector

def animate_axes(axes, vector0, vector1):        
    bbox = axes.get_position()
    [[x0, y0], [x1, y1]] = bbox.get_points()
    x0, y0, new_vector0 = move(x0, y0, vector0)
    x1, y1, new_vector1 = move(x1, y1, vector1)
    vector0[0] = new_vector0[0]
    vector0[1] = new_vector0[1]
    vector1[0] = new_vector1[0]
    vector1[1] = new_vector1[1]
    bbox = Bbox([[x0, y0], [x1, y1]])
    axes.set_position(bbox)

def update(axes, vector0, vector1):
    animate_axes(axes, vector0, vector1)

    dpi = figure.get_dpi()
    dpi += 1
    print("dpi =", dpi)
    figure.set_dpi(dpi)

#     canvas.draw()
    figure.draw_artist(figure)
    canvas.blit(Bbox([[50, 50], [550, 400]])) # (this is a test)

axes.plot(rand(5))
timer = canvas.new_timer(interval=1)
vector0 = [uniform(-0.01, 0.01), uniform(-0.01, 0.01)]
vector1 = [uniform(-0.01, 0.01), uniform(-0.01, 0.01)]
timer.add_callback(update, axes, vector0, vector1)
timer.start()

plt.show()

0 个答案:

没有答案