如何将多个点映射到沿 y 轴的颜色渐变上?

How can I map multiple points onto a color gradient along the y-axis?

我的散点图值的值介于 0 到 1 之间,我试图以这样一种方式绘制它,即某个 y 值具有按强度排列的特定颜色。例如,在 viridis 色图中,我希望 0.90 左右的点为黄色,0.10 左右的点为深紫色。但是,我只得到这些结果。

import matplotlib
import matplotlib.pyplot as plt

#Generate the data
time = np.arange(50)
a = np.random.random(50)
b = np.ones((50, 50), float)
for t in time: 
    b[i, :] = a

#Creating a color mapping variable
z = np.linspace(0, 1, 50)

#Generating the plot
fig, ax = plt.subplots(figsize=(5,5))
for t in time:
    ax.scatter(np.ones(len(b[t,:])) * t, b[t, :], c = z, cmap = 'viridis', s = 5, vmin = 0, vmax = 1)

plt.show()

这是我的身材:

其中一个轴的值​​应该排序。

>>> x = np.random.rand(50)
>>> x.shape
(50,)
>>> y = np.linspace(0,1,50)
>>> y.shape
(50,)
>>> plt.scatter(x,y,c=np.linspace(0,1,50))
<matplotlib.collections.PathCollection object at 0x00000000149AB390>
>>> plt.show()

你们非常亲密。您只需使用 y 值作为 z 参数和 viridis 颜色图。因此,您唯一需要的修改是 c = b[t, :],

for t in time:
    ax.scatter(np.ones(len(b[t,:])) * t, b[t, :], c = b[t, :], cmap = 'viridis', 
               s = 5, vmin = 0, vmax = 1)