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

วิธีได้สีสลับกันในเส้นประ (Dashed Line) ด้วย Matplotlib


เพื่อได้สีสลับกันในเส้นประด้วย Matplotlib

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • ดึง axis ปัจจุบัน
  • สร้างข้อมูล x และ y ด้วย numpy
  • พลอต x และ y ด้วย linestyle - และ --
  • แสดงรูปด้วย show()

ตัวอย่าง

from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

ax = plt.gca()
x = np.linspace(-10, 10, 100)
y = np.sin(x)
ax.plot(x, y, '-', color='red', linewidth=5)
ax.plot(x, y, '--', color='yellow', linewidth=5)
plt.show()

ผลลัพธ์

วิธีได้สีสลับกันในเส้นประ (Dashed Line) ด้วย Matplotlib