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

วิธีโค้งข้อความใน Polar Plot ด้วย Matplotlib

การวาดข้อความโค้งตามมุมใน Polar Plot สามารถทำได้โดยการสร้างเส้นโค้งจากจุดข้อมูล แล้ววางข้อความตามเส้นนั้น

ขั้นตอน

  1. ตั้งค่าขนาด Figure และ Padding
  2. สร้าง Figure และเพิ่ม Subplot แบบ Polar projection
  3. วาดเส้นรัศมีที่มุมต่างๆ (0°, 90°, 360°)
  4. สร้างข้อมูลโค้งด้วย np.linspace และ interp1d สำหรับการ보간
  5. วาดโค้งด้วย ax.plot()
  6. แสดงผลด้วย plt.show()

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

from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np

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

fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")

# วาดเส้นรัศมี
for degree in [0, 90, 360]:
    rad = np.deg2rad(degree)
    ax.plot([rad, rad], [0, 1], color="green", linewidth=2)

# วาดโค้งตัวอย่าง
for curve in [[[0, 90], [0.45, 0.75]]]:
    curve[0] = np.deg2rad(curve[0])
    x = np.linspace(curve[0][0], curve[0][1], 500)
    y = interp1d(curve[0], curve[1])(x)
    ax.plot(x, y, lw=7, color='red')

plt.show()

ผลลัพธ์

จะได้ Polar Plot พร้อมเส้นรัศมีสีเขียวและโค้งสีแดง

วิธีโค้งข้อความใน Polar Plot ด้วย Matplotlib