matplotlib / basemap

Plot on map projections (with coastlines and political boundaries) using matplotlib
MIT License
774 stars 392 forks source link

Load a region-specific Basemap, then load the shapefile file and save it in svg or svgz format, which is very slow and takes up a lot of space. #523

Closed jiangzhiwei2018 closed 1 year ago

jiangzhiwei2018 commented 2 years ago

I am using the multiple subplots in one image and loading shapefile files in the subplots, and then saving them in svg or svgz format by plt,savefig("xxx.svg") command is very slow and takes up a lot of disk space. How can I fix it?


    num_samples = 1
    num_frames = 2

    num_cols = num_frames
    num_rows = num_samples
    num_rows_s = 4
    gs = gridspec.GridSpec(num_rows + 1, 1, hspace=0.05,
                           height_ratios=[1] * num_rows + [0.035])

    for s in range(num_samples):
        gs_s = gridspec.GridSpecFromSubplotSpec(num_rows_s, num_cols,
                                                subplot_spec=gs[s, 0],
                                                wspace=0.05, hspace=0.05)
        for col in range(num_cols):
            for row in range(num_rows_s):
                data = np.random.random(size=(64, 64))

                plt.subplot(gs_s[row, col])
                ax = plt.gca()

                bmap = Basemap(llcrnrlon=115.5, urcrnrlon=117.25, llcrnrlat=39.25, urcrnrlat=41.)
                shp_info = bmap.readshapefile(r'F:\LargeDataset\bound_shape\gadm36_CHN_shp\gadm36_CHN_2', 'states',
                                              drawbounds=True)
                bmap.imshow(data)
        plt.savefig("xxx.svg")
        plt.show()

When I increase the number of subgraphs, the rendering of svg format images will become very slow

It may be that I reload a brand new shapefile each time and save the global region in svg format, but I only need a small part of the region

20211117194856

jklymak commented 2 years ago

First, basemap is end-of-life, so consider cartopy

Second, I assume your shape file is huge, and largely outside your region of interest. You are plotting it 7 times, so your file will be very large. You can probably crop it using a tool like shapely?