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

การควบคุมการหมุนเครื่องหมาย (Marker Orientation) ใน Matplotlib

Matplotlib รองรับ marker แบบ tuple: (num_sides, style, rotation) เช่น (3, 0, angle) สำหรับสามเหลี่ยมหมุนตามมุมที่กำหนด ใช้ได้กับ plt.plot() และ plt.scatter()

ขั้นตอนการทำงาน

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. สร้างข้อมูล x, y แบบสุ่ม
  3. สร้างอาร์เรย์มุมหมุนต่างกัน
  4. วนลูป zip(x, y, angles) พลอตแต่ละจุดด้วย marker tuple
  5. แสดงผล

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

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.random.rand(10)
y = np.random.rand(10)
angles = np.linspace(0, 10, 10)

for x, y, i in zip(x, y, angles):
    plt.plot(x, y, marker=(3, 0, i*45), linestyle='None', markersize=20)

plt.show()

ผลลัพธ์

การควบคุมการหมุนเครื่องหมาย (Marker Orientation) ใน Matplotlib