What does the matplotlib `hold` keyword argument do?

The hold argument appears in many plt.fill_between functions, for example plt.fill_between , plt.arrow , plt.axhline (for import matplotlib.pyplot as plt ). However, I can not find the documentation. Documents are displayed only in the format :

Additional kwargs: hold = [True | False] overrides the default hold state

This is also a complicated keyword argument for google for ...

+7
python matplotlib
source share
2 answers

It seems to be based on the default construction of MATLAB , for which you need to call hold on to add more than one chart to the same chart. The default behavior for matplotlib seems to be true, consider

 import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,np.pi*2,1000) plt.plot(x,np.sin(x),hold=True) plt.plot(x,np.sin(x)**2,hold=True) plt.show() 

enter image description here

which displays both lines on the same chart. If the hold is set to false, the next plot call overwrites the previous one. For example,

 import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,np.pi*2,1000) plt.plot(x,np.sin(x),hold=True) plt.plot(x,np.sin(x)**2,hold=False) plt.show() 

enter image description here

+5
source share

From the documentation :

When true, subsequent graph commands will be added to the current axes. When hold is False, the current axis and shape will be cleared in the next chart command

The hold value determines whether future graphics will be drawn above the previous one (s), or if the drawing will be cleared before drawing.

+3
source share

All Articles