2017-12-05 1 views
0

Ich versuche, farbige Vektoren auf eine Karte in Python 2.7 zu plotten. Ich kann die Vektoren und ihre Farben ohne Probleme darstellen, aber sobald ich versuche, fügen Sie die Küste Dinge schief gehen. Bitte helfenKöcher-Plot auf einer Karte mit Python

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() 

Im Grunde brauche ich eine Küste zu diesem Plot hinzuzufügen: enter image description here

+0

Können Sie ein Beispiel Ihrer CSV-Datei schreiben? Sehr schwierig, ohne einige Beispieldaten zu helfen, die [das Problem neu erstellen] (https://stackoverflow.com/help/mcve). –

+0

Hi Ken - Ich habe herausgefunden, wo ich falsch gelaufen bin. Ich musste die Koordinaten meshgrid und indexieren, wenn ich das Plot erstellte. Danke, dass du auf meinen Beitrag geantwortet hast. – Jetman

+0

Fühlen Sie sich frei, was Sie getan haben, als Antwort und akzeptieren Sie es. –

Antwort

1
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() 
Verwandte Themen