底图和Matplotlib - 提高速度

时间:2012-05-08 19:01:59

标签: python matplotlib matplotlib-basemap

我正在使用MatplotlibBasemap创建一个地理空间可视化经济数据的工具。

但是,现在,我想到的唯一方法就是每次我想要更改数据时都会创建一个新的底图。

以下是我正在使用的代码的相关部分:

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()

如何更快地创建这些图?

我无法想到每次运行代码时都避免创建地图的解决方案。我尝试在课堂外创建画布/人物,但它没有那么大的差别。最慢的调用是创建底图并加载形状数据的调用。其他一切都运行得非常快。

另外,我尝试保存底图以备将来使用,但由于我需要新的轴,我无法让它工作。也许你可以指出我如何做到这一点的正确方向。

我想让你知道我正在使用画布作为PySide QWidget而我正在根据数据绘制不同类型的地图,这只是其中之一(另一个是欧洲地图) ,例如,或美国)。

1 个答案:

答案 0 :(得分:4)

您可以挑选和取消选择底图实例(there is an example of doing this in the basemap source),这可能会在创建绘图时节省大量时间。

此外,可能值得看看shapefile读取的时间长度(您可能也想要腌制)。

最后,我会认真考虑调查更新国家颜色数据的选项,而不是每次都制作一个新数据。

HTH,

相关问题