Can I give a border (outline) to a line in matplotlib plot function?

后端 未结 3 1225
终归单人心
终归单人心 2020-12-16 14:15

I try:

points = [...]
axe.plot([i[0] for i in points], [i[1] for i in points], linestyle=\'-\', linewidth=10, 
color=\'black\', markeredgewidth=2, markeredge         


        
相关标签:
3条回答
  • 2020-12-16 14:35

    Just plot the line twice with different thicknesses:

    axe.plot([i[0] for i in points], [i[1] for i in points], linestyle='-', linewidth=10, 
    color='green')
    axe.plot([i[0] for i in points], [i[1] for i in points], linestyle='-', linewidth=5, 
    color='black')
    
    0 讨论(0)
  • 2020-12-16 14:38

    The more general answer is to use patheffects. Easy outlines and shadows (and other things) for any artist rendered with a path.
    The matplotlib docs (and examples) are quite accessible.

    http://matplotlib.org/users/patheffects_guide.html

    http://matplotlib.org/examples/pylab_examples/patheffect_demo.html

    0 讨论(0)
  • 2020-12-16 14:41

    If you plot a line twice it won't show up in the legend. It's indeed better to use patheffects. Here are two simple examples:

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.patheffects as pe
    
    # setup data
    x = np.arange(0.0, 1.0, 0.01)
    y = np.sin(2*2*np.pi*t)
    
    # create line plot including an outline (stroke) using path_effects
    plt.plot(x, y, color='k', lw=2, path_effects=[pe.Stroke(linewidth=5, foreground='g'), pe.Normal()])
    # custom plot settings
    plt.grid(True)
    plt.ylim((-2, 2))
    plt.legend(['sine'])
    plt.show()
    

    Or if you want to add a line shadow

    # create line plot including an simple line shadow using path_effects
    plt.plot(x, y, color='k', lw=2, path_effects=[pe.SimpleLineShadow(shadow_color='g'), pe.Normal()])
    # custom plot settings
    plt.grid(True)
    plt.ylim((-2, 2))
    plt.legend(['sine'])
    plt.show()
    

    0 讨论(0)
提交回复
热议问题