python/matplotlib-多色线

python/matplotlib-多色线,python,matplotlib,Python,Matplotlib,我试图在一定条件下创建一条彩色线条。基本上,我想有红色的线,当指向y轴,绿色时,指向上,蓝色时,两者都没有 我研究了一些类似的例子,但我从未能够将它们转换为轴上的plot()。只是想知道如何才能做到这一点 以下是我迄今为止提出的一些代码: #create x,y coordinates x = numpy.random.choice(10,10) y = numpy.random.choice(10,10) #create an array of colors based on directi

我试图在一定条件下创建一条彩色线条。基本上,我想有红色的线,当指向y轴,绿色时,指向上,蓝色时,两者都没有

我研究了一些类似的例子,但我从未能够将它们转换为轴上的plot()。只是想知道如何才能做到这一点

以下是我迄今为止提出的一些代码:

#create x,y coordinates
x = 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 nine
colors.append(2)

#create a numpy array of colors
categories = numpy.array(colors)

#create a color map with the three colors
colormap = numpy.array([matplotlib.colors.colorConverter.to_rgb('r'),matplotlib.colors.colorConverter.to_rgb('g'),matplotlib.colors.colorConverter.to_rgb('b')])

#plot line
matplotlib.axes.plot(x,y,color=colormap[categories])
#创建x,y坐标
x=numpy.random.choice(10,10)
y=numpy.random.choice(10,10)
#根据线的方向(0=r,1=g,2=b)创建一个颜色数组
颜色=[]
#创建一个与原始阵列相距一个位置的阵列
#确定直线的方向
yCopy=列表(y[1:])
对于拉链中的y1、y2(y、yCopy):
如果y1>y2:
颜色。追加(0)
elif y1
不确定如何使plot()接受颜色数组。对于用作颜色的格式类型,我总是会遇到错误。尝试了十六进制、十进制、字符串和浮点。适用于scatter()


谢谢

我不认为您可以在
绘图
中使用颜色数组(文档中说颜色可以是任何matlab颜色,而
散点
文档中说您可以使用数组)

但是,您可以通过分别绘制每条线来伪造它:

import numpy
from matplotlib import pyplot as plt

x = range(10)
y = numpy.random.choice(10,10)
for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if y1 > y2:
        plt.plot([x1, x2], [y1, y2], 'r')
    elif y1 < y2:
        plt.plot([x1, x2], [y1, y2], 'g')
    else:
        plt.plot([x1, x2], [y1, y2], 'b')

plt.show()
导入numpy
从matplotlib导入pyplot作为plt
x=范围(10)
y=numpy.random.choice(10,10)
对于拉链中的x1,x2,y1,y2(x,x[1:],y,y[1:]):
如果y1>y2:
plt.图([x1,x2],[y1,y2],'r')
elif y1

我认为在
绘图中不能使用颜色数组(文档中说颜色可以是任何matlab颜色,而
散点
文档中说可以使用数组)

但是,您可以通过分别绘制每条线来伪造它:

import numpy
from matplotlib import pyplot as plt

x = range(10)
y = numpy.random.choice(10,10)
for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if y1 > y2:
        plt.plot([x1, x2], [y1, y2], 'r')
    elif y1 < y2:
        plt.plot([x1, x2], [y1, y2], 'g')
    else:
        plt.plot([x1, x2], [y1, y2], 'b')

plt.show()
导入numpy
从matplotlib导入pyplot作为plt
x=范围(10)
y=numpy.random.choice(10,10)
对于拉链中的x1,x2,y1,y2(x,x[1:],y,y[1:]):
如果y1>y2:
plt.图([x1,x2],[y1,y2],'r')
elif y1

好的。所以我想出了如何使用LineCollecion在轴上画线

import numpy as np
import pylab as pl
from matplotlib import collections  as mc

segments = []
colors = np.zeros(shape=(10,4))
x = range(10)
y = np.random.choice(10,10)
i = 0

for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if y1 > y2:
        colors[i] = tuple([1,0,0,1])
    elif y1 < y2:
        colors[i] = tuple([0,1,0,1])
    else:
        colors[i] = tuple([0,0,1,1])
    segments.append([(x1, y1), (x2, y2)])
    i += 1     

lc = mc.LineCollection(segments, colors=colors, linewidths=2)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.1)
pl.show()
将numpy导入为np
将pylab作为pl导入
从matplotlib将集合导入为mc
段=[]
颜色=np.零(形状=(10,4))
x=范围(10)
y=np.随机选择(10,10)
i=0
对于拉链中的x1,x2,y1,y2(x,x[1:],y,y[1:]):
如果y1>y2:
颜色[i]=元组([1,0,0,1])
elif y1

好的。所以我想出了如何使用LineCollecion在轴上画线

import numpy as np
import pylab as pl
from matplotlib import collections  as mc

segments = []
colors = np.zeros(shape=(10,4))
x = range(10)
y = np.random.choice(10,10)
i = 0

for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if y1 > y2:
        colors[i] = tuple([1,0,0,1])
    elif y1 < y2:
        colors[i] = tuple([0,1,0,1])
    else:
        colors[i] = tuple([0,0,1,1])
    segments.append([(x1, y1), (x2, y2)])
    i += 1     

lc = mc.LineCollection(segments, colors=colors, linewidths=2)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.1)
pl.show()
将numpy导入为np
将pylab作为pl导入
从matplotlib将集合导入为mc
段=[]
颜色=np.零(形状=(10,4))
x=范围(10)
y=np.随机选择(10,10)
i=0
对于拉链中的x1,x2,y1,y2(x,x[1:],y,y[1:]):
如果y1>y2:
颜色[i]=元组([1,0,0,1])
elif y1

有一个演示如何使用
线集合
绘制多色线的程序

剩下的问题是获取线条集合的颜色。因此,如果要比较的值是
y

cm = dict(zip(range(-1,2,1),list("gbr")))
colors = list( map( cm.get , np.sign(np.diff(y))  ))
完整代码:

import numpy as np; np.random.seed(5)
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

x = np.arange(10)
y = np.random.choice(10,10)

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

cm = dict(zip(range(-1,2,1),list("rbg")))
colors = list( map( cm.get , np.sign(np.diff(y))  ))

lc = LineCollection(segments, colors=colors, linewidths=2)
fig, ax = plt.subplots()
ax.add_collection(lc)

ax.autoscale()
ax.margins(0.1)
plt.show()

有一个演示如何使用
线集合
绘制多色线的程序

剩下的问题是获取线条集合的颜色。因此,如果要比较的值是
y

cm = dict(zip(range(-1,2,1),list("gbr")))
colors = list( map( cm.get , np.sign(np.diff(y))  ))
完整代码:

import numpy as np; np.random.seed(5)
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

x = np.arange(10)
y = np.random.choice(10,10)

points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)

cm = dict(zip(range(-1,2,1),list("rbg")))
colors = list( map( cm.get , np.sign(np.diff(y))  ))

lc = LineCollection(segments, colors=colors, linewidths=2)
fig, ax = plt.subplots()
ax.add_collection(lc)

ax.autoscale()
ax.margins(0.1)
plt.show()

lol。这将是我的下一次尝试。谢谢你的回复!实际上,它可以通过LineCollection完成,但我不知道如何使它适应我的需要。哈哈。这将是我的下一次尝试。谢谢你的回复!实际上,它可以通过LineCollection完成,但我不知道如何使它适应我的需要。