Python 如何加速Matplotlib?
我不熟悉Matplotlib,这就是为什么可能有一种更有效的方法来运行我的程序 它是用不同的颜色(取决于某些因素)绘制一组点。它在当前颜色状态的循环中不断生成新图片。 基本上是这样的:Python 如何加速Matplotlib?,python,python-2.7,matplotlib,Python,Python 2.7,Matplotlib,我不熟悉Matplotlib,这就是为什么可能有一种更有效的方法来运行我的程序 它是用不同的颜色(取决于某些因素)绘制一组点。它在当前颜色状态的循环中不断生成新图片。 基本上是这样的: import matplotlib.pyplot as plt def getColour(): #calculate some stuff with x and y and the changing factors while True: fig = plt.figure(figsize=(17,1
import matplotlib.pyplot as plt
def getColour():
#calculate some stuff with x and y and the changing factors
while True:
fig = plt.figure(figsize=(17,10))
plt.scatter(x, y , c=getColour())
plt.show()
plt.close(fig)
我也在试用clf()。然而,这根本没有改变速度。有人有想法吗?我做错了什么
谢谢大家!
编辑:
目标是每次循环时生成一张图片。由于我的程序运行速度很慢,我的问题是是否有办法让它运行得更快。
我正在使用python 2.7,类似于动画:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
ms_between_frames = 100
n_points = 100
x = np.arange(n_points, dtype=float) #EDIT
y = np.random.random(n_points)
z = np.random.random(n_points)
def getColour(x, y, z):
c = np.empty((len(x),3))
for i in range(len(x)):
c[i] = [x[i]/n_points, z[i], 1.-z[i]]
return c
def update(frame_number):
global x, y
z = np.random.random(n_points)
c = getColour(x, y, z)
graph.set_color(c)
fig = plt.figure(figsize=(17,10))
ax = fig.add_subplot(111)
graph = ax.scatter(x, y , c=getColour(x, y, z))
animation = FuncAnimation(fig, update, interval=ms_between_frames)
plt.show()
编辑:使x
保持浮动,这样getcolor中的除法就不会返回0(也可以使/float(n_点)
)
顺便说一句,根据您需要的参数,应该可以只定义一个函数来更新颜色,以避免调用开销。这个问题缺少足够的问题描述。期望的结果是什么?问题是什么?您运行此程序的环境是什么?您可能需要查看,因为这可能是您正在尝试执行的操作。然而,我必须同意@ImportanceOfBeingErnest——这是一个太模糊的问题,你真正想实现什么?循环的速度主要取决于三件事。(1) ColorComputing交付新颜色所需的时间(2)绘制点所需的时间(主要是有多少点的问题)(3)用户关闭窗口以弹出下一个窗口所需的时间。显然,我们不能根据(1)来判断。我们也没有关于点数的信息,所以我们不能对(2)说什么。这就剩下了(3),但是如何提高人类的反应性在这里是离题的。你可以尝试类似的方法。可能是重复的