Matplotlib plot with variable line width

前端 未结 4 1169
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 14:00

Is it possible to plot a line with variable line width in matplotlib? For example:

from pylab import *
x = [1, 2, 3, 4, 5]
y = [1, 2, 2, 0, 0]
width = [.5,          


        
4条回答
  •  天涯浪人
    2020-11-27 14:33

    An alternative to Giulio Ghirardo's answer which divides the lines in segments you can use matplotlib's in-built scatter function which construct the line by using circles instead:

    from matplotlib import pyplot as plt
    import numpy as np
    
    x = np.linspace(0,10,10000)
    y = 2 - 0.5*np.abs(x-4)
    lwidths = (1+x)**2 # scatter 'o' marker size is specified by area not radius 
    plt.scatter(x,y, s=lwidths, color='blue')
    plt.xlim(0,9)
    plt.ylim(0,2.1)
    plt.show()
    

    In my experience I have found two problems with dividing the line into segments:

    1. For some reason the segments are always divided by very thin white lines. The colors of these lines get blended with the colors of the segments when using a very large amount of segments. Because of this the color of the line is not the same as the intended one.

    2. It doesn't handle very well very sharp discontinuities.

提交回复
热议问题