使用matplotlib映射异地(使用geopandas和cartopy)

时间:2016-04-01 14:44:31

标签: matplotlib cartopy geopandas

我创建了一个这样的地图:map

问题在于地图的右侧总是有点偏离现场。我已将界限设置为:

ax.set_xlim(-215800,
            1000000)
ax.set_ylim(3402659,
            4879248)

无论我如何增加xlim,或设置边距,右侧仍然在画布的边界之外。有人可以帮忙吗?

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from osgeo import ogr

import matplotlib.pyplot as plt
import matplotlib as mpl
import rasterio
import cartopy.crs as ccrs

import geopandas
from geopandas import *

from matplotlib import colors



MediumApple='#55FF00'
Cantaloupe='#FFA77F'
Marsred='#FF0000'
crs = ccrs.UTM(zone=10)
ax = plt.axes(projection=crs)
import matplotlib.patches as mpatches

for county in CAcountylist:
    with rasterio.drivers():
        with rasterio.open(r"CA\%s \%s.tif"%(county,county),"r") as src:
            meta = src.meta
            im=src.read().astype('f')
            im=np.transpose(im,[1,2,0])
            print im.shape
            print im.min(),im.max()
            im[im==0]=np.nan
            im=im.squeeze()
            xmin = src.transform[0]
            xmax = src.transform[0] + src.transform[1]*src.width
            print src.width,src.height
            ymin = src.transform[3] + src.transform[5]*src.height
            ymax = src.transform[3]
            colors=[MediumApple,Cantaloupe,Marsred]
             cmap=mpl.colors.ListedColormap([MediumApple,Cantaloupe,Marsred])
            bounds_color=[1,1,2,2,3,3]

            norm=mpl.colors.BoundaryNorm(bounds_color,cmap.N)
            print xmin,xmax,ymin,ymax
            ax.imshow(im, origin='upper', extent=[xmin,xmax,ymin,ymax], transform=crs, interpolation='nearest',cmap=cmap,norm=norm)
df=GeoDataFrame.from_file(r"\CACounty.shp")
df=df.to_crs(epsg=26910)
df.plot(axes=ax,alpha=0)
bounds = df.geometry.bounds
ax.set_xlim(-215800,
            1000000)
ax.set_ylim(3402659,
            4879248)
low_patch = mpatches.Patch(color='#55FF00', label='Low')
Moderate_patch = mpatches.Patch(color='#FFA77F', label='Moderate')
High_patch = mpatches.Patch(color='#FF0000', label='High')

plt.legend(handles=[low_patch,Moderate_patch,High_patch],loc=3)         


plt.show()

0 个答案:

没有答案