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

การนำ DataFrame.plot ของ Pandas ไปวางใน Matplotlib Subplot


เพื่อนำกราฟจาก Pandas DataFrame.plot ไปวางใน Matplotlib subplot

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง figure และ subplots สองแกน
  • สร้าง Pandas DataFrame
  • ใช้ DataFrame.plot() ระบุ ax
  • แสดงรูปด้วย show()

ตัวอย่าง

import pandas as pd
import matplotlib.pyplot as plt

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

fig, (ax1, ax2) = plt.subplots(2)
df = pd.DataFrame(dict(name=["Joe", "James", "Jack"], age=[23, 34, 26]))
df.set_index("name").plot(ax=ax1)
df.set_index("name").plot(ax=ax2)
plt.show()

ผลลัพธ์

การนำ DataFrame.plot ของ Pandas ไปวางใน Matplotlib Subplot