Sometimes, we want to plot in a non-blocking way with Python Matplotlib.
In this article, we’ll look at how to plot in a non-blocking way with Python Matplotlib.
How to plot in a non-blocking way with Python Matplotlib?
To plot in a non-blocking way with Python Matplotlib, we can use the draw
method.
For instance, we write
import numpy as np
from matplotlib import pyplot as plt
def main():
plt.axis([-50,50,0,10000])
plt.ion()
plt.show()
x = np.arange(-50, 51)
for pow in range(1, 5):
y = [n**pow for n in x]
plt.plot(x, y)
plt.draw()
plt.pause(0.001)
input("Press [enter] to continue.")
if __name__ == '__main__':
main()
to create the NumPy array x
with the values for the x-axis.
Then we create y
with the values in x
raised to the pow
power to create the values for the y-axis.
And then we call plot
to plot with x
and y
.
Next, we call draw
to draw the plot in a non-blocking way.
And then we call pause
to pause plotting until enter is pressed.
Conclusion
To plot in a non-blocking way with Python Matplotlib, we can use the draw
method.