首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matplotlib PatchCollection to Legend

Matplotlib PatchCollection to Legend
EN

Stack Overflow用户
提问于 2016-08-30 10:09:08
回答 1查看 1.6K关注 0票数 2

目前,我正试图通过创建代理艺术家(?)来创建自己的自定义传奇处理程序。使用PatchCollections进行修补,然后按照guide.html创建自定义处理程序。

然而,我遇到了一个障碍,试图将其实现到图例中。图例的参数采用补丁,而不是拼贴集合。

代码语言:javascript
复制
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)

上面的代码是可视化处理程序的代码。基本上是一个长方形,上面三角形为虚线,下部为实心。

使用,

代码语言:javascript
复制
plt.legend([patch],["hellocello"],loc='upper right')

重新创建错误。有解决办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-30 10:31:51

从这个部分中的例子来看,您需要定义一个对象,并根据手持机的大小表示所有的坐标,

代码语言:javascript
复制
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上是这样的。结果看起来,

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39224611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档