如何根据 Matplotlib 中的某些变量更改数据点的颜色?

matplotlibserver side programmingprogramming更新于 2025/5/5 23:07:17

要根据 matplotlib 中的某些变量更改数据点的颜色,我们可以采取以下步骤 −

  • 使用 numpy 创建 x、y 和 c 变量。

  • 使用 x、y 绘制散点,并使用 c 绘制颜色(步骤 1)。

  • 要显示图像,请使用 show()  方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 20, 50)
y = np.log(x)
c = np.random.randint(x)
plt.scatter(x, y, c=c)
plt.show()

输出


相关文章