Matplotlib散点图图例,作为单独的图像

时间:2017-01-23 20:23:57

标签: python python-2.7 matplotlib scatter-plot

我正在使用ax.scatter(x,y,c=color, s=size, marker='*', label="mylabel")在散点图中绘制4个符号(标记为',''o''*''^'),每个符号都有所不同尺寸和颜色。

我尝试过调用ax.legend(),虽然我得到了预期的标签信息,但图例中没有标记出现。我试图使用这里解释的一些变体,但没有用:http://matplotlib.org/users/legend_guide.html

此外,我最终需要将我的传奇放在完全独立的图像中。我试过了: Get legend as a separate picture in Matplotlib

https://pymorton.wordpress.com/2016/04/05/creating-separate-legend-figure-with-matplotlib/

但是还没有能够显示标记。任何建议将不胜感激!

这是我的绘图代码:

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
from scipy import stats

lat = np.random.randint(-60.5, high=60.5, size=257087)
lon = np.random.randint(-179.95, high=180, size=257087)
thiscategory =  np.random.randint(12, 60, size=257087)

percRange = np.arange(100,40,-1)


#Rank all values
allPercent=stats.rankdata(thiscategory)/len(thiscategory)

h=np.where(allPercent > 0.9)
hl=np.where((allPercent <= 0.9) & (allPercent > 0.8))
mh=np.where((allPercent <= 0.8) & (allPercent > 0.7))
ml=np.where((allPercent <= 0.7) & (allPercent > 0.6))
l=np.where(allPercent <= 0.6)

allPercent[h]=0
allPercent[hl]=0.25
allPercent[mh]=0.5
allPercent[ml]=0.75
allPercent[l]=1


fig = plt.figure(dpi=400)
ax=fig.add_axes([0,0,1,1]) #position: left, bottom, width, height
ax.set_axis_off()
fig.patch.set_facecolor('none')

latcorners = ([-90.0,90.0])
loncorners = ([-180.0,180.0])


rgba_low=colors.hex2color('#e0ffff') #224-255-255 Light Cyan
rgba_ml=colors.hex2color('#afeeee') #175-238-238 Pale Turquoise
rgba_mh=colors.hex2color('#ffff00') #Yellow 
rgba_hl=colors.hex2color('#ffa500')  #Orange
rgba_high=colors.hex2color('#f8f8ff') #ghost white

m = Basemap(projection='cyl',llcrnrlat=latcorners[0],urcrnrlat=latcorners[1],llcrnrlon=loncorners[0],urcrnrlon=loncorners[1])                       
# Draw on map.
x, y = m(lon, lat)



ax.scatter(x[ml],y[ml], c=rgba_ml, s=3, marker=',',edgecolor='none', alpha=0.4, label=str(mlmin)+" to "+str(mlmax))
ax.scatter(x[mh],y[mh], c=rgba_mh, s=5, marker='o', edgecolor='none', alpha=0.5, label=str(mhmin)+" to "+str(mhmax))
ax.scatter(x[hl],y[hl], c=rgba_hl, s=10, marker='*',edgecolor='none', alpha=0.6, label=str(hlmin)+" to "+str(hlmax))
ax.scatter(x[h],y[h], c=rgba_high, s=20, marker='^', edgecolor='none',alpha=0.7, label=str(hmin)+" to "+str(hmax))

ax.set_xlim([-180,180])
ax.set_ylim([-90,90])

#this is where my legend calls were going, but since I want them in a new plot it doesn't seem appropriate


fig.savefig('testfig.jpg', bbox_inches='tight', transparent=True, pad_inches=0)

*已编辑以显示示例代码。

1 个答案:

答案 0 :(得分:2)

这是我发现的: 我不得不修改我发现的代码,不包括数字调用。绘制散点图(ax.scatter)后,我调用了:

handles,labels = ax.get_legend_handles_labels()

在绘制主图并关闭图后,我称之为新图:

fig_legend = plt.figure(figsize=(2,2))
axi = fig_legend.add_subplot(111)            
fig_legend.legend(handles, labels, loc='center', scatterpoints = 1)
axi.xaxis.set_visible(False)
axi.yaxis.set_visible(False)
fig_legend.canvas.draw()
fig_legend.show()

最终结果包含两个黑色边框,但它是一个单独的图像,并且具有正确颜色的散点符号的单个实例。legend image example here

相关问题