我是Python新手。我在Jupyter Notebook中运行了以下代码行,我正在做一个散点图:
colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]
当我运行这段代码时,Python会抛出以下错误消息:
TypeError Traceback (most recent call last)
<ipython-input-9-ec6675e13d1f> in <module>()
----> 1 colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]
<ipython-input-9-ec6675e13d1f> in <listcomp>(.0)
----> 1 colors = ["#%02x%02x%02x" %(r,g,150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]
TypeError: %x format: an integer is required, not numpy.float64
导致此错误的原因是什么?如何解决?
答案 0 :(得分:0)
感谢Andrew Li和&#34; dsgdfg&#34;指出我正确的方向。
将代码更改为以下内容可以解决问题:
["#%02x%02x%02x" %(int(r),int(g),150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))]