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

การปรับระยะห่างระหว่างขอบกราฟและแกน X ใน Matplotlib


ใช้ tight_layout() หรือตั้งค่า bottom padding ของ figure ปัจจุบัน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล x และ y ด้วย numpy
  • พล็อต x และ y ด้วย plot()
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import numpy as np
import matplotlib.pyplot as plt

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

x = np.linspace(-2, 2, 100)
y = np.exp(x)
plt.plot(x, y, c='red', lw=1)
plt.show()

ผลลัพธ์

การปรับระยะห่างระหว่างขอบกราฟและแกน X ใน Matplotlib