修正 Matplotlib 散点图中的颜色


要修正 Matplotlib 中散点图的颜色,我们可以按照以下步骤进行操作

  • 使用 numpy 创建 xs 和 ys 随机数据点。

  • 使用十六进制字母创建一组颜色,等于 ys 的长度。

  • 使用颜色列表,利用 scatter() 方法绘制列表 xs 和 ys。

  • 若要显示图形,使用 show() 方法。

示例

import random
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
xs = np.random.rand(100)
ys = np.random.rand(100)
colors = ["#" + ''.join([random.choice('0123456789ABCDEF') for j in range(6)])
for i in range(len(xs))]
plt.scatter(xs, ys, c=colors)
plt.show()

产出

更新于: 08-05-2021

843 次浏览

开启你的 职业生涯

完成课程认证

开始
广告