2015-05-11 61 views
3

我與Matplotlib, legend with multiple different markers with one label有'相似'問題。由於這個問題Combine two Pyplot patches for legend,我能夠實現以下目標。具有相同標籤的不同散點圖標記

fig = pylab.figure() 
figlegend = pylab.figure(figsize=(3,2)) 
ax = fig.add_subplot(111) 
point1 = ax.scatter(range(3), range(1,4), 250, marker=ur'$\u2640$', label = 'S', edgecolor = 'green') 
point2 = ax.scatter(range(3), range(2,5), 250, marker=ur'$\u2640$', label = 'I', edgecolor = 'red') 
point3 = ax.scatter(range(1,4), range(3), 250, marker=ur'$\u2642$', label = 'S', edgecolor = 'green') 
point4 = ax.scatter(range(2,5), range(3), 250, marker=ur'$\u2642$', label = 'I', edgecolor = 'red') 
figlegend.legend(((point1, point3), (point2, point4)), ('S','I'), 'center', scatterpoints = 1, handlelength = 1) 
figlegend.show() 
pylab.show() 

但是,我的兩個(金星和火星)標記在圖例中重疊。我嘗試玩handlelength,但這似乎沒有幫助。任何建議或意見都會有所幫助。

回答

2

這裏是我的解決方法MWE。我實際上繪製了兩個額外的「情節」,point_gpoint_r,它們有我們將使用的圖例句柄。然後我用白色的方形標記覆蓋它們。根據需要畫出剩餘的地塊。

import matplotlib.pyplot as plt 
plt.rc('text', usetex=True) 
plt.rc('text', **{'latex.preamble': '\\usepackage{wasysym}'}) 
plt.rc('lines', **{'markersize':20}) 

fig = plt.figure() 

point_g, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='g') 
point_r, = plt.plot((0,), (0,), ls='none', marker='$\\male\\female$', mec='r') 
plt.plot((0,), (0,), marker='s', mec='w', mfc='w') 

plt.plot(range(3), range(1,4), ls='none', marker='$\\male$', mec='g') 
plt.plot(range(3), range(2,5), ls='none', marker='$\\male$', mec='r') 
plt.plot(range(1,4), range(3), ls='none', marker='$\\female$', mec='g') 
plt.plot(range(2,5), range(3), ls='none', marker='$\\female$', mec='r') 

plt.axis([-0.1, 4.1, -0.1, 4.1]) 
plt.legend((point_g, point_r), ('Green', 'Red'), markerscale=1.6, numpoints=1, 
      borderpad=0.8, handlelength=3, labelspacing=1) 

plt.show() 

注:如果您使用unicode符號你不需要LaTeX的序言。我無法讓他們在我的系統(Linux)上工作,所以我使用了LaTeX符號。這種方法適用於所有符號,只需刪除plt.rc命令並將\\male\\female更改爲unicode字符。

3

可能的解決方法是創建一個兩列的傳說與第一列空白標籤:

figlegend.legend((point1, point2, point3, point4), (' ', ' ', 'S', 'I'), 
       'center', scatterpoints = 1, ncol = 2) 

legend with two columns

+0

您還可以調整列間距以將它們靠得更近。 – Blink

相關問題