Диаграмма рассеяния Python: изменение цвета на основе значений X и Y - PullRequest
0 голосов
/ 06 сентября 2018

Я попытался воссоздать прикрепленное изображение с помощью cmaps, а также с помощью операторов if / else.

Example

Моя текущая попытка основана на совете, данном в этой теме

Я пытался использовать 1.8 <= x <= 2.2, но получаю ошибку. </p>

Вот мой текущий код ниже:

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 Ответ

0 голосов
/ 06 сентября 2018

Чтобы построить этот график, вам нужно передать массив с цветом каждой точки. В этом случае цвет - это расстояние до точки (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

...