I am creating a tool for geospatial visualization of economic data using Matplotlib
and Basemap
.
However, right now, the only way I thought about this gives me enough flexibility is to create a new basemap every time I want to change the data.
Here are the relevant parts of the code I'm using:
class WorldMapCanvas(FigureCanvas): def __init__(self,data,country_data): self.text_objects = {} self.figure = Figure() self.canvas = FigureCanvas(self.figure) self.axes = self.figure.add_subplot(111) self.data = data self.country_data = country_data #this draws the graph super(WorldMapCanvas, self).__init__(Figure()) self.map = Basemap(projection='robin',lon_0=0,resolution='c', ax=self.axes) self.country_info = self.map.readshapefile( 'shapefiles/world_country_admin_boundary_shapefile_with_fips_codes', 'world', drawbounds=True,linewidth=.3) self.map.drawmapboundary(fill_color = '#85A6D9') self.map.fillcontinents(color='white',lake_color='#85A6D9') self.map.drawcoastlines(color='#6D5F47', linewidth=.3) self.map.drawcountries(color='#6D5F47', linewidth=.3) self.countrynames = [] for shapedict in self.map.world_info: self.countrynames.append(shapedict['CNTRY_NAME']) min_key = min(data, key=data.get) max_key = max(data, key=data.get) minv = data[min_key] maxv = data[max_key] for key in self.data.keys(): self.ColorCountry(key,self.GetCountryColor(data[key],minv,maxv)) self.canvas.draw()
How can I create these sites faster?
I could not come up with a solution to avoid creating a map every time I run my code. I tried to create a canvas / figure outside the class, but that didn’t have much effect. The slowest call is one that creates a basemap and loads form data. Everything else works pretty fast.
In addition, I tried to save the basemap for future use, but since I needed new axes, I could not get it to work. Perhaps you can point me in the right direction on how to do this.
I would like you to know that I use canvas as PySide QWidget and that I draw different types of maps depending on the data, this is only one of them (the other is a map of Europe, for example, or the USA).