I am making a simulator that I want to use blit on for higher performance. For 2d scatterplots I can very safely use canvas.blit()
to update my plots. But the same is not true for a 3d scatter plot.
This post:
Matplotlib 3D scatter animations
Says that the problem is specifically linked to scatter plots in 3d. I dug into the source code but did not find much(mostly due to lack of experience). Can anyone help me find out a way to allow for blitting of scatter plots in 3d?
Below you will find code that tries to blit a scatter plot:
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()
Now you will find code that follows the same structure with a different artist:
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()