แสดง print และ plot สลับกันใน iPython notebook
ใช้ลูปสร้าง figure ใหม่ทีละรอบ พร้อม print statement
ขั้นตอน
- import pyplot จาก matplotlib
- สร้าง list ข้อมูลสำหรับ hist plots
- กำหนดตัวแปร i สำหรับ print
- วนลูปข้อมูล
- สร้าง figure และ subplots ด้วย subplots()
- วาง print statement
- วาด histogram ด้วย hist()
- เพิ่ม i ขึ้น 1
ตัวอย่างโค้ด (iPython)
In [1]: from matplotlib import pyplot as plt
In [2]: myData = [[7, 8, 1], [2, 5, 2]]
In [3]: i = 0
In [4]: for data in myData:
...: fig, ax = plt.subplots()
...: print("data number i =", i)
...: ax.hist(data)
...: i = i + 1
...:
data number i = 0
data number i = 1
In [5]:
ผลลัพธ์
data number i = 0 data number i = 1