AssertionError in matplotlib animation - python

My problem is following:
I'm taking a data from files and want to make an animation of four plots at the same time: two colourbars and two lines.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import sys
begin = float(sys.argv[1])
end = float(sys.argv[2])
dataCl = np.loadtxt("file1.txt")
dataSS = np.loadtxt("file2.txt")
datajSR = np.loadtxt("file3.txt")
ibegin = 0
iend = 0
for i in range(len(dataCl[:,0])):
if np.abs(dataCl[i,0] - begin) < 1e-9:
ibegin = i
iend = i
while abs(dataCl[i,0] - end) >= 1e-9:
iend = iend + 1
i = i + 1
break
fig = plt.figure()
f, axarr = plt.subplots(2, 2)
temp = np.zeros((10,10))
Qs = axarr[0,0].imshow(temp,cmap = plt.cm.OrRd)
El = axarr[0,1].imshow(temp,cmap = plt.cm.OrRd)
SS, = axarr[1,0].plot([],[])
jSR, = axarr[1,1].plot([],[])
def init():
Qs.set_array(temp)
El.set_array(temp)
SS.set_data([],[])
jSR.set_data([],[])
return Qs,El,SS,jSR,
def animate(i):
a = 0
b = 0
dataQ = np.zeros((10,10))
dataE = np.zeros((10,10))
for j in range(100):
if b >= 10:
a = a + 1
b = 0
dataQ[a][b] = dataCl[i,2*j + 1]
dataE[a][b] = dataCl[i,2*(j+1)]
b = b + 1
Qs.set_array(dataQ)
El.set_array(dataE)
SS.set_data(dataSS[ibegin:ibegin+i,0],dataSS[ibegin:ibegin+i,1])
jSR.set_data(datajSR[ibegin:ibegin+i,0],datajSR[ibegin:ibegin+i,1])
return Qs,El,SS,jSR,
ani = anim.FuncAnimation(fig, animate, init_func = init, frames = iend-ibegin,interval=25, blit=True)
plt.show()
After running it shows these messages:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 236, in resize
self.show()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 239, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 421, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 904, in draw
self.canvas.draw_event(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1544, in draw_event
self.callbacks.process(s, event)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 262, in process
proxy(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 192, in __call__
return mtd(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 273, in _end_redraw
self._post_draw(None, self._blit)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 220, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 235, in _blit_draw
a.axes.draw_artist(a)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2008, in draw_artist
assert self._cachedRenderer is not None
AssertionError
I cannot find a mistake in my code ;(

The error message might be backend and platform specific. As the error message seems to point to the blitting mechanism, you might want to try setting blit=False in FuncAnimation. Also, you might try some other backend to see if the problem persists. (Knowing your platform and matplotlib version might also help.)
Update: If setting blit=False, trying another backend, and updating matplotlib does not help, then a few suggestions:
Try to see manually if you code works with the initial data (init(); animate(0); fig.savefig("/tmp/test.png")) - if it throws an error, there is a static plotting problem to fix.
Now you initialize the plot twice (first in the code, then in init), you can take one away (e.g. do not define init_func)
Initializing the plots with [],[] leaves the scale uninitialized. You should probably use set_ylim, set_xlim with the plots and vmin, vmax keywords with the imshow images when you initialize them. (This could possibly have something to do with the exception you get!)

Related

Buffer is too small for requested array-Astropy

I'm reading TESS data and as you might expect it can be really large, My Buffer is too small. Is there a way I can avoid this error? Like maybe skip the file that has a large file? Or is there a more permanent solution(not involving more memory)? My Code is below, along with the Full Error
from lightkurve import TessTargetPixelFile
import lightkurve as lk
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def func(tpf):
tpf.plot(aperture_mask=tpf.pipeline_mask);
lc = tpf.to_lightcurve()
mask = (lc.time.value < 1464)
masked_lc = lc[mask]
clipped_lc = masked_lc.remove_outliers(sigma=5);
flat_lc = clipped_lc.flatten()
binned_lc = flat_lc.bin(binsize=5)
periodogram = binned_lc.to_periodogram(method="bls", period=np.arange(1, 20, 0.001))
print(periodogram.plot())
planet_b_period = periodogram.period_at_max_power
planet_b_t0 = periodogram.transit_time_at_max_power
planet_b_dur = periodogram.duration_at_max_power
ax = binned_lc.fold(period=planet_b_period, epoch_time=planet_b_t0).scatter()
ax.set_xlim(-5, 5);
best_fit_period = periodogram.period_at_max_power
print('Best fit period: {:.3f}'.format(best_fit_period))
bfc=best_fit_period
print(bfc)
folded_lc = binned_lc.fold(period=bfc)
folded_lc.scatter(s=7);
plt.show()
planet_b_period
planet_b_model = periodogram.get_transit_model(period=planet_b_period,
transit_time=planet_b_t0,
duration=planet_b_dur)
ax = binned_lc.fold(planet_b_period, planet_b_t0).scatter(s=7)
planet_b_model.fold(planet_b_period, planet_b_t0).plot(ax=ax, c='r', lw=2)
ax.set_xlim(-5, 5);
dataset=pd.read_csv("all_targets_S001_v1.csv")
d=dataset["TICID"]
print(d)
IDs=[]
for i in range(len(d)):
IDs.append("TIC"+str(d[i]))
for i in range(len(IDs)):
tpf_file = lk.search_targetpixelfile(IDs[i], mission="TESS", sector=5).download(quality_bitmask='default')
try:
func(tpf_file)
continue
except:
continue
Thanks
The Full Error
WARNING: File may have been truncated: actual file length (262144) is smaller than the expected size (46402560) [astropy.io.fits.file]
Traceback (most recent call last):
File "lc.py", line 42, in <module>
tpf_file = lk.search_targetpixelfile(IDs[i], mission="TESS", sector=5).download(quality_bitmask='default')
File "C:\ProgramData\Anaconda3\lib\site-packages\lightkurve\utils.py", line 555, in wrapper
return f(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\lightkurve\search.py", line 355, in download
return self._download_one(
File "C:\ProgramData\Anaconda3\lib\site-packages\lightkurve\search.py", line 290, in _download_one
return read(path, quality_bitmask=quality_bitmask, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\lightkurve\io\read.py", line 112, in read
return getattr(__import__("lightkurve"), filetype)(path_or_url, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\lightkurve\targetpixelfile.py", line 2727, in __init__
quality_array=self.hdu[1].data["QUALITY"], bitmask=quality_bitmask
File "C:\ProgramData\Anaconda3\lib\site-packages\astropy\utils\decorators.py", line 758, in __get__
val = self.fget(obj)
File "C:\ProgramData\Anaconda3\lib\site-packages\astropy\io\fits\hdu\table.py", line 399, in data
data = self._get_tbdata()
File "C:\ProgramData\Anaconda3\lib\site-packages\astropy\io\fits\hdu\table.py", line 171, in _get_tbdata
raw_data = self._get_raw_data(self._nrows, columns.dtype,
File "C:\ProgramData\Anaconda3\lib\site-packages\astropy\io\fits\hdu\base.py", line 520, in _get_raw_data
return self._file.readarray(offset=offset, dtype=code, shape=shape)
File "C:\ProgramData\Anaconda3\lib\site-packages\astropy\io\fits\file.py", line 330, in readarray
return np.ndarray(shape=shape, dtype=dtype, offset=offset,
TypeError: buffer is too small for requested array

Interactive plot with category axis with Matplotlib

I have the following code snippet to illustrate the issue:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import time
import random
# Mock categories
categories = ["cat1", "cat2", "cat3", "cat4"]
counter = 0
# Plot
x_data = []
y_data = []
plt.ion()
fig = plt.figure()
subplt = fig.add_subplot(312)
subplt_line, = subplt.plot(x_data, y_data, 'b.')
while True:
time.sleep(0.1) # simulate some delay that occurs in the actual application
x_data.append(counter)
subplt_line.set_xdata(x_data)
counter += 1
# y_data.append(random.randrange(1, 15)) # This works fine (except the scaling)
y_data.append(random.choice(categories)) # This will end in an exception
subplt_line.set_ydata(y_data)
# Update the plot
fig.canvas.draw()
fig.canvas.flush_events()
It will end in an exception like this:
Traceback (most recent call last):
File "test.py", line 36, in <module>
fig.canvas.draw()
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 437, in draw
self.figure.draw(self.renderer)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/figure.py", line 1493, in draw
renderer, self, artists, self.suppressComposite)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2635, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/lines.py", line 738, in draw
self.recache()
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/lines.py", line 656, in recache
yconv = self.convert_yunits(self._yorig)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/artist.py", line 200, in convert_yunits
return ax.yaxis.convert_units(y)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/axis.py", line 1526, in convert_units
ret = self.converter.convert(x, self.units, self)
File "/anaconda3/envs/xxx/lib/python2.7/site-packages/matplotlib/category.py", line 65, in convert
unit.update(values)
AttributeError: 'NoneType' object has no attribute 'update'
I seems like the combination of categorcal y data and the interactivity is causing this. When using numerical values it works fine, and when using the non-interactive feature it works well even with the categorical axis.
Another issue is be the automatic scaling of the y axis. New values added via set_y_data() dot seem to trigger this.
The plot will visualize analysis done on an endless stream of data and is used like a dashboard - therefore the plot should update with each iteration of the loop.
I couldn't run your code using the while loop, but I would suggest using FuncAnimation to create self-updating graphs anyway (there are plenty of examples on SO and online).
I believe your problem is with the initialization of the Line2D object. When you're passing any empty y-array, matplotlib seem to assume you're going to use numerical and not categorical values. Initializing the line with a string as a y-value seem to do the trick. You'll have to adjust the code so that the first point created makes sense for your data, but that should only be a minor annoyance.
For the scaling of the axis, matplotlib adds each category to a new integer value, so you need only to count how many categories you have in your data to know what is the extent of the axes.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
import random
# Mock categories
categories = ["cat1", "cat2", "cat3", "cat4"]
counter = 0
# Plot
x_data = [0]
y_data = ['cat1']
fig = plt.figure()
subplt = fig.add_subplot(312)
subplt_line, = subplt.plot(x_data, y_data, 'b.-')
debug_text = fig.text(0, 1, "TEXT", va='top') # for debugging
def init():
subplt_line.set_data([0],['cat1'])
def animate(num, ax):
new_x, new_y = num, random.choice(categories)
debug_text.set_text('{:d} {:s}'.format(num, new_y))
x, y = subplt_line.get_data()
x = np.append(x, new_x)
y = np.append(y, new_y)
subplt_line.set_data(x,y)
ax.set_xlim(min(x),max(x))
ax.set_ylim(0,len(np.unique(y))-1)
return subplt_line,debug_text
ani = animation.FuncAnimation(fig, animate, fargs=[subplt], init_func=init, frames=20, blit=False, repeat=False)
plt.show()

Issues with passing variables inside a class - python

I am facing issues with the implementation of the method animation from the class wave bellow. The animation method aims to create an animation of a wave propagation. I do not want that the animation method modify the state instance variable u of the Wave object, and because of that I am trying to create a local variable u inside the method animation. Nevertheless, I do not know how to pass the local variable u from the animation method for the animate function that is defined inside the animation method. In the way that I tried to implement, from my conception, the local variable u of the method animation should be a king of global variable for the animate method (that is defined inside the animation method). But, this assumption is cleary wrong, otherwise I would not get an error. As a complementary information the error I am getting is: UnboundLocalError: local variable 'u' referenced before assignment.
I will be glad if someone indicate to me the way for the implementation that I want.
thanks in advance
class Wave(object):
def __init__(self, phenomenon):
'''
nx: Number of spatial grid points
dx: Distance between any pair of adjacent grid points
nt: Number of steps in time
nu: Diffusion coefficient
dt: Value of the step in time
c: wave velocity
u: grid vector of the wave
'''
if phenomenon == 'convection':
self.phenomenon = phenomenon
self.nx = 81
self.dx = 2.0/(self.nx - 1) # Distance between any pair of adjacent grid
self.nt = 100
self.dt = 0.002
self.c = 3
self.x = numpy.linspace(0,4,self.nx)
self.u = numpy.ones(self.nx)
self.lbound = numpy.where(self.x >= 0.5)
self.ubound = numpy.where(self.x <= 1.0)
self.bounds = numpy.intersect1d(self.lbound[0], self.ubound[0])
self.u[self.bounds] = 2
if phenomenon == 'diffusion':
...
if phenomenon == 'burgers':
...
def _convection(self, u):
un = u.copy()
u[1:] = un[1:] - self.c*self.dt/self.dx*(un[1:] - un[:-1])
u[0] = 1.0
return u
def integration(self):
if self.phenomenon == 'convection':
for n in range(1,self.nt):
self.u = self._convection(u=self.u)
if self.phenomenon == 'diffusion':
...
if self.phenomenon == 'burgers':
...
def animation(self):
fig = plt.figure()
ax = plt.axes(xlim=(0,4), ylim=(0,3))
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time = %.2fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
def init():
line.set_data([], [])
time_text.set_text('')
return line, time_text
x = self.x
u = self.u.copy()
def animate(i):
if self.phenomenon == 'convection':
u = self._convection(u=u)
if self.phenomenon == 'diffusion':
...
if self.phenomenon == 'burgers':
...
line.set_data(x,u)
time_text.set_text(time_template % (i*self.dt))
return line, time_text
anim = animation.FuncAnimation(fig, animate, frames=500, init_func=init, interval=10, blit=True)
plt.show()
EDIT
Complete trace error:
Exception in Tkinter callback Traceback (most
recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line
1489, in call
return self.func(*args) File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 536, in callit
func(*args) File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py",
line 141, in _on_timer
TimerBase._on_timer(self) File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1203,
in _on_timer
ret = func(*args, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 876, in
_step
still_going = Animation._step(self, *args) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 735, in
_step
self._draw_next_frame(framedata, self._blit) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 754, in
_draw_next_frame
self._draw_frame(framedata) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 1049, in
_draw_frame
self._drawn_artists = self._func(framedata, *self._args) File "wave.py", line 201, in animate
un = u.copy() UnboundLocalError: local variable 'u' referenced before assignment Exception in Tkinter callback Traceback (most recent
call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489,
in call
return self.func(*args) File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 536, in callit
func(*args) File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py",
line 141, in _on_timer
TimerBase._on_timer(self) File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1203,
in _on_timer
ret = func(*args, **kwargs) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 876, in
_step
still_going = Animation._step(self, *args) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 735, in
_step
self._draw_next_frame(framedata, self._blit) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 754, in
_draw_next_frame
self._draw_frame(framedata) File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 1049, in
_draw_frame
self._drawn_artists = self._func(framedata, *self._args) File "wave.py", line 201, in animate
un = u.copy() UnboundLocalError: local variable 'u' referenced before assignment
When you capture a variable with a closure in Python, you are not allowed to assign to it. If Python sees you assign to it, then it isn't allowed to be captured from the enclosing scope; it must be a new function-local variable (local to your animate function). But since you're trying to use the captured u to initialize it, you need that u's value, but in this context Python has decided that u must be local to animate, so it isn't looking at animate's enclosing scope. That's why you're getting that error.
A simple way to get around that in your case is just to define animate as
def animate(i, u=u):
...
This explicitly passes a copy of u into animate.

Python matplotlib: Error while saving scatter plot in svg format

I am trying to create a scatter plot with the color for each point based on the value in y-dimension and the tooltip for each point based on the value of x-axis. I was in need of creating tooltips for each point on mouseover event, which I was able to achieve. I would like to save this plot to a svg file with events so that the svg file can be viewed in a browser with tooltip.
I get the following error when I try to save it,
Traceback (most recent call last):
File "./altered_tooltip.py", line 160, in <module>
example.plot()
File "./altered_tooltip.py", line 70, in plot
pl.savefig(f, format="svg")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig
return fig.savefig(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1978, in print_svg
return svg.print_svg(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_svg.py", line 1157, in print_svg
return self._print_svg(filename, svgwriter, fh_to_close, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_svg.py", line 1185, in _print_svg
self.figure.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/collections.py", line 718, in draw
return Collection.draw(self, renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/collections.py", line 250, in draw
renderer.open_group(self.__class__.__name__, self.get_gid())
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_svg.py", line 516, in open_group
self.writer.start('g', id=gid)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_svg.py", line 141, in start
v = escape_attrib(v)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_svg.py", line 80, in escape_attrib
s = s.replace(u"&", u"&")
AttributeError: 'list' object has no attribute 'replace'
The code that I am running is
from numpy import *
import pylab as pl
import matplotlib.colors as mcolors
import xml.etree.ElementTree as ET
from StringIO import StringIO
ET.register_namespace("","http://www.w3.org/2000/svg")
class wxToolTipExample(object):
def __init__(self, plot_data):
self.figure = pl.figure()
self.axis = pl.axes()
self.ax = self.figure.add_subplot(111)
self.tooltip.SetTip() calls
self.dataX = plot_data[:,0]
self.dataY = plot_data[:,1]
self.annotes = []
self.pathCol = None #to hold the scatter object
def plot(self):
for idx in arange(self.dataX.size):
# create a tuple of co-ordinates
tup = (self.dataX[idx], self.dataY[idx])
# create annotation with tooltip
annotation = self.axis.annotate("Column %s" % int(self.dataX[idx]),
xy=tup, xycoords='data',
xytext=(+10, +30), textcoords='offset points',
#horizontalalignment="right",
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=0.2"),
#bbox=dict(boxstyle="round", facecolor="w",
# edgecolor="0.0", alpha=0.0)
)
# by default, disable the annotation visibility
annotation.set_visible(False)
# append the annotation object and co-ords tuple to the list
self.annotes.append([tup, annotation])
self.figure.canvas.mpl_connect('motion_notify_event', self._onMotion)
c_map = self.WGrYR()
self.pathCol = self.axis.scatter(self.dataX, self.dataY, c=self.dataY, marker='s', s=40, cmap=c_map)
# Set id for the annotations
for i, t in enumerate(self.axis.texts):
t.set_gid('tooltip_%d'%i)
# Set id for the points on the scatter plot
points = ['point_{0}'.format(ii) for ii in arange(1, self.dataX.size+1)]
self.pathCol.set_gid(points)
f = StringIO()
#pl.show()
pl.savefig(f, format="svg")
"""
# Create XML tree from the SVG file.
tree, xmlid = ET.XMLID(f.getvalue())
tree.set('onload', 'init(evt)')
# Hide the tooltips
for i, t in enumerate(self.axis.texts):
ele = xmlid['tooltip_%d'%i]
ele.set('visibility','hidden')
# assign mouseover and mouseout events
for p in points:
ele = xmlid[p]
ele.set('onmouseover', "ShowTooltip(this)")
ele.set('onmouseout', "HideTooltip(this)")
script = self.getSvgScript()
# Insert the script at the top of the file and save it.
tree.insert(0, ET.XML(script))
ET.ElementTree(tree).write('svg_tooltip.svg')
"""
def getSvgScript(self):
return """
<script type="text/ecmascript">
<![CDATA[
function init(evt) {
if ( window.svgDocument == null ) {
svgDocument = evt.target.ownerDocument;
}
}
function ShowTooltip(obj) {
var cur = obj.id.slice(-1);
var tip = svgDocument.getElementById('tooltip_' + cur);
tip.setAttribute('visibility',"visible")
}
function HideTooltip(obj) {
var cur = obj.id.slice(-1);
var tip = svgDocument.getElementById('tooltip_' + cur);
tip.setAttribute('visibility',"hidden")
}
]]>
</script>
"""
def _onMotion(self, event):
visibility_changed = False
for point, annotation in self.annotes:
if event.xdata != None and event.ydata != None: # mouse is inside the axes
should_be_visible = abs(point[0]-event.xdata) < 0.2 and abs(point[1]-event.ydata) < 0.05
if should_be_visible != annotation.get_visible():
visibility_changed = True
annotation.set_visible(should_be_visible)
if visibility_changed:
pl.draw()
def WGrYR(self):
c = mcolors.ColorConverter().to_rgb
seq = [c('white'), c('grey'), 0.33, c('grey'), c('yellow'), 0.66, c('yellow'), c('red')]
seq = [(None,) * 3, 0.0] + list(seq) + [1.0, (None,) * 3]
cdict = {'red': [], 'green': [], 'blue': []}
for i, item in enumerate(seq):
if isinstance(item, float):
r1, g1, b1 = seq[i - 1]
r2, g2, b2 = seq[i + 1]
cdict['red'].append([item, r1, r2])
cdict['green'].append([item, g1, g2])
cdict['blue'].append([item, b1, b2])
#print cdict
return mcolors.LinearSegmentedColormap('Custom_WGrYR', cdict)
ET.register_namespace("","http://www.w3.org/2000/svg")
# test column heat for nodes
n_cols = 5
plot_data = zeros((n_cols,2))
# generate column numbers and random heat values to test
plot_data[:,0] = asarray(arange(1, n_cols+1))#.reshape(n_cols,1)
plot_data[:,1] = random.rand(n_cols,1).reshape(n_cols,)
example = wxToolTipExample(plot_data)
example.plot()
Where am I going wrong?
Your code looks like a work in progress that hasn't been completely cleaned up. (I had to comment out self.tooltip.SetTip() calls to get it to run.) But here is the cause of your immediate issue with the exception you note, and how I found it:
On my machine, I edited backend_svg.py function start() to add a print(extra) and then I run your code. As a result of your lines:
points = ['point_{0}'.format(ii) for ii in arange(1, self.dataX.size+1)]
self.pathCol.set_gid(points)
the matplotlib backend attempts to create an SVG <g> node with the ID: ['point_1', 'point_2', 'point_3', 'point_4', 'point_5']. This is a list, rather than a valid string, so s.replace() fails.
Ultimately you must change your code so that set_gid() only receives string parameters. The simplest way to do that is to change the two lines above to simply:
self.pathCol.set_gid('point_1')
but then you don't get IDs for individual points in the generated SVG. You could also remove the self.pathCol.set_gid line altogether (pathCol it will be rendered to SVG as <g id="PathCollection_1"> and points will also not have IDs).
It appears that it is not simple to assign SVG IDs to individual points/vertices contained within a pathCollection. If you need to do that, you might need to come up with another way to plot them -- if I understand the problem correctly, you'd need to plot individual points rather than a path.

Gracefully stopping (not pausing) an animated plot in python (matplotlib)

I am running an animated scatter in a process. Everything is working fine, except that an exception is throw when I want to exit everything.
import multiprocessing as mp
import time
from collections import deque
def start_colored_scores(nb_channels):
q = mp.Queue()
process = mp.Process(target=colored_scores,args=(q,nb_channels,4000))
process.start()
return process,q
def colored_scores(q,nb_channels,size):
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, axes = plt.subplots(nrows=nb_channels,ncols=1,sharex=True,sharey=True)
plt.axis([-1.0,1.0,-1.0,1.0])
scats = [axe.scatter([0], [0], c="white", s=size) for axe in axes]
def animate(i):
scores = q.get()
if scores is None : # this is the external signal saying things should stop
plt.close()
return [axe.scatter([0], [0], c="white", s=size) for axe in axes]
scats = []
for score,axe in zip(scores,axes):
score = max(min(1,1-score),0)
scats.append(axe.scatter([0], [0], c=(1-score,0,score), s=size))
return scats
ani = animation.FuncAnimation(fig, animate, interval=1, blit=True)
plt.show()
For example, this is working fine:
_,q = start_colored_scores(2)
x = 0
right = 1
time_start = time.time()
while time.time()-time_start < 5:
if right==1 and x>1.0:
x = 1.0
right = -1
if right==-1 and x<0.0:
x = 0.0
right = 1
x+=right*0.02
q.put([x,1-x])
time.sleep(0.02)
q.put(None) # indicating I do not need plotting anymore
print "this is printed ... exception in the process ?"
The behavior is as I expect : scatters are displayed and animated for 5 seconds, then the program continues. The only issue is that an exception is thrown (I guess in the process) saying :
AttributeError: 'NoneType' object has no attribute 'tk'
Is there a way to do the exact same thing but avoiding the exception ? Or to catch this exception somewhere ?
You can catch that exception pretty easily:
def colored_scores(q,nb_channels,size):
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, axes = plt.subplots(nrows=nb_channels,ncols=1,sharex=True,sharey=True)
plt.axis([-1.0,1.0,-1.0,1.0])
scats = [axe.scatter([0], [0], c="white", s=size) for axe in axes]
def animate(i):
scores = q.get()
if scores is None : # this is the external signal saying things should stop
plt.close()
return [axe.scatter([0], [0], c="white", s=size) for axe in axes]
scats = []
for score,axe in zip(scores,axes):
score = max(min(1,1-score),0)
scats.append(axe.scatter([0], [0], c=(1-score,0,score), s=size))
return scats
ani = animation.FuncAnimation(fig, animate, interval=1, blit=True)
try:
plt.show()
except AttributeError: # This will supress the exception
pass
However, once you get catch that one, you get a new one (at least on my system):
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 536, in callit
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 141, in _on_timer
TimerBase._on_timer(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 876, in _step
still_going = Animation._step(self, *args)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 735, in _step
self._draw_next_frame(framedata, self._blit)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 755, in _draw_next_frame
self._post_draw(framedata, blit)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 778, in _post_draw
self._blit_draw(self._drawn_artists, self._blit_cache)
File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 798, in _blit_draw
ax.figure.canvas.blit(ax.bbox)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 353, in blit
tkagg.blit(self._tkphoto, self.renderer._renderer, bbox=bbox, colormode=2)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/tkagg.py", line 20, in blit
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
TclError: this isn't a Tk application
I can't find any way to supress that one. What you could do, is just terminate the subprocess, rather than try to send it a signal to shutdown:
proc,q = start_colored_scores(2)
x = 0
right = 1
time_start = time.time()
while time.time()-time_start < 5:
if right==1 and x>1.0:
x = 1.0
right = -1
if right==-1 and x<0.0:
x = 0.0
right = 1
x+=right*0.02
q.put([x,1-x])
time.sleep(0.02)
#q.put(None) # indicating I do not need plotting anymore
proc.terminate()
This is not as graceful as sending something through the queue (and doesn't allow for any additional clean-up in the sub-process, assuming you want it), but doesn't throw any exceptions.

Categories

Resources