Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python绘制的点,每个点的颜色不同?_Python_Matplotlib_Plot - Fatal编程技术网

Python绘制的点,每个点的颜色不同?

Python绘制的点,每个点的颜色不同?,python,matplotlib,plot,Python,Matplotlib,Plot,python中的Matplotlib使用x和y值序列生成数据图 我们可以为正在绘制的特定图形指定颜色。然而,由于我们正在绘制一个显式的点列表,我想知道是否有一种方法可以分别指定每个点的颜色 例如,如何使绘制在下图中的每个点都具有特定的随机颜色 import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi, 400) y = np.sin(x ** 2) fig, ax = plt.subplots() ax.plot(x, y) ax.

python中的Matplotlib使用
x
y
值序列生成数据图

我们可以为正在绘制的特定图形指定颜色。然而,由于我们正在绘制一个显式的点列表,我想知道是否有一种方法可以分别指定每个点的颜色

例如,如何使绘制在下图中的每个点都具有特定的随机颜色

import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single color plot')

根据评论中的链接和建议,我为需要绘制不同颜色线段的情况拼凑了以下解决方案

理想情况下,我希望每个点都是一种特定的颜色,并且线段在这些各自的颜色之间的颜色光谱中平滑插值。但是我想matplotlib是自由软件,所以它没有最奇特的特性也就不足为奇了。当然,除非能做到,但我不知道怎么做

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=np.random.rand(len(segments), 3))
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()

编辑:

只是为了好玩,按照建议“手动”完成更多步骤并创建渐变,我们可以得到如下结果:

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = [0.]
while x[-1]<2 * np.pi :
    x.append(x[-1] +1/(100*np.sqrt(1+4*x[-1]*x[-1]*np.cos(x[-1]*x[-1])*np.cos(x[-1]*x[-1]))))
x = np.array(x)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
myColors = np.random.rand(len(segments), 3)
for i in range(len(segments)//10):
    for j in range(9):
        myColors[10*i+j+1] = myColors[10*i]+(myColors[10*(i+1)]-myColors[10*i])*(j+1)/10
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=myColors)
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()
导入matplotlib.pyplot作为plt
从matplotlib.collections导入LineCollection
x=[0]

当x[-1]遵循评论中的链接和建议时,我为需要绘制不同颜色线段的情况拼凑了以下解决方案

理想情况下,我希望每个点都是一种特定的颜色,并且线段在这些各自的颜色之间的颜色光谱中平滑插值。但是我想matplotlib是自由软件,所以它没有最奇特的特性也就不足为奇了。当然,除非能做到,但我不知道怎么做

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=np.random.rand(len(segments), 3))
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()

编辑:

只是为了好玩,按照建议“手动”完成更多步骤并创建渐变,我们可以得到如下结果:

import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
x = [0.]
while x[-1]<2 * np.pi :
    x.append(x[-1] +1/(100*np.sqrt(1+4*x[-1]*x[-1]*np.cos(x[-1]*x[-1])*np.cos(x[-1]*x[-1]))))
x = np.array(x)
y = np.sin(x ** 2)
xy = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.hstack([xy[:-1], xy[1:]])
myColors = np.random.rand(len(segments), 3)
for i in range(len(segments)//10):
    for j in range(9):
        myColors[10*i+j+1] = myColors[10*i]+(myColors[10*(i+1)]-myColors[10*i])*(j+1)/10
fig, ax = plt.subplots()
lc = LineCollection(segments, colors=myColors)
ax.add_collection(lc)
ax.autoscale()
ax.set_title('A multi-color plot')
plt.show()
导入matplotlib.pyplot作为plt
从matplotlib.collections导入LineCollection
x=[0]

当x[-1]我看不到点你不是在画点,而是在画一条线。如果要绘制点,可以使用
ax.scatter
。相反,如果您希望对各个线段进行着色,可以使用
LineCollection
,如中所示。@ImportanceOfBeingErnest哦,好的,如果散点图可以为每个点采用单独的颜色,我也很乐意使用散点图。我该怎么做呢?
ax.scatter(x,y,c=np.random.rand(len(x),3))
plot
创建一个
Line2D
对象,该对象为快速绘制而优化。让同一个命令
plot
根据输入返回不同的对象是不好的风格。这就是为什么有
LineCollection
,如果不同的线段应该具有不同的属性,可以使用它。我看不到点您不是在打印点,而是在打印线。如果要绘制点,可以使用
ax.scatter
。相反,如果您希望对各个线段进行着色,可以使用
LineCollection
,如中所示。@ImportanceOfBeingErnest哦,好的,如果散点图可以为每个点采用单独的颜色,我也很乐意使用散点图。我该怎么做呢?
ax.scatter(x,y,c=np.random.rand(len(x),3))
plot
创建一个
Line2D
对象,该对象为快速绘制而优化。让同一个命令
plot
根据输入返回不同的对象是不好的风格。这就是为什么会有
LineCollection
,如果不同的段应该具有不同的属性,就可以使用它。当然可以这样做。您可以将每个线段再次划分为几个较短的线段,每个线段在两个端点颜色之间插入一种颜色。@ImportanceOfBeingErnest哦,好的,我明白了。我有点希望会有某种自动搅拌机,这将使过程更简单。。。我的意思是,理想情况下,需要一个连续的颜色梯度,这对于“手工”应用的离散方法是不可行的。当然可以做到。您可以将每个线段再次划分为几个较短的线段,每个线段在两个端点颜色之间插入一种颜色。@ImportanceOfBeingErnest哦,好的,我明白了。我有点希望会有某种自动搅拌机,这将使过程更简单。。。我的意思是,理想情况下,人们需要一个连续的颜色梯度,这是不可行的离散方法应用“手”。