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

วิธี unset 'sharex' หรือ 'sharey' จากสอง axes ใน Matplotlib

ในการ unset sharex และ sharey จากสอง axes ใน matplotlib สามารถใช้ 'none' คือ False หรือ 'none' แกน X หรือ Y ของแต่ละ subplot จะเป็นอิสระต่อกัน

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • เริ่มต้นตัวแปร rows และ cols

  • สร้าง figure และชุด subplots

  • วนลูป axes ที่ rows=2 และ cols=4

  • วาดข้อมูลสุ่มบน axis

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
rows = 2
cols = 4
fig, axes = plt.subplots(rows, cols, sharex='none', sharey='none', squeeze=False)
for row in range(rows):
    for col in range(cols):
        axes[row][col].plot(np.random.rand(10), np.random.rand(10))
plt.show()

ผลลัพธ์

จะแสดง grid 2x4 subplots แต่ละอันมีแกน X และ Y อิสระ ไม่ share scale

วิธี unset  sharex  หรือ  sharey  จากสอง axes ใน Matplotlib