Python Pool: Matplotlib ion() in Python With Examples

Hello programmers, in this article, we will discuss the Matplotlib ion() in Python. Matplotlib is a multi-platform data visualization library built on NumPy array. The Pyplot module of the matplotlib library is designed to give visual access to several plots like line, bar, scatter, histogram, etc. The matplotlib.pyplot.ion() function is used to turn on the interactive mode. The interactive mode is turned off by default. After every change, some interactive backends dynamically update and pop up to users. You can check the status of interactive mode by running the plt.isinteractive() or plt.rcParams[‘interactive’] commands.

Contents of Tutorial

Syntax and Return type of Matplotlib ion:

matplotlib.pyplot.ion()

Parameters

It accepts no parameters. It just turns on the interactive mode.

Examples of Matplotlib ion() in Python

EXAMPLE 1:

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

OUTPUT:

Examples of Matplotlib ion()

EXPLANATION:

In the above example, the function is implemented just after importing the matplotlib library. This helps to turn on the interactive mode for the sample interactive plot created. Thus the ion() in python is turned on using the plt.ion() statement.

EXAMPLE 2:

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:

Another Example

EXPLANATION:

In the above example, two arrays – collection_1 and collection_ 2 are defined using the Numpy arrange() function. The arrays contain the points to plot the interactive plot. The interactive mode is turned on for the above example by implementing the line plt.ion().

Update plot with draw()

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10*np.pi, 100)
y = np.sin(x)

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, 'b-')

for phase in np.linspace(0, 10*np.pi, 100):
line1.set_ydata(np.sin(0.5 * x + phase))
fig.canvas.draw()

OUTPUT:

Update plot with draw()

EXPLANATION:

In the above example, we first create the data to plot using the following Numpy functions: x = np.linspace(0, 10*np.pi, 100) and y = np.sin(x). The interactive mode, i.e., ion() in python, is turned on. Setting interactive mode on is essential. It controls if the figure is redrawn for every draw() command. The figure does not update itself if the plt.ion() is False (the default). The plot is configured and finally updated in a loop.

Matplotlib ion() freeze

from matplotlib import pyplot as plt
import time

plt.ion()
fig = plt.figure()
axis = fig.add_subplot(111)

for i in range(30000):
    axis.plot(i,i,'o')
    plt.draw()
    if i > 5:
        time.sleep(2)   
plt.close()

EXPLANATION:

In the above code snippet, an interactive plot is made using the plt.ion() function. Some other parts of the code do some calculations, and the plot is updated frequently with calls to plt.draw(). On clicking the figure window of the interactively drawn plot and trying to move it around, the figure stops updating and Windows tags the process "Not Responding." But the script continues to run without a problem i.e., only the figure freezes.

Conclusion

This article talks about the Matplotlib ion() in Python. And examples of implementing the function in python programs. I turn on the interactive mode for various plots. Refer to this article for any queries related to the Matplotlib ion() function.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Author: Shantun Parmar

4 thoughts on “Python Pool: Matplotlib ion() in Python With Examples

  1. I’m just commenting to let you be aware of of the exceptional discovery my cousin’s daughter enjoyed going through your blog. She discovered lots of issues, most notably how it is like to have a very effective giving heart to have many more effortlessly fully understand certain extremely tough matters. You really did more than my desires. Many thanks for displaying the helpful, healthy, educational and also easy thoughts on the topic to Gloria.

  2. I am just writing to make you understand what a notable experience my wife’s girl had using your web site. She mastered so many pieces, which included what it is like to possess an excellent helping nature to let other people with ease learn some problematic things. You truly surpassed people’s expected results. Thank you for producing these precious, trusted, informative and in addition fun tips about this topic to Lizeth.

Thanks for your support, You may click on ads to encourage us which assits to writers.

Leave a Reply

Your email address will not be published.