Hello.
I've poste here a question but no one seems to have an hint to give so I
repeat my question here.
In the attached Python 3 code, I draw n**2 squares in black and then I
dynamically change in a random way the colors of some squares.
The problem I met is that at each new random choice, I redraw all the
squares. I know that it is stupid for large number of squares.
Is there a way to change some propriety of one square, and then to ask to
matplotlib to redraw only what have changed ? I'm looking for something
similar to `blint` for animations.
Christophe BAL
#!/usr/bin/env python3
from random import randint, choice
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.colors as colors
import matplotlib
# ------------------- #
# -- SOME SETTINGS -- #
# ------------------- #
matplotlib.rc_context({'toolbar':False})
background_color = "black"
colors = [
'gray',
'red',
'blue',
'magenta',
'green',
'cyan',
'yellow',
]
# ---------------- #
# -- DIMENSIONS -- #
# ---------------- #
# Not too slow
width = 16
height = 16
# Very slow
width = 64
height = 64
# ------------ #
# -- FIGURE -- #
# ------------ #
fig = plt.figure()
plt.axes(
xlim = (0, width),
ylim = (0, height),
aspect = 'equal'
)
plt.axis('off')
plotgrid = []
for line in range(height):
plotgrid.append([])
for col in range(width):
rect = mpatches.Rectangle(
(col, line), 1, 1,
facecolor = background_color,
edgecolor = background_color
)
plt.gca().add_patch(rect)
plotgrid[-1].append(rect)
# ---------------------- #
# -- BINDINGS - TIMER -- #
# ---------------------- #
def update():
global colors, width, height
x = randint(0, width - 1)
y = randint(0, height - 1)
plotgrid[y][x].set_facecolor(choice(colors))
plt.draw()
def onkey(event):
if event.key == "q":
sys.exit()
fig.canvas.mpl_connect('key_press_event', onkey)
timer = fig.canvas.new_timer(interval = 1)
timer.add_callback(update)
timer.start()
plt.show()
------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users