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

การพล็อต Yscale แบบ Linear, Log, Logit และ Symlog ใน Matplotlib


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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล y ด้วย numpy
  • สร้างข้อมูล x ด้วย numpy
  • เพิ่ม subplot ลงใน figure ที่ index 1
  • พล็อต x และ y ด้วย plot()
  • สำหรับ linear ใช้ yscale("linear")
  • ตั้งชื่อเรื่อง subplot ทำซ้ำขั้นตอน 4-5 ด้วย index, yscale และ title ต่างกัน
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import numpy as np
import matplotlib.pyplot as plt

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

y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('symlog')

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')

plt.show()

ผลลัพธ์

การพล็อต Yscale แบบ Linear, Log, Logit และ Symlog ใน Matplotlib