简体   繁体   中英

How can I plot random pixels in real time in Python?

I'm almost a beginner in Python even though I did Computer Sciences.

I'd like to do several projects with real-time Python plotting.

My start is to display an image with random pixels constantly changing. I tried Python interactive mode, and loops, but I can't figure it out.

Here is the code to show random pixels:

import matplotlib
from numpy import random

Z = random.random((50,50))   # Test data
imshow(Z, cmap=get_cmap("Spectral"), interpolation='nearest')
fig.show()

What would be an easy way to replace all pixels with new random ones each frame? (Like a TV noise) Thanks for help !

If you want to show a plot between making changes you can use plt.pause(time_in_s):

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from numpy import random

for i in range(20):
    Z = random.random((50,50))   # Test data
    plt.imshow(Z, cm.get_cmap("Spectral"), interpolation='nearest')
    plt.show()
    plt.pause(0.01)

For more complex animations, see https://matplotlib.org/api/animation_api.html#module-matplotlib.animation or https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

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