注意我们在第二个plot调用中使用了x[49:]而不是x[50:],这是为了确保两段线条能够无缝连接。 4. 使用LineCollection实现更复杂的样式变化 对于更复杂的样式变化,我们可以使用Matplotlib的LineCollection类。这允许我们为每个线段指定不同的样式。 importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.collectionsimpor...
如何获得从0到20}之间的{整型到行图的{color}之间的一致映射? matplotlib的LineCollection将传递的数组colors缩放到实际间隔0,1上。因此,如果在其中出现数字0, ..., 7,那么(我想)从tab20得到8种颜色,它们在所有20种颜色之间的间隔大致相等。 通过将关键字参数norm这里 lineCollectionsegmentssegments...
4*np.pi,1000)y = np.sin(x)MAP = 'cubehelix'NPOINTS = len(x)我们将针对上面的LineCollection...
另一种更灵活的方法是使用 Matplotlib 的 LineCollection 类。这种方法允许我们为线的每个段设置不同的颜色。 importmatplotlib.pyplotaspltimportnumpyasnpfrommatplotlib.collectionsimportLineCollection# 生成数据x=np.linspace(0,10,100)y=np.sin(x)# 创建线段points=np.array([x,y]).T.reshape(-1,1,2)segme...
# 创建图表并添加LineCollection fig, ax = plt.subplots() ax.add_collection(lc) # 设置坐标轴范围 ax.set_xlim(0, 10) ax.set_ylim(-1.5, 1.5) # 显示图表 plt.show() 这段代码将会在正弦波上绘制出不同颜色的线条段,每段代表数据中的一部分,并且每段的颜色是由colors列表指定的。
使用 LineCollection 对象效率更高。 使用colorline 配方,您可以执行以下操作: import matplotlib.pyplot as plt import numpy as np import matplotlib.collections as mcoll import matplotlib.path as mpath def colorline( x, y, z=None, cmap=plt.get_cmap('copper'), norm=plt.Normalize(0.0, 1.0), ...
import numpy as np import matplotlib.pyplot as plt from matplotlib.collections import LineCollection from matplotlib.colors import ListedColormap, BoundaryNorm x = np.linspace(0, 3*np.pi, 500) y = np.sin(x) dydx= np.cos(0.5*(x[:-1]+x[1:])) # 两点之间的中点的导数 """ 这里的目的...
line.set_alpha(0.5) #调用Line2D对象的set_*()方法来设置属性值 同时绘制正弦和余弦两条曲线,lines是一个有两个Line2D对象的列表: lines = plt.plot(x, np.sin(x), x, np.cos(x)) 调用setp()以同时配置多个对象的属性,这里同时设置两条曲线的颜色和线宽: plt.setp(lines, color="r”, linewidth...
# adjust from https://stackoverflow.com/questions/38051922/how-to-get-differents-colors-in-a-single-line-in-a-matplotlib-figure import numpy as np, matplotlib.pyplot as plt from matplotlib.collections import LineCollection from matplotlib.colors import ListedColormap, BoundaryNorm # my func x = ...
xdata: 需要绘制的line中点的在x轴上的取值,若忽略,则默认为range(1,len(ydata)+1) ydata: 需要绘制的line中点的在y轴上的取值 linewidth: 线条的宽度 linestyle: 线型 color: 线条的颜色 marker: 点的标记,详细可参考markers API markersize: 标记的size ...