如何在 Python 中获取最新绘制的线段的颜色?


要获取最新绘制线段的颜色,我们可以执行以下步骤 −

  • 用 numpy 创建 x 和 y 点。

  • 用 x 和 y 绘制线段,颜色设为红色,线宽设为 2。

  • 要获取线段的颜色,请使用get_color() 方法,并打印出来。

  • 要显示图形,请使用 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, 10, 1000)
y = np.linspace(10, 20, 1000)
line, = plt.plot(x, y, c="red", lw=2)
print("Color of the most recent plot line: ", line.get_color())
plt.show()

输出

Color of the most recent plot line: red

更新日期: 09-Apr-2021

3K+ 浏览量

开启您的职业生涯

通过完成课程获得认证

开始
广告