slicing and plotting 3D array with Python - python

i am rather new in python and before was a MATLAB user. I am sorry if my question is to obvious.
I have a huge 47MB file that contains 3D array (351:467:300). That is 300 images from a camera. I want to re-plot them as a kind of animation. Basically, just slice and plot all 300 images. Here is my code
import numpy as np
import time
import matplotlib.pyplot as plt
import scipy.io as c
datafile = c.loadmat('data.mat') # loading data
img = datafile['img'] # extracting the (351:467:300) array
imgShape = np.shape(img)
for i in range(0,imgShape(2)):
plt.imshow(img[:,:,i])
time.sleep(0.3)
plt.draw()
print('Done!')
the problem is: when it comes to imshow the figure window is black and NOT RESPONDING until it finishes the loop, so i can't see anything. How to solve this? How to force it to update "on stream" ?
And the plotting is veeeryyy slow compared to matlab (not because of time.sleep :) i tried without :) ) loop runs really slowly. I am using Spyder, is that a reason ?
Thanks a lot in advance !

Use the following corrections:
you need to make it interactive.
import numpy as np
#import time
#import matplotlib.pyplot as plt
import scipy.io as c
import pylab as pl #added
datafile = c.loadmat('data.mat') # loading data
img = datafile['img'] # extracting the (351:467:300) array
imgShape = np.shape(img)
pl.ion() #added
for i in range(imgShape(2)): #no need for 0
pl.cla() #added
pl.imshow(img[:,:,i])
# time.sleep(0.3)
pl.draw()
pl.pause(0.3) #added
pl.ioff() #added
print('Done!')

Related

How to smooth data

I have a data file that I would like to smooth out. I am not sure on how to do it. I have looked at a few things and nothing really helps. here is my code as is:
import matplotlib.pyplot as plt
import numpy as np
data1 = np.loadtxt('2_Record2308.dat')
plt.title('2_Record2308')
plt.plot(data1)
plt.show()
I know you can do it with the "spicy" thing but I would prefer not to use it.

display images inside a loop by overwriting the existing plot/figure in python

I have hundreds of thousands of images which I have to get from URL, see them ,tag them and then save them in their respective category as spam or non spam. On top of that, I'll be working with google which makes it impossible. My idea is that instead of looking at each image by opening, analysing, renaming and then saving them in directory, I just get the image from url, see within a loop, input a single word and based on that input, my function will save them in their respective directories.
I tried doing
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Image
fg = plt.figure()
for i in range(5):
plt.imshow(np.random.rand(50,50))
plt.show()
x = input()
print(x)
but instead of overwriting the existing frame, it is plotting a different figure. I have even used 1,1 subplot inside a loop but it is not working. Ipython's method does not even display inside a loop either. Could somebody please help me with this problem.
You can make use of matplotlib's interactive mode by invoking plt.ion(). An example:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
fig, ax = plt.subplots()
plt.ion()
plt.show()
for i in range(5):
ax.imshow(np.random.rand(50,50)) # plot the figure
plt.gcf().canvas.draw()
yorn = input("Press 1 to continue, 0 to break")
if yorn==0:
break
Expected output:

How to show single colorbar in loop by Python?

I've tried to plot images of my loaded 2D data (512x512 pixels) in a loop by matplotlib with Python3. But it turns out to show weird images with multiple colorbars. Here is my code:
import numpy as np
import sys
import os
from load_images import load, File
import matplotlib.pyplot as plt
from matplotlib import cm
arr_spe = [x for x in os.listdir() if x.endswith(".SPE")]
for x in arr_spe:
try:
dat = load(x)
plt.imshow(dat,cmap=cm.jet, vmax = 2000)
plt.colorbar()
plt.savefig(x[:-4]+'.png', dpi=500, bbox_inches='tight')
except ValueError as error:
print('ValueError(empty)-'+x)
I use the code to load my data in the following link: Reading SPE file from CCD camera by naming the code as load_images.py.
And I got many images like
Does anybody have ideas to solve this issue? Just simply show single colorbar in an image

Python 2.7 GUI with Matplotlib Issue

I am building a GUI that is a heads-up display and in the background an animation is played.
The tool is supposed to read data from a text file that contains the information that will be displayed. Also, the animation is a library of images. Thus, each second the code looks at a line in the table and displays the information for that row as well as the image associated with that row.
I have implemented a possible solution to this particular need using Matplotlib. My issue is that once the code is ran, if I click anywhere on the screen, or try to open a new window while the loop is running, I get a "(Not Responding") status on the program toolbar
Figure 1 - Not Responding
How can I prevent this issue from happening?
Or are there better ways to implement this functionality? I has to be able to read a txt/csv file as well as render the images one after the other.
Here is a sample of the code:
import matplotlib.pyplot as plt
import numpy as np
import time
from scipy.misc import imread
import matplotlib.cbook as cbook
import pandas as pd
from pylab import *
#Open file with information for HUD
filename = "data.txt"
rndz_data = pd.read_table(filename, sep="\s+")
frames = np.arange(5)
plt.ion()
fig = plt.figure()
#Create array of image files
datafile = [cbook.get_sample_data('rndz0000.png'),
cbook.get_sample_data('rndz0001.png'),
cbook.get_sample_data('rndz0002.png'),
cbook.get_sample_data('rndz0003.png'),
cbook.get_sample_data('rndz0004.png')]
#Create plot and animate
for i in frames:
img = imread(datafile[i])
plt.clf()
plt.imshow(img, zorder=0, extent=[0.5, 8.0, 1.0, 7.0])
plt.plot
plt.draw()
time.sleep(1)

matplotlib plot array size limit?

I've created a program that retrieves data from a device on the serial port every half second or so. It then appends that data to the array that sets the data points and then updates the plot. Everything goes fine until it's been running for an hour or so, at which point the program stops responding.
Does anyone know if there is a size limit for this array? If anyone has any ideas on handling a data set that could be millions of points, I would love to hear your thoughts.
Using the code below I was able to get matplotlib to show a simple graph of ten million points. I suspect the problem isn't with the array size.
import numpy as np
import matplotlib.pyplot as plt
import random
nsteps = 10000000
draws = np.random.randint(0,2,size=nsteps)
steps = np.where(draws>0,1,-1)
walk = steps.cumsum()
plt.plot(np.arange(nsteps), np.array(walk), 'r-')
plt.title("Big Set Random Walk with $\pm1$ steps")
plt.show()
There seems to be a some limit. I just tried
import pylab
import numpy as np
n = 10000000 # my code works fine for n = 1000000
x = np.random.normal(0,1,n)
pylab.plot(x)
pylab.show()
And got the following error:
OverflowError: Agg rendering complexity exceeded. Consider downsampling or decimating your data.

Categories