gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:14:52 28 4
gpt4 key购买 nike

我的散点图值的值介于 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()

这就是我的身材:

Scatter plot image

最佳答案

你们非常接近。您只需使用 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)

enter image description here

关于python - 如何将多个点映射到沿 y 轴的颜色渐变上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com