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

การจัดการพื้นที่แนวนอนระหว่าง Subplots ใน Matplotlib


ใช้ wspace=1 ใน subplots_adjust() โดยไม่ใช้ tight layout

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

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างข้อมูล x และ y ด้วย numpy
  • สร้าง figure และชุด subplots 4 ช่อง
  • ปรับพื้นที่แนวนอนด้วย wspace=1
  • ใช้เมธอด 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(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
fig.subplots_adjust(wspace=1)
plt.show()

ผลลัพธ์

การจัดการพื้นที่แนวนอนระหว่าง Subplots ใน Matplotlib