我发现了一段将1D Numpy数组传递给MatplotLib的代码。数组的值为1或0,但绘制的图表颜色为黄色或紫色。我无法找到任何相关文档。
以下是代码:
import numpy as np
import matplotlib.pyplot as plt
num_observations = 5000
x1 = np.random.multivariate_normal([0, 0], [[1, .85],[.85, 1]], num_observations) # mean, covariance
x2 = np.random.multivariate_normal([1, 4], [[1, .85],[.85, 1]], num_observations)
features = np.vstack((x1, x2)).astype(np.float32)
labels = np.hstack((np.zeros(num_observations),np.ones(num_observations)))
plt.figure(figsize=(12,8))
plt.scatter(features[:, 0], features[:, 1],
c = labels, alpha = .4)
plt.show()
任何人都可以解释我们如何将颜色变为黄色和紫色?相关文档也会有所帮助。
答案 0 :(得分:1)
它使用默认的viridis
色图,因此紫色代表0而黄色代表1.有关色彩映射的更多信息,请参阅此处:https://matplotlib.org/examples/color/colormaps_reference.html。
添加颜色栏有助于此。在示例中添加一个很简单:
import numpy as np
import matplotlib.pyplot as plt
num_observations = 5000
x1 = np.random.multivariate_normal([0, 0], [[1, .85],[.85, 1]], num_observations) # mean, covariance
x2 = np.random.multivariate_normal([1, 4], [[1, .85],[.85, 1]], num_observations)
features = np.vstack((x1, x2)).astype(np.float32)
labels = np.hstack((np.zeros(num_observations),np.ones(num_observations)))
plt.figure(figsize=(12,8))
p = plt.scatter(features[:, 0], features[:, 1],
c = labels, alpha = .4)
plt.colorbar(p)
plt.show()