How to animate a bar char being updated in Python

2024/10/4 15:24:59

I want to create an animated, stacked bar chart.

There is a great tutorial, which shows how to animate line graphs.

However, for animating bar charts, the BarContainer object, does not have any attribute to 'set_data'. I am therefore forced to clear the figure axes each time, e.g. ,

fig=plt.figure()def init():plt.cla()def animate(i):p1 = plt.bar(x_points,y_heights,width,color='b')return p1anim = animation.FuncAnimation(fig,animate,init_func=init,frames=400,interval=10,blit=False)

Is there an alternative option, following the style of the link, such that I do not have to clear the axes each time? Thanks.

Answer

You'll want to call plt.bar outside of animation(), update the height of each bar with Rectangle.set_height as new data comes in.

In practice, loop through each incoming set of y_heights zipped with the list of Rectangles returned by plt.bar() as seen below.

p1 = plt.bar(x_points,y_heights,width,color='b')def animate(i):for rect, y in zip(p1, y_heights):rect.set_height(y)anim = animation.FuncAnimation(fig,animate,init_func=init,frames=400,interval=10,blit=False)

You might want to put p1 in your init() but that's up to you!

All credit for this answer goes to unutbu's answer in the related question Updating a matplotlib bar graph?. I would've added in a comment but I'm obviously quite new.

https://en.xdnf.cn/q/70593.html

Related Q&A

Add text to end of line without loading file

I need to store information into a very big file, in form of many dictionaries. Thats not so important, is just to say that I tried to first get all the data into these dictionaries and I run out of me…

How does one use `dis.dis` to analyze performance?

Im trying to use pythons dis library to experiment with & understand performance. Below is an experiment i tried, with the results.import disdef myfunc1(dictionary):t = tuple(dictionary.items())ret…

How do I require HTTPS for this Django view?

(r^login/?$,django.contrib.auth.views.login,{template_name:login.html, authentication_form:CustomAuthenticationForm}),How do I add HTTPS required to this? I usually have a decorator for it..But in th…

How many times a number appears in a numpy array

I need to find a way to count how many times each number from 0 to 9 appears in a random matrix created using np.random.randint()import numpy as np p = int(input("Length of matrix: ")) m = np…

python: How to remove values from 2 lists based on whats in 1 list

I have 2 lists of numbers, one called xVar and the other called yVar. I will use these 2 elements to plot X and Y values on a graph. They both have the same number of elements. Normally, I would jus…

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single columnIndex A Index B 0 A 0 NAN 1 NAN 1 D 2 B 2 …

Upsample and Interpolate a NumPy Array

I have an array, something like:array = np.arange(0,4,1).reshape(2,2)> [[0 12 3]]I want to both upsample this array as well as interpolate the resulting values. I know that a good way to upsample an…

How to extract text from table in image?

I have data which in a structured table image. The data is like below:I tried to extract the text from this image using this code:import pytesseract from PIL import Imagevalue=Image.open("data/pic…

numpy.savetxt tuple index out of range?

Im trying to write a few lines into a text file, and heres the code I used:import numpy as np# Generate some test data data = np.arange(0.0,1000.0,50.0)with file(test.txt, w) as outfile: outfile.w…

Retrieve list of USB items using Python

How can I retrieve the items plugged into the computer through USB using python? Ive searched around on here and found some old examples which dont appear to work anymore as they are over 5 years old.…