Dynamically updating a bar plot in Matplotlib

To update a bar plot dynamically in Matplotlib, we can create an animated visualization where bars change height and color over time. This is useful for creating engaging data visualizations or real-time data displays.

Steps to Create Dynamic Bar Plot

  • Set the figure size and adjust the padding between and around the subplots
  • Create a new figure or activate an existing figure
  • Make a list of data points and colors
  • Plot the bars with data and colors, using bar() method
  • Using FuncAnimation() class, make an animation by repeatedly calling a function that updates bar properties
  • To display the figure, use show() method

Example

Here's a complete example that creates an animated bar plot with randomly changing heights and colors ?

import numpy as np
from matplotlib import animation, pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()

data = [1, 4, 3, 2, 6, 7, 3]
colors = ['red', 'yellow', 'blue', 'green', 'black']
bars = plt.bar(range(len(data)), data, facecolor='green', alpha=0.75)

def animate(frame):
    global bars
    index = np.random.randint(1, 8)
    bars[frame].set_height(index)
    bars[frame].set_facecolor(colors[np.random.randint(0, len(colors))])

ani = animation.FuncAnimation(fig, animate, frames=len(data), repeat=True, interval=500)

plt.title('Dynamic Bar Plot Animation')
plt.ylabel('Value')
plt.xlabel('Bar Index')
plt.show()

How It Works

The animation function is called repeatedly for each frame. In each call:

  • frame parameter indicates which bar to update (0 to 6)
  • set_height() changes the bar's height to a random value
  • set_facecolor() changes the bar's color randomly
  • interval=500 sets 500ms delay between frames

Advanced Example with Data Simulation

Here's a more realistic example simulating real-time data updates ?

import numpy as np
from matplotlib import animation, pyplot as plt

# Setup figure
fig, ax = plt.subplots()
categories = ['A', 'B', 'C', 'D', 'E']
values = [10, 15, 12, 8, 20]

bars = ax.bar(categories, values, color='skyblue')
ax.set_ylim(0, 25)
ax.set_title('Real-time Data Simulation')

def update_bars(frame):
    # Simulate changing data
    new_values = np.random.randint(5, 25, len(categories))
    
    for bar, new_val in zip(bars, new_values):
        bar.set_height(new_val)
    
    ax.set_title(f'Real-time Data - Frame {frame}')

ani = animation.FuncAnimation(fig, update_bars, frames=50, repeat=True, interval=200)
plt.show()

Key Parameters

Parameter Description Example Value
frames Number of animation frames 50, len(data)
interval Delay between frames (ms) 200, 500, 1000
repeat Whether to repeat animation True, False

Conclusion

Dynamic bar plots in Matplotlib use FuncAnimation to repeatedly update bar properties like height and color. This technique is perfect for creating engaging visualizations of changing data or real-time monitoring dashboards.

Updated on: 2026-03-25T23:48:31+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements