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

วิธีแสดง print statements สลับกับ Matplotlib plots แบบ inline ใน iPython


แสดง print และ plot สลับกันใน iPython notebook

ใช้ลูปสร้าง figure ใหม่ทีละรอบ พร้อม print statement

ขั้นตอน

  1. import pyplot จาก matplotlib
  2. สร้าง list ข้อมูลสำหรับ hist plots
  3. กำหนดตัวแปร i สำหรับ print
  4. วนลูปข้อมูล
  5. สร้าง figure และ subplots ด้วย subplots()
  6. วาง print statement
  7. วาด histogram ด้วย hist()
  8. เพิ่ม 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