【发布时间】:2016-08-30 10:09:08
【问题描述】:
目前,我正在尝试通过使用 PatchCollections 创建代理艺术家 (?) 补丁,然后按照 http://matplotlib.org/users/legend_guide.html 制作自定义处理程序来制作自己的自定义图例处理程序。
但是,我在尝试将其应用到图例中时遇到了障碍。图例的参数包含补丁,但不包含补丁集合。
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)
以上是可视化处理程序的代码。基本上是一个矩形,上三角为虚线,下三角为实线
使用,
plt.legend([patch],["hellocello"],loc='upper right')
重新创建错误。有解决办法吗?
【问题讨论】:
标签: python matplotlib