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

วิธีวาดปลายเส้นโค้งมนใน Matplotlib

ในการวาดปลายเส้นโค้งมน (rounded line ends) ใน matplotlib เราสามารถใช้ solid_capstyle='round'

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างจุดข้อมูล x และ y แบบสุ่มด้วย numpy
  • สร้าง figure และชุด subplots
  • พลอต x และ y ด้วยเมธอด plot() พร้อม solid_capstyle ในอาร์กิวเมนต์
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import matplotlib.pyplot as plt
import numpy as np

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

x = np.random.randn(5)
y = np.random.randn(5)

fig, ax = plt.subplots()
ln, = ax.plot(x, y, lw=10, solid_capstyle='round', color='red')
plt.show()

ผลลัพธ์

วิธีวาดปลายเส้นโค้งมนใน Matplotlib