- 积分
- 55950
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-6-21
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先读取所有站点的相关数据。在地图上绘制站点bar图有两种方法,一种方法是为每一个站点添加一个坐标系(Axes),坐标系的建立目前还不能直接利用地图坐标,所以需要用data2pixel函数将站点坐标转为图像坐标,然后再生成bar图所需的坐标系(需要使用units='pixels'参数),再用bar函数绘制直方图即可。需要注意的这种方式成图后图形大小交互式改变会导致坐标系位置偏差,需要重新运行脚本。
- #Read data
- fn = os.path.join(migl.get_sample_folder(), 'ASCII', 'wp.csv')
- df = DataFrame.read_table(fn, delimiter=',', format='%9f', index_col=0)
- lons = df['lon'].values
- lats = df['lat'].values
- models = df['model%'].values
- ois = df['oi%'].values
- enois = df['enoi%'].values
- n = len(lons)
- maxv = max([models.max(), ois.max(), enois.max()])
- #Creat a map axes
- #figure(figsize=[700,550], newfig=False)
- proj = projinfo(proj='lcc', lon_0=105, lat_1=25, lat_2=47)
- ax = axesm(outerposition=[0,0,1,1], projinfo=proj, gridline=False)
- geoshow('cn_province', edgecolor='gray')
- geoshow('china', edgecolor='b')
- scatter(lons, lats, color='k', edge=False, size=4)
- axism([78, 130, 14, 53])
- #Draw figure so we can use data2pixel function
- draw()
- h = 25
- w = 20
- cols = ['r','b','g']
- for lon,lat,model,oi,enoi in zip(lons,lats,models,ois,enois):
- x, y = ax.data2pixel(lon, lat)
- print lon, lat
- data = [model, oi, enoi]
- #Draw fixed pixels size pie charts at specific longitude/latitude
- ax1 = axes(position=[x, y, w, h], units='pixels', axis=False)
- left = 0
- width = .1
- for i in range(3):
- bar(left, data, width, color=cols, edgecolor='gray')
- left += width
- ylim(0, maxv)
- ax1 = axes(position=[150, 100, w * 3, h], units='pixels', axis=False)
- left = 0
- width = 1
- labels = ['model (%)','oi (%)','enoi (%)']
- bb = []
- data = [60, 80, 100]
- for i in range(3):
- b = bar(left, data, width, color=cols, edgecolor='gray', label=labels)
- bb.append(b)
- left += width
- text(left+0.5, 50, '100', xalign='left', yalign='center', fontsize=12)
- ylim(0, maxv)
- xlim(0, 9)
- ax2 = axes(position=[130, 30, 100, 100], units='pixels', axis=False)
- legend(ax1.get_legend(), loc='lower left', fontsize=12, frameon=False)
- #Add south China Sea
- axesm(position=[0.8,0.08,0.15,0.2], projinfo=proj, axison=False, gridlabel=False, frameon=True)
- geoshow('cn_border', facecolor=(0,0,255))
- axism([106, 123, 2, 23])
另一种方式是先用scatter函数生成站点图层,并将数据加入站点图层的属性表中,然后用图层的add_charts函数添加所有站点的直方图。
- #Read data
- fn = 'D:/Temp/ascii/wp.csv'
- df = DataFrame.read_table(fn, delimiter=',', format='%9f', index_col=0, index_format='%s')
- ids = df.index.data
- lons = df['lon'].values
- lats = df['lat'].values
- models = df['model%'].values
- ois = df['oi%'].values
- enois = df['enoi%'].values
- n = len(lons)
- maxv = max([models.max(), ois.max(), enois.max()])
- #Creat a map axes
- figure(figsize=[1000,656], newfig=False)
- proj = projinfo(proj='lcc', lon_0=105, lat_1=25, lat_2=47)
- ax = axesm(outerposition=[0,0,1,1], projinfo=proj, gridline=False)
- geoshow('china', edgecolor='b')
- layer = scatter(lons, lats, color='k', edge=False, size=4)
- layer.addfield('ID', 'string', ids)
- layer.addfield('model', 'float', models)
- layer.addfield('oi', 'float', ois)
- layer.addfield('enoi', 'float', enois)
- cols = ['r', 'g', 'b']
- labels = ['model (%)','oi (%)','enoi (%)']
- ls = makesymbolspec('polygon', dict(color=cols[0], edgecolor='gray', caption=labels[0]), \
- dict(color=cols[1], edgecolor='gray', caption=labels[1]),
- dict(color=cols[2], edgecolor='gray', caption=labels[2]))
- layer.add_charts(['model','oi','enoi'], ls, avoidcoll=False, align='none')
- layer.addlabels('ID', avoidcoll=False, yoffset=-5)
- layer.movelabel('58606', x=10)
- axism([78, 130, 14, 53])
- # Add legend axes
- w = 24
- h = 50
- ax1 = axes(position=[150, 100, w * 3, h], units='pixels', axis=False)
- left = 0
- width = 1
- bb = []
- data = [60, 80, 100]
- for i in range(3):
- b = bar(left, data, width, color=cols, edgecolor='gray', label=labels)
- bb.append(b)
- left += width
- text(left+0.5, 50, '100', xalign='left', yalign='center', fontsize=12)
- ylim(0, maxv)
- xlim(0, 9)
- ax2 = axes(position=[130, 30, 100, 100], units='pixels', axis=False)
- legend(ax1.get_legend(), loc='lower left', markerscale=1.2, fontsize=12, frameon=False)
- #Add south China Sea
- axesm(position=[0.78,0.08,0.15,0.2], projinfo=proj, axison=False, gridlabel=False, frameon=True)
- geoshow('cn_border', facecolor=(0,0,255))
- axism([106, 123, 2, 23])
|
|