python画图一条线两种颜色_Python:如何以不同的颜色绘制一条线

545845e900013e3e02200220-100-100.jpg

LEATH

从此示例复制:import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectionfrom matplotlib.colors import ListedColormap, BoundaryNormx = np.linspace(0, 3 * np.pi, 500)y = np.sin(x)z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative# Create a colormap for red, green and blue and a norm to color# f' < -0.5 red, f' > 0.5 blue, and the rest greencmap = ListedColormap(['r', 'g', 'b'])norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)# Create a set of line segments so that we can color them individually# This creates the points as a N x 1 x 2 array so that we can stack points# together easily to get the segments. The segments array for line collection# needs to be numlines x points per line x 2 (x and y)points = np.array([x, y]).T.reshape(-1, 1, 2)segments = np.concatenate([points[:-1], points[1:]], axis=1)# Create the line collection object, setting the colormapping parameters.# Have to set the actual values used for colormapping separately.lc = LineCollection(segments, cmap=cmap, norm=norm)lc.set_array(z)lc.set_linewidth(3)fig1 = plt.figure()plt.gca().add_collection(lc)plt.xlim(x.min(), x.max())plt.ylim(-1.1, 1.1)plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值