How to update plot without redraw figure












0















I draw grid inside a circle, and when I pick any grid, I will add a rectangle object to circle. But when the grid number is large(over 40000) and there are many rectangles have been added, the plot figure time would be very long.



I think that is because every time I add rectangle I will redraw the figure.
So I think if I can just update figure and not to redraw it, it will be much faster. But, unfortunately, I can not find any api to do it in matplotlib. Does anybody have any idea to solve this problem ? Thank you !



Here's part of my code :



import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

def onclick(event):
"""add Rectangle when click any gird on circle"""
try:
rect = patches.Rectangle((int(event.mouseevent.xdata), int(event.mouseevent.ydata)), 1, 1, facecolor=(.7, .9, .9, .9), edgecolor=(.2, .1, .1,.5))
ax.add_patch(rect)
rect.set_clip_path(circ)
# redraw can update the figure, but it would take long time
fig.canvas.draw()
except Exception as e:
print(e.args)


r = 50 # radius
fig = plt.figure()
ax = plt.gca()
plt.grid(True)

circ = patches.Circle((0, 0), radius=r,
facecolor=(.5, .5, .5), edgecolor='black', picker=5)
ax.add_patch(circ)

# Fill half of circle with rectagles
for x in range(int(-r/2), int(r/2)):
for y in range(int(-r/2), int(r/2)):
if x + y < r * r:
rect = patches.Rectangle((x, y), 1, 1, facecolor=(.7, .5, .5, .5), edgecolor=(.2, .1, .1, .5))
ax.add_patch(rect)
rect.set_clip_path(circ)

fig.canvas.mpl_connect('pick_event', onclick)

# Diable any ticks and labels
plt.tick_params(
axis='both',
left='off',
bottom='off',
labelleft='off',
labelbottom='off'
)
plt.xlim([-r-1, r+1])
plt.ylim([-r-1, r+1])
plt.xticks(np.arange(-r-1, r+1))
plt.yticks(np.arange(-r-1, r+1))
plt.setp(ax.xaxis.get_gridlines(), clip_path=circ)
plt.setp(ax.yaxis.get_gridlines(), clip_path=circ)

plt.axes().set_aspect('equal')
plt.show()









share|improve this question

























  • "Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

    – ImportanceOfBeingErnest
    Jan 2 at 10:52











  • Thanks for your advise. I'll try to use PolyCollection to see if it works.

    – Lester_wu
    Jan 3 at 1:27
















0















I draw grid inside a circle, and when I pick any grid, I will add a rectangle object to circle. But when the grid number is large(over 40000) and there are many rectangles have been added, the plot figure time would be very long.



I think that is because every time I add rectangle I will redraw the figure.
So I think if I can just update figure and not to redraw it, it will be much faster. But, unfortunately, I can not find any api to do it in matplotlib. Does anybody have any idea to solve this problem ? Thank you !



Here's part of my code :



import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

def onclick(event):
"""add Rectangle when click any gird on circle"""
try:
rect = patches.Rectangle((int(event.mouseevent.xdata), int(event.mouseevent.ydata)), 1, 1, facecolor=(.7, .9, .9, .9), edgecolor=(.2, .1, .1,.5))
ax.add_patch(rect)
rect.set_clip_path(circ)
# redraw can update the figure, but it would take long time
fig.canvas.draw()
except Exception as e:
print(e.args)


r = 50 # radius
fig = plt.figure()
ax = plt.gca()
plt.grid(True)

circ = patches.Circle((0, 0), radius=r,
facecolor=(.5, .5, .5), edgecolor='black', picker=5)
ax.add_patch(circ)

# Fill half of circle with rectagles
for x in range(int(-r/2), int(r/2)):
for y in range(int(-r/2), int(r/2)):
if x + y < r * r:
rect = patches.Rectangle((x, y), 1, 1, facecolor=(.7, .5, .5, .5), edgecolor=(.2, .1, .1, .5))
ax.add_patch(rect)
rect.set_clip_path(circ)

fig.canvas.mpl_connect('pick_event', onclick)

# Diable any ticks and labels
plt.tick_params(
axis='both',
left='off',
bottom='off',
labelleft='off',
labelbottom='off'
)
plt.xlim([-r-1, r+1])
plt.ylim([-r-1, r+1])
plt.xticks(np.arange(-r-1, r+1))
plt.yticks(np.arange(-r-1, r+1))
plt.setp(ax.xaxis.get_gridlines(), clip_path=circ)
plt.setp(ax.yaxis.get_gridlines(), clip_path=circ)

plt.axes().set_aspect('equal')
plt.show()









share|improve this question

























  • "Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

    – ImportanceOfBeingErnest
    Jan 2 at 10:52











  • Thanks for your advise. I'll try to use PolyCollection to see if it works.

    – Lester_wu
    Jan 3 at 1:27














0












0








0








I draw grid inside a circle, and when I pick any grid, I will add a rectangle object to circle. But when the grid number is large(over 40000) and there are many rectangles have been added, the plot figure time would be very long.



I think that is because every time I add rectangle I will redraw the figure.
So I think if I can just update figure and not to redraw it, it will be much faster. But, unfortunately, I can not find any api to do it in matplotlib. Does anybody have any idea to solve this problem ? Thank you !



