创建自定义标记作为复杂顶点的并集

时间:2017-07-12 15:29:51

标签: python matplotlib

我一直在寻找一种优雅/简单(工作!)的解决方案来为matplotlib创建新的复杂标记。

例如,我想设计一个新标记,它是一组顶点的并集,例如(只是一个例子),两个对称的花瓣(见verts1和verts2)和上下两条线(看到verts3和verts4)。我也希望花瓣可能填充(或不填充)和每个顶点的边缘颜色可能有各种颜色(一个花瓣是蓝色,另一个是橙色)。我该怎么办?

一个天真的方法是做一些事情(对于一个双花瓣,左边的一个没有被填满,右边的一个被填满,请参阅下面的verts1,verts2,verts3,verts4的定义):

代码

x = rand(10)
y = rand(10)
verts = [verts1, verts2, verts3, verts4]
fc = ['k', 'None', 'None', 'None']
ec = ['b', 'orange', 'k', 'k']

for lverts, lfc, lec in list(zip(verts, fc, ec)) :
    scatter(x, y, marker= (lverts, 0), facecolor=lfc, edgecolor=lec, s=1000, label='My symbol')

==>但是,由于这些是在for循环中完成的,因此我不会将其视为单个标记,例如:

legend(loc=0)

问题:我该如何管理? (无法在网上找到答案)

非常欢迎建议!

谢谢!

顶点的定义
if 1:
    # verts1:
    size, angrad = 10., 0.
    rx = 4. * size
    theta = np.linspace(-pi / 4., pi / 4., 151)
    x = rx*np.sqrt(cos(2.*theta))*cos(theta)
    y = rx*np.sqrt(cos(2.*theta))*sin(theta)
    rotx = x * cos(angrad) + y * sin(angrad)
    roty = -x * sin(angrad) + y * cos(angrad)
    verts1 = list(zip(rotx,roty))

    # verts2:
    size, angrad = 10.,  np.pi
    rx = 4. * size
    theta = np.linspace(-pi / 4., pi / 4., 151)
    x = rx*np.sqrt(cos(2.*theta))*cos(theta)
    y = rx*np.sqrt(cos(2.*theta))*sin(theta)
    rotx = x * cos(angrad) + y * sin(angrad)
    roty = -x * sin(angrad) + y * cos(angrad)
    verts2 = list(zip(rotx,roty))

    # verts3
    verts3 = list(zip([0.,0.],[0,0.1]))

    # verts4
    verts4 = list(zip([0.,0.],[-0.1,-0.03])) 

1 个答案:

答案 0 :(得分:1)

关于如何创建单个自定义图例标记有一些问题:

在这种情况下,所有其他方法都无法创建自定义处理程序类并使用它在图例中创建符号。

enter image description here

import matplotlib.pyplot as plt
import numpy as np


class Symbol(object):
    def __init__(self,fc, ec, markersize):
        size, angrad = 10., 0.
        rx = 4. * size
        theta = np.linspace(-np.pi / 4., np.pi / 4., 151)
        x = rx*np.sqrt(np.cos(2.*theta))*np.cos(theta)
        y = rx*np.sqrt(np.cos(2.*theta))*np.sin(theta)
        rotx = x * np.cos(angrad) + y * np.sin(angrad)
        roty = -x * np.sin(angrad) + y * np.cos(angrad)
        verts1 = list(zip(rotx,roty))
        # verts2:
        size, angrad = 10.,  np.pi
        rx = 4. * size
        theta = np.linspace(-np.pi / 4., np.pi / 4., 151)
        x = rx*np.sqrt(np.cos(2.*theta))*np.cos(theta)
        y = rx*np.sqrt(np.cos(2.*theta))*np.sin(theta)
        rotx = x * np.cos(angrad) + y * np.sin(angrad)
        roty = -x * np.sin(angrad) + y * np.cos(angrad)
        verts2 = list(zip(rotx,roty))
        # verts3
        verts3 = list(zip([0.,0.],[0,0.1]))
        # verts4
        verts4 = list(zip([0.,0.],[-0.1,-0.03])) 

        self.verts=[verts1,verts2,verts3,verts4]
        self.fc = fc
        self.ec = ec
        self.size=markersize
        self.group = list(zip(self.verts, self.fc, self.ec))

class SymbolHandler(object):
    def legend_artist(self, legend, orig_handle, fontsize, handlebox):

        x0, y0 = handlebox.xdescent, handlebox.ydescent
        width,height = handlebox.width, handlebox.height
        sc = []
        for lverts, lfc, lec in orig_handle.group:
            c = plt.scatter([width/2.-x0], [height/2.-y0], marker=(lverts, 0), 
                             facecolor=lfc, edgecolor=lec,s=orig_handle.size, 
                             transform=handlebox.get_transform())
            handlebox.add_artist(c)
            c.remove()
            sc.append(sc)
        return []


x = np.random.rand(4)
y = np.random.rand(4)
x2 = np.random.rand(4)
y2 = np.random.rand(4) 

fc = ['k', 'None', 'None', 'None']
ec = ['b', 'orange', 'k', 'k']
size =1000.
s = Symbol(fc,ec, size)
for lverts, lfc, lec in s.group:
    plt.scatter(x, y, marker= (lverts, 0), facecolor=lfc, edgecolor=lec, 
                s=size)


fc2 = ['crimson', 'limegreen', 'None', 'None']
ec2 = ['gold', 'gold', 'purple', 'k']
size2 =800.
s2 = Symbol(fc2,ec2, size2)
for lverts, lfc, lec in s2.group:
    plt.scatter(x2, y2, marker= (lverts, 0), facecolor=lfc, edgecolor=lec, 
                s=size2)


plt.legend([s,s2], ['label 1', "label 2"], handleheight=5, handlelength=3,
           handler_map={Symbol: SymbolHandler()})

plt.xlim(-0.3,1.3)
plt.ylim(-0.3,1.3)
plt.show()