python - 图例中的分割标记和线条 - Matplotlib

标签 python matplotlib legend

我想制作一个图例,在其中指定标记的值和线条的值,但不是两者的组合。

这个例子应该有助于说明我的目标:

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')

结果图如下 MWE figure

我想要的是一个function_to_split来自动结束这样的图例:

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

最佳答案

我通过自动创建虚构的句柄来解决这个问题。

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')

enter image description here

关于python - 图例中的分割标记和线条 - Matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552273/

相关文章:

python - 使用 TF Estimator 时 Tensorflow 分布式训练的损失和学习率缩放策略

python - 使用 WTForms 将表示与逻辑分开

matplotlib - 有没有一种方便的方法可以将比例指示器添加到 matplotlib 中的绘图中?

r - 更改图例中的线条长度

r - ggplot 图例与 geom_rect

python - 如何将WAV文件切成10ms数据

python - 检查某列是否包含其他列的值并填充第三列(True 或 False)

python - 具有自动标签功能的标签堆叠条

Matplotlib: add_lines 到具有定义属性的颜色条(颜色:OK;虚线:不OK)

python - 如何使用 xlsxwriter - python 更改图例字体大小