Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีดึงสีของเส้นที่พล็อตล่าสุดใน Python (Matplotlib)


ขั้นตอนการดึงสีของเส้นที่พล็อตล่าสุด

  • สร้างจุด x และ y ด้วย NumPy
  • พล็อตเส้นด้วย x และ y สีแดง ความหนา 2
  • ใช้เมธอด get_color() เพื่อดึงสีของเส้น และพิมพ์ออกมา
  • ใช้เมธอด show() เพื่อแสดง Figure

ตัวอย่างโค้ด

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