matplotlib与点颜色

时间:2017-05-16 15:45:49

标签: python python-2.7 matplotlib matplotlib-basemap

我制作了一个底图,其最大风速位置根据风速绘制为绿色,黄色或红色。我现在正在尝试添加一个有三种颜色的图例,但我收到了这条消息:

C:\ Python27 \ lib \ site-packages \ matplotlib \ axes.py:4747:UserWarning:找不到标记的对象。在个别情节上使用label ='...'kwarg。   warnings.warn(“找不到标记的对象。”

情节输出正常,但没有传说。

任何帮助非常感谢。

#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

1 个答案:

答案 0 :(得分:1)

以下是您可以尝试的工作代码:

DbValidationException

enter image description here