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

วิธีตั้งค่าแกน Y เป็นเรเดียนใน Python Plot

ในการตั้งค่าแกน Y เป็นเรเดียนใน Python plot เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • สร้างจุดข้อมูล x และ y ด้วย numpy
  • สร้าง figure ใหม่หรือเปิดใช้งาน figure ที่มีอยู่ด้วยเมธอด figure()
  • เพิ่ม axes ax ให้กับ figure เป็นส่วนหนึ่งของ subplot arrangement
  • รับลิสต์ของ Y-axis ticks และ ticklabels
  • ตั้งค่า ticks และ ticklabels โดยใช้เมธอด set_yticks() และ set_yticklabels()
  • เพื่อแสดงรูป ใช้เมธอด 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.arange(-10.0, 10.0, 0.1)
y = np.arctan(x)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

unit = 0.25
y_tick = np.arange(-0.5, 0.5 + unit, unit)
y_label = [r"$-\frac{\pi}{2}$", r"$-\frac{\pi}{4}$", r"$0$", r"$+\frac{\pi}{4}$", r"$+\frac{\pi}{2}$"]

ax.set_yticks(y_tick * np.pi)
ax.set_yticklabels(y_label, fontsize=10)
plt.show()

ผลลัพธ์

วิธีตั้งค่าแกน Y เป็นเรเดียนใน Python Plotวิธีตั้งค่าแกน Y เป็นเรเดียนใน Python Plot