如何防止散点图超出shapefile映射的范围?

时间:2018-07-20 16:35:33

标签: python matplotlib matplotlib-basemap

我正在使用底图在地图上绘制数据,但是圆圈超出了shapefile国家/地区轮廓设置的边界。我想防止这种情况。我并不是很关心圆心不在轮廓线之外的圆,因为我的数据中没有圆心,但是我想知道如何消除圆心超出边界的部分,即使圆心不在中心在里面。

代码与问题一起重现该问题:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

def get_color_map():
    color_map = {}
    i = 0
    kick = 255
    for x in range(1,10):
        color_map[x] = ((kick-10*x)/kick,(10*x)/kick,0,0.1*x)
    color_map[10] = (1, 0, 0, 1)
    return color_map

lat = np.linspace(40,48,100)
lon = np.linspace(-100,-90,100)
count = pd.Series(np.logspace(0,1, 100,dtype=int))
color_map = get_color_map()
m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49,
            projection='lcc',lat_1=33,lat_2=45,lon_0=-95, resolution='c')
# shapefile can be downloaded from
# http://www2.census.gov/geo/tiger/GENZ2017/shp/cb_2017_us_nation_20m.zip
shp_info = m.readshapefile('cb_2017_us_nation_20m/cb_2017_us_nation_20m','outline',drawbounds=True,
                           linewidth=0.5,color='black', zorder=2)
lons, lats = m(lon, lat)
sc = m.scatter(lons, lats, c=count.map(color_map), marker = 'o', zorder=1)
plt.savefig('fig1.pdf',dpi=1200)

Parts of circles causing the problem

0 个答案:

没有答案