2017-05-16 4 views
1

Ich habe eine Grundkarte mit Orten der maximalen Windgeschwindigkeit in grün, gelb oder rot je nach Windgeschwindigkeit erstellt. Ich versuche jetzt, eine Legende hinzuzufügen, die die drei Farben hat, aber ich bekomme diese Nachricht:matplotlib Grundkarte Legende entsprechend der Farbe der Punkte

C: \ Python27 \ lib \ Site-Pakete \ Matplotlib \ Achsen.py: 4747: UserWarning: Keine beschrifteten Objekte gefunden . Verwenden Sie label = '...' kwarg auf einzelnen Plots. warnings.warn ("Keine markierten Objekte gefunden."

Das Grundstück in Ordnung ausgibt, aber es gibt keine Legende.

Jede Hilfe sehr geschätzt.

#Produce map of North Atlantic with locations of maximum storm winds plotted 
from mpl_toolkits.basemap import Basemap 
#lat_0 and long_0 set the center of the map 
plt.figure(3) 
ax = plt.gca() 
map1 = Basemap(projection='merc', lat_0 = 35, lon_0 = -40, 
    resolution = 'l', area_thresh = 1000.0, ax=ax, 
    #set the locations of the corners of the map  
    llcrnrlon=-85.0, llcrnrlat=0.0, 
    urcrnrlon=6.0, urcrnrlat=75.0) 

map1.drawcoastlines() 
map1.drawcountries() 
map1.fillcontinents(color = 'coral') 
map1.drawmapboundary() 
#draw lines of longitude and latitude 
map1.drawmeridians(np.arange(-80, 0, 10)) 
map1.drawparallels(np.arange(10, 70, 10)) 

#function to adjust colour depending on wind speed 
def get_marker_colour(max_wind): 
    if max_wind < 20.0: 
     return ('go') 
    elif max_wind < 35.0: 
     return ('yo') 
    else:   
     return ('ro') 

#Plotting data on the map 
for lon, lat, wind in zip(alt_long_max, lat_max, max_wind): 
    x, y = map1(lon, lat)  
    marker_string = get_marker_colour(wind) 
    map1.plot(x, y, marker_string, markersize=4, markeredgewidth=0.0) 

import matplotlib.patches as mpatches 
low = mpatches.Patch(color='green', label='<20') 
med = mpatches.Patch(color='yellow', label='20-35') 
high = mpatches.Patch(color='red', label='>35') 
plt.legend(handles=[low,med,high],title='Wind speed') 

plt.title("Location of storm maximum winds") 
plt.show() 

code output

+0

Es scheint mit Grundkarte verbunden zu sein, wie in Matplotlib allein sollte dies funktionieren. Es würde daher helfen, wenn Sie ein Problem des Problems bereitstellen könnten. – ImportanceOfBeingErnest

Antwort

1

Hier Der Arbeitscode, den Sie versuchen können:

import matplotlib.patches as mpatches 
from mpl_toolkits.basemap import Basemap 
import numpy as np 
import matplotlib.pyplot as plt 

# color function 
def colorf(wspeed): 
    if wspeed<20: 
     return 'green' 
    elif wspeed>35: 
     return 'red' 
    else: 
     return 'yellow' 
    pass 

# make up some data 
npts = 30 
lon = np.linspace(91,99,npts) 
lat = np.linspace(21,26,npts) 
wspeed = np.linspace(10,60,npts) #wind speed 10-60 

fig = plt.figure(figsize=(8,6)) 
ax = plt.gca() 

bm = Basemap(llcrnrlon = 90, \ 
      llcrnrlat = 20, \ 
      urcrnrlon = 100, \ 
      urcrnrlat = 30, \ 
      projection='cyl', \ 
      resolution='c', \ 
      ax=ax) 

# plot basic map features here 

# plot storm path 
for alon,alat,ws in zip(lon,lat,wspeed): 
    bm.plot(alon,alat, \ 
      marker='o', \ 
      markerfacecolor=colorf(ws), \ 
      markersize=12) 

#create legend 
low = mpatches.Patch(color='green', label='<20') 
med = mpatches.Patch(color='yellow', label='20-35') 
high = mpatches.Patch(color='red', label='>35') 
plt.legend(handles=[low,med,high], title='Wind speed') 

plt.title("Location of storm maximum winds") 
plt.show() 

enter image description here

+0

Danke für die Antwort Swatchai, als ich versuchte, nur Ihren Code ausgeführt habe ich Ihr Grundstück aber die Legende fehlte und es hatte die Fehlermeldung: C: \ Python27 \ lib \ Site-Pakete \ Matplotlib \ Achsen.py: 4747: UserWarning: Keine beschrifteten Objekte gefunden. Verwenden Sie label = '...' kwarg auf einzelnen Plots. warnings.warn ("Keine beschrifteten Objekte gefunden." Ich verstehe nicht, warum es für Sie arbeiten würde und nicht für mich! – Matt

Verwandte Themen