Python Figure not responding after I ask for input? - python

So the following is a snippet of what I'm working on where I'm working on and it's meant to find the peaks from the data. I'm trying to add user choice into the mix and for each peak whether it's good or not. I want to have the figure constantly change throughout the script and the input will save the peak value for later use. But the figure simply doesn't respond and I can't seem to find why. Any help would be much appreciated!
import os
import numpy as np
import datetime
import matplotlib.pyplot as plt
for moving in range(len(indexes)):
parsedFile = 'TEST/parsedWVIAdata_%d-%d_%d' % (DT[1,0],int(DT[1,5]),moving + 1)
plt.figure(1)
plt.plot(HD,H2O)
plt.plot(HD[indexes[moving]],H2O[indexes[moving]],"rx")
plt.axis([HD[indexes[moving]]-xrang,HD[indexes[moving]]+xrang,H2O[indexes[moving]]-yrang,H2O[indexes[moving]]+yrang])
plt.show()
select = False
rawrchoice = raw_input("Is this point a peak (y/n): ")
if rawrchoice=='y': select = True
peaks = 4
plt.savefig(parsedFile+'.jpg',pdi=1000)
plt.clf()
plt.close()
Picture of figure not responding - http://i.imgur.com/I6YAtYF.png

Related

Interactive Ploting

I'm searching for an interactive pliting library. I have some dataset that I would like to plot like a scatter (if posible over an image), And need to show some information on focus (or click if focus it's not posible). Let's say for sake of the question, some number.
I don't really know if there a tool that would let me do this, without an excesive amount of code (probably I can do it myself using a canvas in tkinter).
I only have some experience in matplotlib for ploting. That's my principal limitation.
What I'm doing now is to annotate every marker with something like this:
import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4,5]
y = [2,3,1,1,5]
an = [10,42,3,33,4]
plt.figure()
plt.plot(x,y,'o')
for i,a in enumerate(an):
plt.annotate(a, (x[i],y[i]))
Thanks in advance, and sorry if something isn't clear

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 can plots plotted in a foor-loop be successively inspected (no subplots!)?

I have data for several subjects and would like to make a plot for each of them. The goal is to loop through the individual subjects, extract the data for the current subject and then plot it. Once the data of the first subject is plotted, the program should stop and wait for user input, so that the plot for that subject can be inspected in peace. How is this possible?
Note: Im not interested in making subplots.
Im working with PyCharm and the TkAgg Backend.
import matplotlib.pyplot as plt
import numpy as np
data_per_subject = [np.array([1,2]),
np.array([3,4]),
np.array([5,6])]
for data in data_per_subject:
# open figure and plot data of current subject
fig, ax = plt.subplots()
plt.plot(data)
# Here some magic has to take place, so that the program waits for my
# input. The importent thing is, that I have to be able to inspect
# the plot for the current subject while the programm is waiting for my
# input!
# close figure
plt.close()

Python SAC plot w/ grid

I'm required to use the information from a .sac file and plot it against a grid. I know that using various ObsPy functions one is able to plot the Seismograms using st.plot() but I can't seem to get it against a grid. I've also tried following the example given here "How do I draw a grid onto a plot in Python?" but have trouble when trying to configure my x axis to use UTCDatetime. I'm new to python and programming of this sort so any advice / help would be greatly appreciated.
Various resources used:
"http://docs.obspy.org/tutorial/code_snippets/reading_seismograms.html"
"http://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html#obspy.core.stream.Stream.plot"
The Stream's plot() method actually automatically generates a grid, e.g. if you take the default example and plot it via:
from obspy.core import read
st = read() # without filename an example file is loaded
tr = st[0] # we will use only the first channel
tr.plot()
You may want to play with the number_of_ticks, tick_format and tick_rotationparameters as pointed out in http://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.plot.html.
However if you want more control you can pass a matplotlib figure as input parameter to the plot() method:
from obspy.core import read
import matplotlib.pyplot as plt
fig = plt.figure()
st = read('/path/to/file.sac')
st.plot(fig=fig)
# at this point do whatever you want with your figure, e.g.
fig.gca().set_axis_off()
# finally display your figure
fig.show()
Hope it helps.

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