

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dynamically updating a bar plot in Matplotlib
To update a bar plot dynamically in Matplotlib, we can take the following steps −
- 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, animation, that sets the height of the bar and facecolor of the bars.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import animation as animation, pyplot as plt, cm 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(data, data, facecolor='green', alpha=0.75) def animate(frame): global bars index = np.random.randint(1, 7) bars[frame].set_height(index) bars[frame].set_facecolor(colors[np.random.randint(0, len(colors))]) ani = animation.FuncAnimation(fig, animate, frames=len(data)) plt.show()
Output
- Related Questions & Answers
- Plot a bar using matplotlib using a dictionary
- How to make a broken horizontal bar plot in Matplotlib?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- How to plot a bar graph in Matplotlib from a Pandas series?
- How to plot a bar chart for a list in Python matplotlib?
- How to sort bars in a bar plot in ascending order (Matplotlib)?
- How to plot an emoji as a label for a bar in Matplotlib?
- How to add a legend on Seaborn facetgrid bar plot using Matplotlib?
- How to write text above the bars on a bar plot (Python Matplotlib)?
- How to show a bar and line graph on the same plot in Matplotlib?
- Plot multiple columns of Pandas dataframe on the bar chart in Matplotlib
- How to turn off error bars in Seaborn Bar Plot using Matplotlib?
- How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?
- Plot two horizontal bar charts sharing the same Y-axis in Python Matplotlib
- How to a plot stem plot in Matplotlib Python?
Advertisements