Python 是否已经有一种方法允许对matplotlib 3d散点图进行分幅显示?
我正在制作一个模拟器,我想使用blit来提高性能。对于2d散点图,我可以非常安全地使用Python 是否已经有一种方法允许对matplotlib 3d散点图进行分幅显示?,python,matplotlib,Python,Matplotlib,我正在制作一个模拟器,我想使用blit来提高性能。对于2d散点图,我可以非常安全地使用canvas.blit()更新我的图。但对于三维散点图来说,情况并非如此。 本帖: 说这个问题与3d中的散点图有关。我深入研究了源代码,但没有找到太多(主要是由于缺乏经验)。有人能帮我找到一种方法,允许在3d中进行散点图的布点吗 在下面,您将找到尝试blit散点图的代码: import matplotlib.pyplot as plt import numpy as np import matplotlib a
canvas.blit()
更新我的图。但对于三维散点图来说,情况并非如此。
本帖:
说这个问题与3d中的散点图有关。我深入研究了源代码,但没有找到太多(主要是由于缺乏经验)。有人能帮我找到一种方法,允许在3d中进行散点图的布点吗
在下面,您将找到尝试blit散点图的代码:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
#create figure
fig = plt.gcf()
ax = fig.add_subplot(111, projection='3d')
lim=(-20,20)
ax.set(xlim=lim,ylim=lim, zlim = lim)
canvas = fig.canvas
t = canvas.new_timer()
fig2, ax2 = plt.subplots()
butt = mpl.widgets.Button(ax2, "play")
#Save reference figure
canvas.draw() #To make sure there is a figure
background = canvas.copy_from_bbox(ax.get_window_extent(ax.figure.canvas.renderer))
artist = ax.scatter([1],[1],[1], marker = "o")
def activate(event):
global artist
global canvas
global fig
global ax
global background
artist.set_animated(True)
for x in range(300):
artist._offsets3d =[[x/10], [1], [1]]
#artist.set_data(a[0], a[1])
#artist.set_3d_properties([1])
ax.draw_artist(artist)
canvas.blit(ax.bbox)
canvas.restore_region(background)
canvas.flush_events()
artist.set_animated(False)
canvas.draw()
canvas.flush_events()
butt.on_clicked(activate)
plt.show()
现在,您将发现使用不同艺术家的代码遵循相同的结构:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
#create figure
fig = plt.gcf()
ax = fig.add_subplot(111, projection='3d')
lim=(-20,20)
ax.set(xlim=lim,ylim=lim, zlim = lim)
canvas = fig.canvas
t = canvas.new_timer()
fig2, ax2 = plt.subplots()
butt = mpl.widgets.Button(ax2, "play")
#Save reference figure
canvas.draw() #To make sure there is a figure
background = canvas.copy_from_bbox(ax.get_window_extent(ax.figure.canvas.renderer))
artist = ax.scatter([1],[1],[1], marker = "o")
def activate(event):
global artist
global canvas
global fig
global ax
global background
artist.set_animated(True)
for x in range(300):
artist._offsets3d =[[x/10], [1], [1]]
#artist.set_data(a[0], a[1])
#artist.set_3d_properties([1])
ax.draw_artist(artist)
canvas.blit(ax.bbox)
canvas.restore_region(background)
canvas.flush_events()
artist.set_animated(False)
canvas.draw()
canvas.flush_events()
butt.on_clicked(activate)
plt.show()
由于问题在于
matplotlib.pyplot.scatter
(返回PathCollection
)而不是matplotlib.pyplot.plot
(返回Line2D
)您应该能够像这样初始化artist
artist=ax.plot([1],[1],[1],linestyle=“none”,marker=“o”)
然后应该允许blit-至少在使用FuncAnimation
时是这样,表面上看canvas.blit()
也应该可以
请在链接问题的中注意此点。添加
artist.do_3d_projection(fig._cachedRenderer)
之后
解决了问题。我用FuncAnimation对每一帧进行计时,对于散点图,使用
blit=True
比使用blit=False
进行绘制花费的时间更长,这意味着使用canvas.draw()
更好。此外,使用plt.plot()
可能会限制某些动画,在这些动画中可能需要实现彩色贴图和其他特定于散点图的功能。
artist._offsets3d = [[x/10], [1], [1]]