12 Jan 2021

Instant Update Plots in Jupyter Notebook

收藏到CSDN网摘

Jupyter Notebook is a very effective toolkit for both teaching and learning. The interactive mode is straightforward for demonstrating the logic behind the code blocks and explaining how the programme works segment by segment. With the help of powerful Matlab-equivalent matplotlib library, data plotting can be easilly implemented. However, from time to time, you might miss Matlab's `hold on` capability that enables the user draw on and update the plot instantly without producing multiple figures that flood your screen. In Jupyter notebook, if the plt.show() was called without a loop it will produce multiple outputs as many as your loop count, which is not convenient. To make it works in the same way as Matlab's `hold on`, all we need to do is: to use `IPython` display module to clear the output before plotting the new data. To show the progress, history data need to be saved in a list as shown below.
from IPython import display
import matplotlib.pyplot as plt

display.clear_output(wait=True)
plt.plot(ex,ey,'r-*')
plt.xlim(0,num_epoch)
plt.ylim(0,0.2)
plt.show()

No comments :

Post a Comment