BufferRegion is cleared by calling clf ()

I have an application in which I would like to draw counties from a shapefile using Basemap. Drawing graph polygons is a bottleneck in rendering, and since I will draw the same region in the USA (a bunch of times), I'd rather not draw all the polygons more than I need. So I got the idea to draw counties on a figure with a transparent background, copy the axes to the pixel buffer using copy_from_bbox() and restore the buffer using restore_region() when I need to draw counties.

The main code is as follows:

 import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap map = Basemap(...) # Create Basemap object map.readshapefile("countyp020", 'counties', linewidth=0.5) # Draws the county lines plt.gcf().patch.set_alpha(0.0) plt.gca().patch.set_alpha(0.0) # Copy to the pixel buffer (county_buffer is of type BufferRegion) county_buffer = plt.gcf().canvas.copy_from_bbox(plt.gca().bbox) plt.clf() # This line is problematic (see below) # Plot my data here ... # Restore the pixel buffer plt.gcf().canvas.restore_region(county_buffer) plt.gcf().canvas.blit(plt.gca().bbox) # Not sure if this line is necessary plt.gcf().canvas.draw() 

It works like a charm ... except for the line where I clean the figure. Clearing a shape between visualizers seems to also clear the BufferRegion object, and since I am updating the title and color bar, I would also like to clear the shape between renderings.

So my question is, does anyone know a way to clear the shape and keep the pixel buffer intact? I could not find much documentation on BufferRegion , copy_from_bbox() or restore_region() , so it was a bit difficult to debug this. If there is no easy way around, does anyone know another way to do basically what I'm trying to do?

Thanks in advance!

+4
source share

Source: https://habr.com/ru/post/1440929/


All Articles