繁体   English   中英

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

[英]Matplotlib scatter plot legend, as separate image

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

我尝试调用ax.legend() ,尽管我得到了预期的标签信息,但图例中没有任何标记。 我尝试使用此处没有解释的多种变体: http : //matplotlib.org/users/legend_guide.html

此外,我最终需要将我的图例放在完全独立的图像中。 我尝试过: 在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)

*编辑以显示示例代码。

这是我发现的内容:我不得不修改发现的代码以不包括图形调用。 绘制散点图(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()

最终结果包含两个黑色边框,但它是单独的图像,并且具有正确颜色的散布符号的单个实例。 图例图片示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM