2017-07-05 38 views
2

我似乎弄清楚如何將手柄和標籤從matplotlib.patches.Patch傳遞到圖例。matplotlib自定義傳奇與孵化

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 


a_val = 0.6 
colors = ['#EA5739','#FEFFBE','#4BB05C'] 


circ1 = mpatches.Patch(facecolor=colors[0],alpha=a_val,hatch=['\\\\'],label='Label1') 
circ2= mpatches.Patch(facecolor=colors[1],alpha=a_val,hatch='o',label='Label2') 
circ3 = mpatches.Patch(facecolor=colors[2],alpha=a_val,hatch='+',label='Label3') 

fig,(ax) = plt.subplots() 

ax.legend(handles = [circ1,circ2,circ3],loc=2) 
plt.tight_layout() 

爲什麼上面例子中的圖例爲空?

回答

1

我無法重現您的問題,或者您錯過了一個很大的錯誤。當我在上面運行你的代碼時,我得到一個關於list不可排隊的錯誤,這個錯誤似乎來自第一個Patch調用的kwarg。刪除列表中的語法(使用4個反斜槓額外效果的原始字符串)似乎爲我工作在matplotlib 2.0.2:

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 


a_val = 0.6 
colors = ['#EA5739','#FEFFBE','#4BB05C'] 


circ1 = mpatches.Patch(facecolor=colors[0],alpha=a_val,hatch=r'\\\\',label='Label1') 
circ2= mpatches.Patch(facecolor=colors[1],alpha=a_val,hatch='o',label='Label2') 
circ3 = mpatches.Patch(facecolor=colors[2],alpha=a_val,hatch='+',label='Label3') 

fig,(ax) = plt.subplots() 

ax.legend(handles = [circ1,circ2,circ3],loc=2) 
plt.tight_layout() 

result of the above

這是你看到的是什麼?

+1

它已經成爲'hatch = ['\\\'']'。現在事情似乎表現得很好。謝謝! – dubbbdan