简体   繁体   中英

Changing graphs in real-time using matplotlib

This is the code.

import matplotlib.pyplot as plt
import random

x = []
y = []

for i in range(10):
    x.append(i)
    y.append(random.randint(0,100))

graph = plt.bar(x,y)
plt.show()

Whenever I change any value of y, say y[4] = 7 , then I want that to be reflected in the graph. I want that graph to move .

I tried searching the solution for this but none of them worked for me.

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import random

x = []
y = []

for i in range(10):
    x.append(i)
    y.append(random.randint(0,100))

fig, ax = plt.subplots()
bar, = ax.plot(x,y)

def animate(i):
    x = []
    y = []

    for i in range(10):
        x.append(i)
        y.append(random.randint(0,100))

    bar.set_xdata(x)
    bar.set_ydata(y)

    return bar,

animation  = FuncAnimation(fig, animate, interval = 1000)
plt.show()

I want similar results, but in form of bar graph. Any help is appreciated.

The data displayed in the bar chart is not linked to the data in your list. There is no listener attached to the list that lets pyplot know when the list has been modified.

You will need to change the heights of the bars manually. You can do this by grabbing the children of your graph object, which is a list of bars, and updating the height of the bar.

Please note, the code below works because x and the indices of the bars are the same. If x started at 1 or a was range(0, 100, 10) , the code gets more complicated.

import matplotlib.pyplot as plt
import random

# turn on interactive graphing
plt.ion()

x = []
y = []

for i in range(10):
    x.append(i)
    y.append(random.randint(0,100))

graph = plt.bar(x,y)
plt.show()

y[4] = 7
graph.get_children()[4].set_height(7)

Finally got what I wanted

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

X = []
Y = []

for i in range(20):
    Y.append(random.randint(1, 100))
    X.append(i)

fig, ax = plt.subplots(figsize=(10, 6))
ax.bar(X, Y, color="#ff7f7f")
ax.set_yticks([])
ax.set_xticks(X)

for number in range(len(Y)):
    ax.text(number, Y[number], Y[number],
            horizontalalignment='center', va="baseline", fontsize=13)


def draw_barchart(year):
    ax.clear()

    X.clear()
    Y.clear()

    for i in range(20):
        Y.append(random.randint(1, 100))
        X.append(i)

    ax.bar(X, Y, color="#ff7f7f")
    ax.set_yticks([])
    ax.set_xticks(X)

    ax.set_yticks([])
    ax.set_xticks(X)

    for number in range(len(Y)):
        ax.text(number, Y[number], Y[number], horizontalalignment='center', va="baseline", fontsize=13)

    plt.box(False)


animator = animation.FuncAnimation(fig, draw_barchart, interval=1000)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM