Matplotlib plot with variable line width

前端 未结 4 1159
隐瞒了意图╮
隐瞒了意图╮ 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:40

    Use LineCollections. A way to do it along the lines of this Matplotlib example is

    import numpy as np
    from matplotlib.collections import LineCollection
    import matplotlib.pyplot as plt
    x = np.linspace(0,4*np.pi,10000)
    y = np.cos(x)
    lwidths=1+x[:-1]
    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lc = LineCollection(segments, linewidths=lwidths,color='blue')
    fig,a = plt.subplots()
    a.add_collection(lc)
    a.set_xlim(0,4*np.pi)
    a.set_ylim(-1.1,1.1)
    fig.show()
    

    output

提交回复
热议问题