Python散点图:基于X和Y值更改颜色

3

我已经尝试使用Cmaps和if/else语句重新创建附加的图片。

Example

我的当前尝试是基于这个帖子中给出的建议。

我尝试使用1.8<=x<=2.2,但是出现了错误。

下面是我当前的代码:

import numpy as np
import matplotlib.pyplot as plt
N = 500
# center, variation, number of points
x = np.random.normal(2,0.2,N)
y = np.random.normal(2,0.2,N)
colors = np.where(x<=2.2,'r',np.where(y<=2.2,'b','b'))
plt.scatter(x , y, c=colors)
plt.colorbar()
plt.show()
1个回答

1
要制作该图,您需要传递一个包含每个点颜色的数组。在这种情况下,颜色是到点(2,2)的距离,因为分布以该点为中心。
import numpy as np
import matplotlib.pyplot as plt

N = 500
# center, variation, number of points
x = np.random.normal(2,0.2,N)
y = np.random.normal(2,0.2,N)

# we calculate the distance to (2, 2).
# This we are going to use to give it the color.
color = np.sqrt((x-2)**2 + (y-2)**2)

plt.scatter(x , y, c=color, cmap='plasma', alpha=0.7)
# we set a alpha
# it is what gives the transparency to the points.
# if they suppose themselves, the colors are added.

plt.show()

plot


非常感谢你,卢卡斯。我认为当我使用反向颜色映射时,它已经足够接近了。 - Haribon66

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接