在图例中分割标记和线条-Matplotlib [英] Split marker and line in Legend - Matplotlib

查看:59
本文介绍了在图例中分割标记和线条-Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个图例,在其中指定标记的值和行的值,但不能同时指定两者的组合.

I want to make a legend where I specify the value for the markers and the value for the lines but not the combination of both.

此示例应有助于说明我的目标:

This example should help to illustrate my goal:

import matplotlib.pyplot as plt
import numpy as np

pi=3.14
xx=np.linspace(0,2*pi,100)


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

phases=[0,pi/4,pi/2]
markers=['s','o','^']

for phase,mk in zip(phases,markers):
    labeltext='Sine' + '*' + str(phase)
    F=[np.sin(x+phase) for x in xx]
    ax.plot(xx,F,color='b',marker=mk,label=labeltext)

    labeltext='Cosine' + '*' + str(phase)
    F=[np.cos(x+phase) for x in xx]
    ax.plot(xx,F,color='g',marker=mk,label=labeltext)

hand, labl = ax.get_legend_handles_labels()
#hand, labl = function_to_split(hand,labl,'*')
ax.legend(hand,labl)
plt.savefig('Figure.png')

结果图如下

我想要的是一个 function_to_split ,自动以这样的图例结尾:

What I wanted is a function_to_split to automatically end up with a legend like this:

[blue line] Sine
[green line] Cosine
[black square] 0
[black circle] 0.785
[black triangle] 1.57

推荐答案

我通过自动创建虚拟句柄来解决这个问题.

I figured it out by automatically creating fictitious handles.

import matplotlib.pyplot as plt
import numpy as np

def function_to_split(hand,labl,dividor):

    Hand_L=[]
    Hand_M=[]
    Labl_L=[]
    Labl_M=[]

    for h,l in zip(hand,labl):
        co=h.get_color()
        ls=h.get_linestyle()
        lw=h.get_linewidth()
        mk=h.get_marker()
        mew=h.get_markeredgewidth()
        ms=h.get_markersize()

        LABS=l.split(dividor)

        if len(LABS) != 2:
            print 'Split Legends Error: Only exactly 1 Dividor is accepted.'
            print '                     Currently ' + str(len(LABS)-1) + ' dividors were given'
            return hand,labl

        #Line and Color
        LICO = plt.Line2D((0,1),(0,0), color=co, marker='', linestyle=ls,linewidth=lw)
        #Marker
        MARK = plt.Line2D((0,1),(0,0), color='k', marker=mk, markeredgewidth=mew, markersize=ms, linestyle='')

        if LABS[0] not in Labl_L:
            Hand_L.append(LICO)
            Labl_L.append(LABS[0])

        if LABS[1] not in Labl_M:
            Hand_M.append(MARK)
            Labl_M.append(LABS[1])

    return Hand_L+Hand_M,Labl_L+Labl_M



pi=3.14
xx=np.linspace(0,2*pi,100)


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

phases=[0,pi/4,pi/2]
markers=['s','o','^']

for phase,mk in zip(phases,markers):
    labeltext='Sine' + '*' + str(phase)
    F=[np.sin(x+phase) for x in xx]
    ax.plot(xx,F,color='b',marker=mk,label=labeltext)

    labeltext='Cosine' + '*' + str(phase)
    F=[np.cos(x+phase) for x in xx]
    ax.plot(xx,F,color='g',marker=mk,label=labeltext)

hand, labl = ax.get_legend_handles_labels()
hand, labl = function_to_split(hand,labl,'*')
ax.legend(hand,labl)
plt.savefig('Figure.png')

这篇关于在图例中分割标记和线条-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