[英]Python. Color coding elements for scatter plot
I have a simulation set up for traffic flow and i require a visualisation of the system for which ive used a scatter plot. 我已经为交通流进行了仿真,并且我需要对我使用散点图的系统进行可视化。 I am looking for a way to give each element in my array a different color but one that is constant as my program loops
我正在寻找一种为数组中的每个元素赋予不同颜色但在程序循环时保持不变的颜色的方法
You can set the colour of scatter plot points using c=...
in the call to scatter
: 您可以使用设置的散点图点颜色
c=...
在调用scatter
:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 11, 12, 11, 9]
z = [2, 4, 4, 1, 1]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, c=z, linewidth=0)
plt.show()
To give each point its own colour simply use range(len(x))
for the colours: 要给每个点自己的颜色,只需对颜色使用
range(len(x))
:
x = [1, 2, 3, 4, 5]
y = [10, 11, 12, 11, 9]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, c=range(len(x)), linewidth=0)
plt.show()
Looking at the plt.scatter
documentation, one finds that the c
argument can be used for setting the color of the scatter points. 查看
plt.scatter
文档,发现c
参数可用于设置散点的颜色。
c : color, sequence, or sequence of color, optional, default: 'b'c:颜色,序列或颜色序列,可选,默认值:“ b” \n c can be a single color format string, or a sequence of color specifications
c可以是单个颜色格式字符串,也可以是颜色规范序列 \n of length N, or a sequence of N numbers to be mapped to colors using
长度为N的序列,或使用以下方式映射到颜色的N个数字序列 \n the cmap and norm specified via kwargs (see below).
通过kwargs指定的cmap和规范(请参见下文)。
So, in order to obtain a constant color for each scatter point, there are two options: 因此,为了获得每个散点的恒定颜色,有两种选择:
plt.scatter(x,y, c=["blue", "red", "green"])
plt.scatter(x,y, c=[3.4, 5.6, 7.9, 1.0], cmap="jet", vmin=0, vmax=10)
or using a Normalize instance 或使用Normalize实例
norm = matplotlib.colors.Normalize(vmin=0, vmax=10)
plt.scatter(x,y, c=[3.4, 5.6, 7.9, 1.0], cmap="jet", norm=norm)
Without the normalization, the colors from the colormap would be distributed according the the minimum and maximum value in the array that is given to c
. 如果不进行归一化,则将根据给
c
的数组中的最小值和最大值来分配颜色图中的颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.