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

Logscale Plot ที่มีค่าศูนย์ใน Matplotlib

Log scale ปกติไม่รองรับค่า 0 หรือติดลบ ใช้ plt.xscale('symlog') และ plt.yscale('symlog') แทน (Symmetric Log) ซึ่งรองรับค่า 0 และติดลบได้โดยใช้สเกล log แบบสมมาตร

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

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. พลอตข้อมูลที่มีค่า 0
  3. ตั้งค่า xscale และ yscale เป็น 'symlog'
  4. แสดงผล

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

from matplotlib import pyplot as plt

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

plt.plot([0, 1, 2, 0, 3], [1, 0, 2, 3, 5], marker='o', linestyle='-')
plt.yscale('symlog')
plt.xscale('symlog')
plt.show()

ผลลัพธ์

Logscale Plot ที่มีค่าศูนย์ใน Matplotlib