gpt4 book ai didi

python - 如何将颜色图绑定(bind)到数组?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:21 26 4
gpt4 key购买 nike

我想从一个数组值开始获取一组颜色。

例如:

a = [4,3,2,5,6,20,1,34]

我希望有一个新的颜色数组,就像 matplotlib 的 viridis cmap,其中小数字(相同索引)与深色绑定(bind),大数字与浅色绑定(bind)。

最佳答案

您可以使用 matplotlib.pyplot.Normalize 所以数据被标准化为 [0-1]提供给绘图功能时的间隔。这是使用标准化范围的示例

a = [4,3,2,5,6,20,1,34]
# An example colormap
colormap = plt.cm.cool
# Normalize using the min and max values in a
normalize = plt.Normalize(vmin=min(a), vmax=max(a))
# scatter plot with the parameters defined above
plt.scatter(range(len(a)), y=a, c=a, cmap=colormap, marker='o')

enter image description here

请注意,您必须使用 a作为 c matplotlib.pyplot.scatter 中的参数,如文档中所述,它接受:

Color, sequence, or sequence of color, optional

所以这样你将使用数组 a从颜色图中选择值,这些值又将由 ,ax 归一化和 min a 中的值.

关于python - 如何将颜色图绑定(bind)到数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651571/

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