Open In App

Matplotlib.pyplot.draw() in Python

Last Updated : 19 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

matplotlib.pyplot.draw() function redraw the current figure in Matplotlib. Unlike plt.show(), it does not block the execution of code, making it especially useful in interactive sessions, real-time visualizations where the plot needs to update dynamically without pausing the program. Example:

Python
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [4, 5, 6])
plt.draw()
plt.pause(2)  

Output

Output
Lineplot

Explanation: A simple line plot is created with plt.plot([1, 2, 3], [4, 5, 6]), connecting points (1,4), (2,5) and (3,6). plt.draw() renders the plot without blocking execution and plt.pause(2) keeps it visible for 2 seconds.

Syntax

matplotlib.pyplot.draw()

Parameter: This function does not take any arguments.

Returns: This function does not return anything. It updates/redraws the current figure in-place.

Examples

Example 1: In this example, we create a simple plot, display it with an initial title and then update the title after a pause to reflect changes without blocking execution.

Python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 5])
plt.title("Initial Title")
plt.draw()
plt.pause(1)

ax.set_title("Updated Title")
plt.draw()
plt.pause(2)

Output

Output
Simple plot

Explanation: The plot is first drawn with a title using plt.draw(). The title is then updated and plt.draw() is called again to reflect the change immediately.

Example 2: This example demonstrates how plt.draw() can be used in interactive mode to update the plot in each iteration of a loop with real-time data changes.

Python
import matplotlib.pyplot as plt
import numpy as np

plt.ion()  # Turn on interactive mode
fig, ax = plt.subplots()

for i in range(5):
    ax.clear()
    x = np.linspace(0, 2*np.pi, 100)
    y = np.sin(x + i)
    ax.plot(x, y)
    ax.set_title(f"Frame {i}")
    plt.draw()
    plt.pause(0.5)

Output

Output
Real-time Plot

Explanation: In interactive mode, the plot is cleared and redrawn with updated data in each iteration. plt.draw() reflects the updates in real-time without blocking code execution.

Example 3: Here, we rotate a 3D wireframe plot by updating the view angle in a loop using plt.draw() and plt.pause() to show animation-like behavior.

Python
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection ='3d')

X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride = 5, 
                  cstride = 5)

for angle in range(0, 360):
    ax.view_init(30, angle)
    plt.draw()
    plt.pause(.001)
    ax.set_title('matplotlib.pyplot.draw()\
    function Example', fontweight ="bold")

Output

Explanation: A 3D wireframe plot is created and rotated using ax.view_init(30, angle). plt.draw() updates the view each frame and plt.pause(0.001) enables smooth animation with dynamic title updates.



Next Article
Article Tags :
Practice Tags :

Similar Reads