为 matplotlib Axes.scatter 指定颜色级别?

问题描述

如果我正在做一个散点图,点的颜色根据幅度着色,我可以像在轮廓和轮廓中一样指定颜色级别吗?

例如我这样做:

from random import randrange
import matplotlib.pyplot as plt

x,t,t  = [],[],[]
for x in range(10):
     y.append(randrange(30,45,1))
     x.append(randrange(50,65,1))
     t.append(randrange(0,20,1))

fig = plt.figure()
ax = fig.add_subplot()
scats = ax.scatter(x,y,s=30,c=t,marker = 'o',cmap = "hsv")
plt.show()

产生这样的情节:

enter image description here

但是有什么方法可以在散点图中添加“级别”类型的参数?在那里我可以将我的级别指定为 [0,4,8,12,16,20] 或其他什么?

解决方法

这是一种方法,您的代码中也有多个错误,我已修复:您定义了 t 两次,但根本没有定义 y,您覆盖了 {{1} } 使用您的循环变量,您应该将其替换为 x,这是 Python 中对于您不会使用的变量的约定。

_