2016-08-30 12 views
0

現在のところ、PatchCollectionsを使用してプロキシアーティスト(?)パッチを作成し、次にhttp://matplotlib.org/users/legend_guide.htmlをカスタムハンドラにすることによって、独自のカスタム凡例ハンドラを作成しようとしています。Matplotlib PatchCollectionを凡例

しかし、私はこれを伝説に実装しようとする際に障害物にぶつかっています。凡例の引数はパッチを取りますが、パッチコレクションは取りません。

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.mlab as mlab 
import matplotlib.patches as mpatches 
from matplotlib.path import Path 
from matplotlib.collections import PatchCollection 

fig = plt.figure() 
ax = fig.add_subplot(111) 

verts1 = [(0.,0.),(0.,1.),(1.,1.),(0.51,0.51),(0.,0.),(0.,0.),] 
codes1 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,] 
path1 = Path(verts1,codes1) 
patch1 = mpatches.PathPatch(path1,ls='dashed',ec='red',facecolor="none") 


verts2 = [(0.49,0.49),(0.,0.),(1.,0.),(1.,1.),(0.5,0.5),(0.,0.),] 
codes2 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,] 
path2 = Path(verts2,codes2) 
patch2 = mpatches.PathPatch(path2,ls='solid',edgecolor='red', facecolor="none") 

patch = PatchCollection([patch1,patch2],match_original=True) 

ax.set_xlim(-2,2) 
ax.set_ylim(-2,2) 

ax.add_collection(patch) 

Visual

上記ハンドラを可視化するためのコードです。基本的に破線で上三角と使用固形

低級有する長方形、

plt.legend([patch],["hellocello"],loc='upper right') 

エラーを再作成します。回避策はありますか?このsectionの例から

答えて

2

、あなたがオブジェクトを定義し、handleboxサイズの面ですべての座標を表現する必要があるように見える、

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.mlab as mlab 
import matplotlib.patches as mpatches 
from matplotlib.path import Path 
from matplotlib.collections import PatchCollection 

class AnyObject(object): 
    pass 

class AnyObjectHandler(object): 
    def legend_artist(self, legend, orig_handle, fontsize, handlebox): 
     x0, y0 = handlebox.xdescent, handlebox.ydescent 
     width, height = handlebox.width, handlebox.height 
     hw = 0.5*width; hh = 0.5*height 
     verts1 = [(x0,y0),(x0,y0+height),(x0+width,y0+height),((x0+hw)*1.01,(y0+hh)*1.01),(x0,y0),(x0,y0),] 
     codes1 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,] 
     path1 = Path(verts1,codes1) 
     patch1 = mpatches.PathPatch(path1,ls='dashed',ec='red',facecolor="none") 

     verts2 = [((x0+hw)*0.99,(y0+hh)*0.99),(x0,y0),(x0+width,y0),(x0+width,y0+height),(x0+hw,y0+hh),(x0,y0),] 
     codes2 = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.MOVETO,Path.CLOSEPOLY,] 
     path2 = Path(verts2,codes2) 
     patch2 = mpatches.PathPatch(path2,ls='solid',edgecolor='red', facecolor="none") 

     patch = PatchCollection([patch1,patch2],match_original=True) 

     handlebox.add_artist(patch) 
     return patch 


fig = plt.figure() 
ax = fig.add_subplot(111) 

ax.set_xlim(-2,2) 
ax.set_ylim(-2,2) 

plt.legend([AnyObject()], ['hellocello'], 
      handler_map={AnyObject: AnyObjectHandler()}) 

plt.show() 

これは、少なくとも私にとっては、PatchCollectionで大丈夫動作するようですmatplotlibバージョン1.4.3結果がHi

+0

enter image description here

、のように見える、あなたの最後の文はspotonました。私のバージョンのmatplotlib 1.3.1に問題があり、1.5.x(?)にアップグレードしたときに解決されました。どうもありがとうございました! –

+0

これはうまくいきました。疑問があれば、ソフトウェアをアップデートしてください:) –

関連する問題