Computer >> Computer tutorials >  >> Programming >> Python

How to display print statements interlaced with Matplotlib plots inline in iPython?


To display print statements interlaced with matplotlib plots inline in iPython, we can take the following steps.

Steps

  • Import pyplot from matplotlib.

  • Make a list of data for hist plots.

  • Initialize a variable "i" to use in print statement.

  • Iterate the list of data (Step 2).

  • Create a figure and a set of subplots using subplots() method.

  • Place print statement.

  • Plot the histogram using hist() method.

  • Increase "i" by 1.

Example

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

Output

data number i = 0
data number i = 1