如何在python中绘制流畅的线条?

时间:2017-04-06 15:27:19

标签: python netcdf matplotlib-basemap smoothing

NCEP数据来自此website。 我想画一幅这样的照片: wind field and heights 或者这个(这个添加槽线): wind field and heights

我的数据与他们不同,因此内容不同。 但是,方法应该是相同的。

我不知道如何平滑线条。这是我的结果: my_picture

这是我的代码:

import numpy as np
import scipy
import matplotlib.pyplot as plt
from netCDF4 import Dataset
from scipy.interpolate import Rbf
from scipy.ndimage import zoom 
from mpl_toolkits.basemap import Basemap

m=Basemap(projection='cyl',llcrnrlat=20,urcrnrlat=50,llcrnrlon=90,urcrnrlon=130)

CHNshp = 'D:\python\shapefile\data\CHN_adm_shp\CHN_adm1'
m.readshapefile(CHNshp,'CHN',drawbounds = False)
TWNshp = 'D:\python\shapefile\data\TWN_adm_shp\TWN_adm0'
m.readshapefile(TWNshp,'TWN',drawbounds = False)

for info, shape in zip(m.CHN_info, m.CHN):
        x, y = zip(*shape) 
        m.plot(x, y, marker=None,color='k',linewidth = 0.5)
for info, shape in zip(m.TWN_info, m.TWN):
        x, y = zip(*shape) 
        m.plot(x, y, marker=None,color='k',linewidth = 0.5)

parallels = np.arange(-90.,91.,10.)
m.drawparallels(parallels,labels=[1,0,0,1],linewidth=0.5,xoffset=1.2)
meridians = np.arange(-180.,181.,10.)
m.drawmeridians(meridians,labels=[1,0,0,1],linewidth=0.5,yoffset=1.2)

u=Dataset(r'D:\python\TRY\ncep\uwnd.2016.nc','r')
v=Dataset(r'D:\python\TRY\ncep\vwnd.2016.nc','r')
hgt_data=Dataset(r'D:\python\TRY\ncep\hgt.2016.nc','r')

uwnd=u.variables['uwnd'][728][2][:]
vwnd=v.variables['vwnd'][728][2][:]
hgt=hgt_data.variables['hgt'][728][2][:]
lat=u.variables['lat'][:]
lon=u.variables['lon'][:]

index1=np.logical_and(lon>=90,lon<=130);index2=np.logical_and(lat>=20,lat<=50)
lons=lon[index1];lats=lat[index2]
u1=uwnd[index2,:];u2=u1[:,index1]
v1=vwnd[index2,:];v2=v1[:,index1]
hgt1=hgt[index2,:];hgt2=hgt1[:,index1]

nx,ny=np.meshgrid(lons,lats)
x,y=m(nx,ny)
Q = m.quiver(x,y,u2,v2,scale=250,width=0.003)
qk = plt.quiverkey(Q, 0.85, -0.12, 20, '20 m/s', labelpos='N')

rbf = scipy.interpolate.Rbf(x, y, hgt2)
zi = rbf(x, y)
plt.contour(x,y,zi,color='k')

plt.show()

更新

lons = zoom(lons,3,order=3)
lats = zoom(lats,3,order=3)
x,y  = np.meshgrid(lons,lats,copy=False)
hgt2 = zoom(hgt2,3,order=3)
cs = plt.contour(x,y,hgt2,levels=levels,colors='k',linewidths=0.7)

1 个答案:

答案 0 :(得分:2)

查看matplotlib网站https://matplotlib.org/examples/pylab_examples/contour_demo.htmlcontour()函数的示例

这是他们如何生成此plot的x和y坐标:

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)

您需要做的是提高您在自己程序中lons函数中使用的latsmeshgrid()字段的分辨率。

更高的分辨率 - &gt;线条更流畅