2017-01-23 61 views
0

我使用ax.scatter(x,y,c=color, s=size, marker='*', label="mylabel")繪製在散點圖中的4個符號(標誌物是',''o''*',和'^'),每個具有不同的尺寸和顏色。Matplotlib散點圖說明,作爲分開的圖像

我曾嘗試致電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

這就像是打電話給某人,說「我的電腦無法正常工作」。有太多可能發生錯誤的事情。因此,您需要發佈您使用過的代碼才能獲得有關此問題的任何幫助。您描述它的方式預計會起作用,因此代碼中的某處必定存在一些問題,我們只能在獲取[MCVE]時幫助您解決問題。 – ImportanceOfBeingErnest

+0

我想保持這個開放式結束的情況下,有一個我不知道的散點圖屬性。我將編輯以張貼繪圖代碼。 – mofs

+1

當我添加'ax.legend()'時,會顯示包括標記在內的圖例。由於您不應用[鏈接的問題]中提到的任何命令(http://stackoverflow.com/questions/4534480/get-legend-as-a-separate-picture-in-matplotlib)關於生成圖例一個單獨的文件,你爲什麼期望發生?最後,請考慮[MCVE]中最小,完整和可驗證的單詞是什麼意思! – ImportanceOfBeingErnest

回答

1

這裏是我發現的: 我不得不修改我發現的代碼,不包括數字調用。繪製散射(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