Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách hiển thị câu lệnh print xen kẽ với đồ thị Matplotlib inline trong iPython

Trong iPython/Jupyter, print và plot xuất hiện theo thứ tự thực thi.

  • Import pyplot.
  • myData = [[7,8,1], [2,5,2]].
  • i = 0.
  • Vòng lặp: fig, ax = subplots(); print("data number i =", i); ax.hist(data); i += 1.

Ví dụ

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]:

Kết quả

data number i = 0
data number i = 1

Hai figure histogram hiển thị xen kẽ với câu print.