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

การทำ Logarithmic Y-axis bins ใน Python (Matplotlib)


ขั้นตอนการทำ Logarithmic Y-axis bins

  • สร้างจุด x และ y ด้วย NumPy
  • ตั้งค่า Scale ของ Y-axis ด้วยเมธอด yscale()
  • พล็อตจุด x และ y ด้วยเมธอด plot() พร้อม linestyle="dashdot" และ label="y=log(x)"
  • ใช้เมธอด legend() เพื่อเปิดใช้งาน Label ของเส้น
  • ใช้เมธอด show() เพื่อแสดง Figure

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

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.linspace(1, 100, 1000)
y = np.log(x)
plt.yscale('log')
plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)")
plt.legend()
plt.show()

ผลลัพธ์

การทำ Logarithmic Y-axis bins ใน Python (Matplotlib)