Here's part of my code :



import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

def onclick(event):
"""add Rectangle when click any gird on circle"""
try:
rect = patches.Rectangle((int(event.mouseevent.xdata), int(event.mouseevent.ydata)), 1, 1, facecolor=(.7, .9, .9, .9), edgecolor=(.2, .1, .1,.5))
ax.add_patch(rect)
rect.set_clip_path(circ)
# redraw can update the figure, but it would take long time
fig.canvas.draw()
except Exception as e:
print(e.args)


r = 50 # radius
fig = plt.figure()
ax = plt.gca()
plt.grid(True)

circ = patches.Circle((0, 0), radius=r,
facecolor=(.5, .5, .5), edgecolor='black', picker=5)
ax.add_patch(circ)

# Fill half of circle with rectagles
for x in range(int(-r/2), int(r/2)):
for y in range(int(-r/2), int(r/2)):
if x + y < r * r:
rect = patches.Rectangle((x, y), 1, 1, facecolor=(.7, .5, .5, .5), edgecolor=(.2, .1, .1, .5))
ax.add_patch(rect)
rect.set_clip_path(circ)

fig.canvas.mpl_connect('pick_event', onclick)

# Diable any ticks and labels
plt.tick_params(
axis='both',
left='off',
bottom='off',
labelleft='off',
labelbottom='off'
)
plt.xlim([-r-1, r+1])
plt.ylim([-r-1, r+1])
plt.xticks(np.arange(-r-1, r+1))
plt.yticks(np.arange(-r-1, r+1))
plt.setp(ax.xaxis.get_gridlines(), clip_path=circ)
plt.setp(ax.yaxis.get_gridlines(), clip_path=circ)

plt.axes().set_aspect('equal')
plt.show()









share|improve this question
















I draw grid inside a circle, and when I pick any grid, I will add a rectangle object to circle. But when the grid number is large(over 40000) and there are many rectangles have been added, the plot figure time would be very long.



I think that is because every time I add rectangle I will redraw the figure.
So I think if I can just update figure and not to redraw it, it will be much faster. But, unfortunately, I can not find any api to do it in matplotlib. Does anybody have any idea to solve this problem ? Thank you !



Here's part of my code :



import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np

def onclick(event):
"""add Rectangle when click any gird on circle"""
try:
rect = patches.Rectangle((int(event.mouseevent.xdata), int(event.mouseevent.ydata)), 1, 1, facecolor=(.7, .9, .9, .9), edgecolor=(.2, .1, .1,.5))
ax.add_patch(rect)
rect.set_clip_path(circ)
# redraw can update the figure, but it would take long time
fig.canvas.draw()
except Exception as e:
print(e.args)


r = 50 # radius
fig = plt.figure()
ax = plt.gca()
plt.grid(True)

circ = patches.Circle((0, 0), radius=r,
facecolor=(.5, .5, .5), edgecolor='black', picker=5)
ax.add_patch(circ)

# Fill half of circle with rectagles
for x in range(int(-r/2), int(r/2)):
for y in range(int(-r/2), int(r/2)):
if x + y < r * r:
rect = patches.Rectangle((x, y), 1, 1, facecolor=(.7, .5, .5, .5), edgecolor=(.2, .1, .1, .5))
ax.add_patch(rect)
rect.set_clip_path(circ)

fig.canvas.mpl_connect('pick_event', onclick)

# Diable any ticks and labels
plt.tick_params(
axis='both',
left='off',
bottom='off',
labelleft='off',
labelbottom='off'
)
plt.xlim([-r-1, r+1])
plt.ylim([-r-1, r+1])
plt.xticks(np.arange(-r-1, r+1))
plt.yticks(np.arange(-r-1, r+1))
plt.setp(ax.xaxis.get_gridlines(), clip_path=circ)
plt.setp(ax.yaxis.get_gridlines(), clip_path=circ)

plt.axes().set_aspect('equal')
plt.show()






python-3.x matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 3:33







Lester_wu

















asked Jan 2 at 3:14









Lester_wuLester_wu

815




815













  • "Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

    – ImportanceOfBeingErnest
    Jan 2 at 10:52











  • Thanks for your advise. I'll try to use PolyCollection to see if it works.

    – Lester_wu
    Jan 3 at 1:27



















  • "Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

    – ImportanceOfBeingErnest
    Jan 2 at 10:52











  • Thanks for your advise. I'll try to use PolyCollection to see if it works.

    – Lester_wu
    Jan 3 at 1:27

















"Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

– ImportanceOfBeingErnest
Jan 2 at 10:52





"Update" and "redraw" are the same things in this case. You may however try to blit the new rectangle onto the canvas. Before doing that I would first try to not add individual rectangles though, but instead work with a single PolyCollection with as many shapes as you need.

– ImportanceOfBeingErnest
Jan 2 at 10:52













Thanks for your advise. I'll try to use PolyCollection to see if it works.

– Lester_wu
Jan 3 at 1:27





Thanks for your advise. I'll try to use PolyCollection to see if it works.

– Lester_wu
Jan 3 at 1:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000830%2fhow-to-update-plot-without-redraw-figure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000830%2fhow-to-update-plot-without-redraw-figure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter