Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib+;blit&x2B;设置\u dpi=不重新绘制_Python_Matplotlib - Fatal编程技术网

Python matplotlib+;blit&x2B;设置\u dpi=不重新绘制

Python matplotlib+;blit&x2B;设置\u dpi=不重新绘制,python,matplotlib,Python,Matplotlib,我正在尝试通过替换以下内容来加速我的程序: canvas.draw() 与: …但是当我使用set\u dpi()时,我没有得到任何重绘: 有办法解决吗 完整源代码: from __future__ import print_function from random import uniform from numpy.random import rand import matplotlib matplotlib.use('Qt4Agg') import matplotlib.pyplot a

我正在尝试通过替换以下内容来加速我的程序:

canvas.draw()
与:

…但是当我使用set\u dpi()时,我没有得到任何重绘:

有办法解决吗

完整源代码:

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()
from\uuuuu future\uuuuu导入打印功能
从随机导入制服
从numpy.random导入rand
导入matplotlib
matplotlib.use('Qt4Agg')
将matplotlib.pyplot作为plt导入
从matplotlib.transforms导入Bbox
从PyQt4.QtCore导入QT_版本_STR
从PyQt4.Qt导入PYQT\u版本\u STR
从sip导入sip\u版本\u STR
导入系统
打印(“matplotlib版本:”,matplotlib.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“matplotlib后端:”,matplotlib.get_backend())#Qt4Agg
打印(“Qt版本:”,Qt#U版本#STR)#4.8.2
打印(“PyQt版本:”,PyQt#U版本#STR)#4.9.4
打印(“SIP版本:”,SIP#U版本#STR)#4.13.3
打印(“Python版本:”,sys.version)#2.7.3(默认值,2012年4月10日,23:31:26)[MSC v.1500 32位(英特尔)]
图=plt.gcf()
canvas=figure.canvas
轴=plt.gca()
def弹跳(c,v):
c+=v
如果c<0.0:
v=绝对值(v)
elif c>1.0:
v=-abs(v)
返回c,v
def移动(x、y、矢量):
vx,vy=向量
x、 vx=反弹(x,vx)
y、 vy=反弹(y,vy)
向量=[vx,vy]
返回x,y,向量
def动画_轴(轴、矢量0、矢量1):
bbox=轴。获取位置()
[[x0,y0],[x1,y1]]=bbox.get_points()
x0,y0,新向量0=移动(x0,y0,向量0)
x1,y1,新向量1=移动(x1,y1,向量1)
向量0[0]=新的_向量0[0]
向量0[1]=新的向量0[1]
向量1[0]=新的_向量1[0]
向量1[1]=新的向量1[1]
bbox=bbox([[x0,y0],[x1,y1]]
轴。设置位置(bbox)
def更新(轴、矢量0、矢量1):
设置_轴的动画(轴、向量0、向量1)
dpi=图。获取dpi()
dpi+=1
打印(“dpi=”,dpi)
图.设置dpi(dpi)
#canvas.draw()
图.画师(图)
blit(Bbox([50,50],[550400]])#(这是一个测试)
轴线图(兰特(5))
计时器=画布。新计时器(间隔=1)
向量0=[均匀(-0.01,0.01),均匀(-0.01,0.01)]
向量1=[均匀(-0.01,0.01),均匀(-0.01,0.01)]
timer.add_回调(更新、轴、向量0、向量1)
timer.start()
plt.show()

为什么要在更新过程中更改dpi?我下意识的反应是,更改dpi+blitting是不兼容的,因为更改dpi会更改最终的人物空间->屏幕空间变换,因此以不同dpi绘制的艺术家不能彼此重叠。我想通过更改人物的dpi来进行缩放,但当人物变大时,使用canvas.draw()会非常慢。如果它与blit()不兼容,我将找到另一种方法。谢谢你的回答。
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()