我试图将彩色向量绘制到Python 2.7中的地图上。我可以毫无问题地绘制向量及其颜色,但是一旦尝试添加海岸线,事情就会出错。请帮助
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.basemap import Basemap
filename = 'wg_weather1_fixed.csv'
readcsv = pd.read_csv(filename)
temp = readcsv.Temperature_degC
wind_speed_kt = readcsv.Wind_Speed_kt
Wind_Direction = readcsv.Wind_Direction
lat = readcsv.Latitude
lon = readcsv.Longitude
Wind_speed_2 = wind_speed_kt*1.852
# extract U and V components
WG_wind_U = Wind_speed_2 * np.sin((360-Wind_Direction)*np.pi/180)
WG_wind_V = -Wind_speed_2*np.cos((360-Wind_Direction)*np.pi/180)
m = Basemap(projection='merc',llcrnrlat=-32.2,urcrnrlat=-29,
llcrnrlon=30,urcrnrlon=33,lat_ts=5,resolution='i')
# Create colour bar
norm = matplotlib.colors.Normalize()
norm.autoscale(temp)
cm = matplotlib.cm.CMRmap
sm = matplotlib.cm.ScalarMappable(cmap=cm, norm=norm)
sm.set_array([])
# Plot
q = m.quiver(lon,lat,WG_wind_U,WG_wind_V,color=cm(norm(temp)))
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.quiverkey(q,0.9, 0.05, 30, r'$30 frac{Kp}{h}$',labelpos='W',fontproperties={'size': 15,'weight': 'bold'})
cbar = plt.colorbar(sm)
plt.show()
本质上,我需要在此图中添加海岸线:在此处输入图像描述
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from mpl_toolkits.basemap import Basemap
# Read in data from CSV
filename = 'wg_weather1_fixed.csv'
readcsv = pd.read_csv(filename)
temp = readcsv.Temperature_degC
wind_speed_kt = readcsv.Wind_Speed_kt
Wind_Direction = readcsv.Wind_Direction
lat = readcsv.Latitude
lon = readcsv.Longitude
Wind_speed_2 = wind_speed_kt*1.852 # Convert from knots to KpH
# extract U and V components
WG_wind_U = Wind_speed_2 * np.sin((360-Wind_Direction)*np.pi/180)
WG_wind_V = -Wind_speed_2*np.cos((360-Wind_Direction)*np.pi/180)
m = Basemap(projection='merc',llcrnrlat=-30.2,urcrnrlat=-29,
llcrnrlon=30.8,urcrnrlon=32.2,lat_ts=5,resolution='i')
# Grid the co-ordinates
X,Y = np.meshgrid(lon,lat)
lons,lats = m(X,Y) # convert the co-ordinates to fit on the map
# Create colour bar
norm = matplotlib.colors.Normalize()
norm.autoscale(temp)
cm = matplotlib.cm.CMRmap # selecting the colourmap
sm = matplotlib.cm.ScalarMappable(cmap=cm, norm=norm)
sm.set_array([])
# Plot
q = m.quiver(lons[0,:],lats[:,0],WG_wind_U,WG_wind_V,color=cm(norm(temp)))
m.fillcontinents(color='#cc9955', zorder = 0)
# Latitudes
parallels = m.drawparallels(np.arange(-30.2,-29.,0.5))
m.drawparallels(parallels,labels=[True,False,False,True])
# Longitudes
meridians = m.drawmeridians(np.arange(30.8,32.2,0.5))
m.drawmeridians(meridians,labels=[True,False,False,True])
#plt.xlabel('Longitude')
#plt.ylabel('Latitude')
plt.quiverkey(q,0.9, 0.05, 30,'30 KpH',labelpos='W')
cbar = plt.colorbar(sm)
cbar.set_label('Air Temperature')
plt.title('Wind velocity shaded by air temperature')
plt.show()