gpt4 book ai didi

python - Matplotlib cmap 只显示灰色

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

我使用“tab20”颜色图在 python 上使用 matplotlib 进行绘图,代码如下:

colors=[str(float(year-1980)/(2017-1980)) for i in years];
fig,ax = plt.subplots()
ax.scatter(Topic[:,0],Topic[:,1],c=colors,cmap='tab20')

但是我得到的图完全是灰色的。可能是什么原因?

enter image description here

最佳答案

通过在 ax.scatter 调用中将字符串列表传递给 c,您可以告诉 matplotlib 将它们视为颜色格式字符串。由于字符串看起来像是代表 float ,因此它将它们视为灰度值。如果您传递 float 列表,它应该正确使用颜色图:

颜色 = [float((year-1980)/(2017-1980)) 代表年份]

参见the docs了解更多详细信息,特别是:

cmap : Colormap, optional, default: None

A Colormap instance or registered name. cmap is only used if c is an array of floats.

(此外,您不需要在第一行之后添加 ;。)

关于python - Matplotlib cmap 只显示灰色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828254/

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