Monday, November 18, 2024
Google search engine
HomeLanguagesMatplotlib.pyplot.ion() in Python

Matplotlib.pyplot.ion() in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. One of the greatest benefits of visualization is that it allows us visual access to huge amounts of data in easily digestible visuals. Matplotlib consists of several plots like line, bar, scatter, histogram etc.
The matplotlib.pyplot.ion() is used to turn on interactive mode. To check the status of interactive mode one can run the below commands, 
 

plt.rcParams['interactive']

or, this command
 

plt.isinteractive()

Matplotlib also interacts with different backends behind the scene. The workhorse behind rendering charts in matplotlib is its backends. Some interactive backends dynamically update and pop up to users after every change. By default, the interactive mode is turned off.
Syntax: 
 

matplotlib.pyplot.ion()

It accepts no parameters.
Example 1: 
 

Python3




import matplotlib.pyplot as plt
  
#the function to turn on interactive mode
plt.ion()
 
#creating randomly generate collections/data
random_array = np.arange(-4, 5)
collection_1 = random_array ** 2
collection_2 = 10 / (random_array ** 2 + 1)
figure, axes = plt.subplots()
 
axes.plot(random_array, collection_1,
          'rx', random_array,
          collection_2, 'b+',
          linestyle='solid')
 
axes.fill_between(random_array,
                  collection_1,
                  collection_2,
                  where=collection_2>collection_1,
                  interpolate=True,
                  color='green', alpha=0.3)
 
lgnd = axes.legend(['collection-1',
                    'collection-2'],
                   loc='upper center',
                   shadow=True)
 
lgnd.get_frame().set_facecolor('#ffb19a')


Output: 
 

matplotlib.pyplot.ion()

  
Example 2: 
 

Python3




import matplotlib.pyplot as plt
 
plt.ion()
plt.plot([1.4, 2.5])
plt.title(" Sample interactive plot")
 
axes = plt.gca()
axes.plot([3.1, 2.2])


Output: 
 

matplotlib.pyplot.ion()

 

RELATED ARTICLES

Most Popular

Recent Comments