为了账号安全,请及时绑定邮箱和手机立即绑定

python / matplotlib-多色线

python / matplotlib-多色线

婷婷同学_ 2019-11-20 13:10:47
我正在尝试创建具有某些条件的彩色线条。基本上,我想使该线在y轴上指向下方时为红色,朝上时为绿色,而在两者都不指向时为蓝色。我使用了一些类似的示例,但我一直无法将它们转换为与轴上的plot()一起使用。只是想知道如何做到这一点。到目前为止,我想出了一些代码:#create x,y coordinatesx = numpy.random.choice(10,10)y = numpy.random.choice(10,10)#create an array of colors based on direction of line (0=r, 1=g, 2=b)colors = []#create an array that is one position away from original #to determine direction of line yCopy = list(y[1:])for y1,y2 in zip(y,yCopy):    if y1 > y2:        colors.append(0)    elif y1 < y2:        colors.append(1)    else:        colors.append(2)#add tenth spot to array as loop only does ninecolors.append(2)#create a numpy array of colorscategories = numpy.array(colors)#create a color map with the three colorscolormap = numpy.array([matplotlib.colors.colorConverter.to_rgb('r'),matplotlib.colors.colorConverter.to_rgb('g'),matplotlib.colors.colorConverter.to_rgb('b')])#plot linematplotlib.axes.plot(x,y,color=colormap[categories])不确定如何使plot()接受颜色数组。我总是会收到关于用作颜色的格式类型的错误。尝试了十六进制,十进制,字符串和浮点数。与scatter()完美搭配。谢谢
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 779 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信