Updating matplotlib image - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Updating matplotlib image (/thread-7010.html) |
Updating matplotlib image - mcgillca - Dec-17-2017 Hi - I'm just starting to programme in Python, so this may be a very simple error - I'm trying to write code which will monitor astroimages and as they are taken, calculate the running average of the image to improve the signal to noise. To do that, I'm going to need to update my image every few minutes, but am struggling to work out how to update the image using PyPlot. Code below. If I take out the block=False on the second call, then the updated figure is shown - but I need to do this with block=False so that I can automatically add new images. Thank you for any advice, Colin #!/usr/bin/env python2 # Import basic files for reading and manipulating fits data import numpy as np import math import matplotlib import matplotlib.pyplot as plt from astropy.io import fits import time hdul = fits.open('M31s.fit') image_data = hdul[0].data image_real = image_data * 1.0/(2**16) image_min = np.amin(image_real) image_max = np.amax(image_real) image_real -= image_min image_real /= (image_max-image_min) image_med = np.median(image_real) beta = math.sinh(0.25*10)/image_med image_real *= beta image_real = np.arcsinh(image_real) print image_min, image_max, image_med, beta image_real_min = np.amin(image_real) image_real_max = np.amax(image_real) image_real_med = np.median(image_real) print image_real_min, image_real_max, image_real_med fig = plt.imshow(image_real, cmap='gray') plt.colorbar() plt.draw() plt.show(block=False) time.sleep(5) image_real /= math.asinh(beta) image_real_min = np.amin(image_real) image_real_max = np.amax(image_real) image_real_med = np.median(image_real) print image_real_min, image_real_max, image_real_med image_neg = 1-image_real fig.set_data(image_neg) plt.show(block=False) time.sleep(10) RE: Updating matplotlib image - SRG - Dec-17-2017 Here you go. Just watched a video showing exactly this. https://www.youtube.com/watch?v=ZmYPzESC5YY RE: Updating matplotlib image - mcgillca - Dec-21-2017 Dear SRG, Thanks for that - it wasn't quite what I needed, but it did give me somewhere to look. It turns out that the plot needs to be active for set_data to work - so that's why the image did not update when I set block = False. The solution I came up with was to use threads - I left the plot active in the main thread (there are reports of problems when mathplotlib is used in a secondary thread) and updated the plot in another thread - this works well, and allows me to use the zoom, pan etc functions in the mathplotlib image window. Thank you for your help, Colin |