3D animation with matplotlib to visualize moving stick figure - python

I'm trying to make moving stick figure 3D animation with matplotlib, using npy file made from VideoPose3D.
And I find this topic and use the same code, but it made this log.
"Module 'matplotlib.cm' has no 'jet' member"
I tried to resolve this problem and made the following code. But the next error occurred.
So please tell me why this happens and fix this code and show animation?
details : matplotlib==3.3.3, python3==3.6.9, numpy==1.19.5
npy file is here.
https://drive.google.com/file/d/1PJME78STVCPgKZdxpC3KOeP39SRJAx00/view?usp=sharing[][1]
import numpy as np
import pandas as pd
import matplotlib
import os
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
from matplotlib import cm
arrayMarker3Dcoordinates = np.load('/home/shime/posesimulate/pose.npy')
arrayMarker3Dcoordinates.shape
size = arrayMarker3Dcoordinates.size
df = pd.DataFrame(arrayMarker3Dcoordinates.reshape(size//51, 17*3),
columns=['pelvisX', 'pelvisY', 'pelvisZ', 'R-troX', 'R-troY', 'R-troZ', 'R-kneeX', 'R-kneeY', 'R-kneeZ', 'R-ankleX', 'R-ankleY', 'R-ankleZ', 'L-troX', 'L-troY', 'L-troZ', 'L-kneeX', 'L-kneeY', 'L-kneeZ', 'L-ankleX', 'L-ankleY', 'L-ankleZ', 'centerX', 'centerY', 'centerZ', 'neckX', 'neckY', 'neckZ', 'noseX', 'noseY', 'noseZ', 'headX', 'headY', 'headZ', 'L-shoX', 'L-shoY', 'L-shoZ', 'L-elbX', 'L-elbY', 'L-elbZ', 'L-wristX', 'L-wristY', 'L-wristZ', 'R-shoX', 'R-shoY', 'R-shoZ', 'R-elbX', 'R-elbY', 'R-elbZ', 'R-wristX', 'R-wristY', 'R-wristZ'])
t_start = 0 # start frame
t_end = df.shape[0] #frame数
N_tag = df.shape[1] # nr of tags used (all)
N_trajectories = N_tag
# pseudo time-vector for first walking activity
t = np.linspace(0, t_end/30, df.shape[0])
# empty animation array (3D) 3*3のarray
x_t = np.zeros(shape=(int(N_tag), df.shape[0], 3))
for tag in range(17):
# store data in numpy 3D array: (tag,time-stamp,xyz-coordinates)
x_t[tag,:,:] = df.iloc[:, tag * 3:tag * 3 + 3]
# ===STICK-LINES========================================================================================
# xx = [x_t[1,:,0],x_t[2,:,0]]
# yy = [x_t[1,:,1],x_t[2,:,1]]
# zz = [x_t[1,:,2],x_t[2,:,2]]
# ======================================================================================================
# Set up figure & 3D axis for animation
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
ax.axis('on')
"""#this is original code
# choose a different color for each trajectory
colors = plt.cm.jet(np.linspace(0, 1, int(N_trajectories)))
#colors
# set up trajectory lines
lines = sum([ax.plot([], [], [], '-', c=c) for c in colors], [])
# set up points
pts = sum([ax.plot([], [], [], 'o', c=c) for c in colors], [])
"""
lines = sum([ax.plot([], [], [], '-', color='C3')for i in range(N_trajectories)], [])
pts = sum([ax.plot([], [], [], 'o', color='C3') for i in range(N_trajectories)], [])
# set up lines which create the stick figures
stick_defines = [
(0, 1),
(0, 4),
(4, 5),
(5, 6),
(0, 7),
(1, 2),
(2, 3),
(7, 8),
(8, 9),
(9, 10),
(8, 14),
(8, 11),
(11, 12),
(12, 13),
(14, 15),
(15, 16)
]
# 上で定義されたstick_definesにある点の間に線をひく "draw line between 2 points"
stick_lines = [ax.plot([], [], [], 'k-')[0] for _ in stick_defines]
# Automatically set axes limits
# prepare the axes limits
# ax.set_xlim(df_minmax.loc['x'].values)
# ax.set_ylim(df_minmax.loc['y'].values)
# ax.set_zlim(df_minmax.loc['z'].values)
# Setting the axes properties
# https://matplotlib.org/2.1.2/gallery/animation/simple_3danim.html
mid_x = 0
x_range = 1
ax.set_xlim3d(mid_x - x_range, mid_x + x_range)
ax.set_xlabel('X')
mid_y =0
y_range = 1
ax.set_ylim3d(mid_y - x_range, mid_y + x_range)
ax.set_ylabel('Y')
mid_z = 0.5
z_range = 1
ax.set_zlim3d(mid_z - z_range, 1.5)
ax.set_zlabel('Z')
ax.set_title('markers in 3D')
# set point-of-view: specified by (altitude degrees, azimuth degrees)
ax.view_init(20,45)
# initialization function: plot the background of each frame
def init():
for line, pt in zip(lines, pts): # lines, ptsのリストから,同時にi番目のデータを取得する.linesからline, pts からpt "at the same time,get number i date, line from lines, pt from pts"
# trajectory lines
line.set_data([], [])
line.set_3d_properties([], [])
# points
pt.set_data([], [])
pt.set_3d_properties([], [])
return lines + pts + stick_lines
# animation function. This will be called sequentially with the frame number
def animate(i):
# 1 * i means show every frame, 2*i means one frame show one frame no-show
i = (1 * i) % x_t.shape[1]
for pt, xi in zip(pts, x_t):
x, y, z = xi[:i].T # note ordering of points to line up with true exogenous registration (x,z,y)
pt.set_data(x[-1:], y[-1:])
pt.set_3d_properties(z[-1:])
for stick_line, (sp, ep) in zip(stick_lines, stick_defines):
stick_line._verts3d = x_t[[sp, ep], i,:].T.tolist()
# ax.view_init(30, 0.3 * i)
ax.view_init(20,60+i*2)
fig.canvas.draw()
return lines + pts + stick_lines
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=t_end, interval=100, repeat = False, blit=True)
# save GIF animation
# anim.save('3Dplotanimation.gif', writer='pillow', fps=10)
plt.show()
And log is this.
/usr/bin/python3 /home/shime/posesimulate/simulate.py
Traceback (most recent call last):
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 258, in size_allocate
FigureCanvasBase.resize_event(self)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 1767, in resize_event
self.callbacks.process(s, event)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 229, in process
self.exception_handler(exc)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 81, in _exception_printer
raise exc
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 224, in process
func(*args, **kwargs)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 1259, in _on_resize
self._init_draw()
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 1706, in _init_draw
self._drawn_artists = self._init_func()
File "/home/shime/posesimulate/simulate.py", line 109, in init
line.set_3d_properties([], [])
File "/home/shime/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 143, in set_3d_properties
zs = np.broadcast_to(zs, xs.shape)
AttributeError: 'list' object has no attribute 'shape'
Traceback (most recent call last):
File "/home/shime/posesimulate/simulate.py", line 139, in <module>
plt.show()
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 353, in show
return _backend_mod.show(*args, **kwargs)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 3524, in show
manager.show() # Emits a warning for non-interactive backend.
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3.py", line 446, in show
self.canvas.draw()
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backends/backend_gtk3agg.py", line 70, in draw
backend_agg.FigureCanvasAgg.draw(self)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 407, in draw
self.figure.draw(self.renderer)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 41, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1870, in draw
self.canvas.draw_event(renderer)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 1759, in draw_event
self.callbacks.process(s, event)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 229, in process
self.exception_handler(exc)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 81, in _exception_printer
raise exc
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 224, in process
func(*args, **kwargs)
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 959, in _start
self._init_draw()
File "/home/shime/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 1706, in _init_draw
self._drawn_artists = self._init_func()
File "/home/shime/posesimulate/simulate.py", line 109, in init
line.set_3d_properties([], [])
File "/home/shime/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 143, in set_3d_properties
zs = np.broadcast_to(zs, xs.shape)
AttributeError: 'list' object has no attribute 'shape'
My writing is probably difficult to read. I'm sorry. As ever, any help is much appreciated.

Inside the init function, simply replace line.set_3d_properties([], []) with line.set_3d_properties([])

Related

edgecolors='None' causes error on animated 3d scatterplot

I'm trying to make a 3d scatterplot with fading points over time, similar to this question. However, when I apply the colormap to the data, then the face of the marker changes color but the edge remains solid.
I've tried using the edgecolors='None', edgecolor='none' kwargs, the marker='o' kwarg as per this example, and the edgecolor='face' kwarg. I've also tried using post creation methods such as scat3D.set_edgecolor("None") etc. The first attempts throw an error, and the latter have no effect.
Error from setting kwargs:
Traceback (most recent call last):
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 230, in saving
yield self
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 1156, in save
writer.grab_frame(**savefig_kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/animation.py", line 384, in grab_frame
dpi=self.dpi, **savefig_kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 2180, in savefig
self.canvas.print_figure(fname, **kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py", line 88, in print_figure
super().print_figure(*args, **kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2082, in print_figure
**kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 442, in print_raw
FigureCanvasAgg.draw(self)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/figure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/home/max/.local/lib/python3.6/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/max/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 292, in draw
reverse=True)):
File "/home/max/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 291, in <lambda>
key=lambda col: col.do_3d_projection(renderer),
File "/home/max/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 545, in do_3d_projection
ecs = (_zalpha(self._edgecolor3d, vzs) if self._depthshade else
File "/home/max/.local/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 847, in _zalpha
rgba = np.broadcast_to(mcolors.to_rgba_array(colors), (len(zs), 4))
File "<__array_function__ internals>", line 6, in broadcast_to
File "/home/max/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 182, in broadcast_to
return _broadcast_to(array, shape, subok=subok, readonly=True)
File "/home/max/.local/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 127, in _broadcast_to
op_flags=['readonly'], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (0,4) and requested shape (100,4)
(There's a during the handling the above exception another occured error after that, but my post was flagged as spam so I didn't include it)
Here's the code without the error (adding a kwarg listed above causes the error):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.patches as mpatches
total = 10
num_whatever = 100
to_plot = [np.random.rand(num_whatever, 3) for i in range(total)]
colors = np.linspace(0, 1, num_whatever)
fig = plt.figure()
ax3d = Axes3D(fig)
scat3D = ax3d.scatter([],[],[], s=10, cmap="Blues", vmin=0, vmax=1)
scat3D.set_cmap("Blues") # cmap argument above is ignored, so set it manually
ttl = ax3d.text2D(0.05, 0.95, "", transform=ax3d.transAxes)
def update_plot(i):
print( i, to_plot[i].shape)
ttl.set_text('PCA on 3 components at step = {}'.format(i*20))
scat3D._offsets3d = np.transpose(to_plot[i])
scat3D.set_array(colors)
return scat3D,
def init():
scat3D.set_offsets([[],[],[]])
plt.style.use('ggplot')
ani = animation.FuncAnimation(fig, update_plot, init_func=init,
blit=False, interval=300, frames=range(total))
ani.save("ani.gif", writer="imagemagick")
plt.show()
Here's the plot with the marker edges not hidden:
Plot
You can see that the faded points still have the marker edges not colormapped. I would expect that setting edgecolor would be unnecessary in the first place, since the 2d animation clearly doesn't need it. But why would adding that kwarg cause me to get a operands could not be broadcast togeather error? I don't have any array with shape (0, 4), so it's not clear where this is coming from.
The problem is that if c (as the color argument) is not specified, it is not a priori clear if a colormapping should happen or not. This leads to some internal confusion about which arguments to obey to. But we can specify c as an empty list to get around that.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
total = 10
num_whatever = 100
to_plot = [np.random.rand(num_whatever, 3) for i in range(total)]
colors = np.linspace(0, 1, num_whatever)
fig = plt.figure()
ax3d = Axes3D(fig)
scat3D = ax3d.scatter([], [], [], s=10, c=[], cmap="Blues", vmin=0, vmax=1,
depthshade=False)
ttl = ax3d.text2D(0.05, 0.95, "", transform=ax3d.transAxes)
def update_plot(i):
print( i, to_plot[i].shape)
ttl.set_text('PCA on 3 components at step = {}'.format(i*20))
scat3D._offsets3d = np.transpose(to_plot[i])
scat3D.set_array(colors)
return scat3D,
def init():
scat3D.set_offsets([[],[],[]])
plt.style.use('ggplot')
ani = animation.FuncAnimation(fig, update_plot, init_func=init,
blit=False, interval=300, frames=range(total))
ani.save("ani.gif", writer="imagemagick")
plt.show()

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()

Animating 3D scatter plot using Python mplotlib via serial data

I am trying to animate a 3D scatter plot using mplotlib in Python. I am able to graph the data and redraw every time, but this results in a frame rate of less than 1 FPS, and I need to scale to upwards of 30 FPS. When I run my code:
import serial
import numpy
import matplotlib.pyplot as plt #import matplotlib library
from mpl_toolkits.mplot3d import Axes3D
from drawnow import *
import matplotlib.animation
import time
ser = serial.Serial('COM7',9600,timeout=5)
ser.flushInput()
time.sleep(5)
ser.write(bytes(b's1000'))
x=list()
y=list()
z=list()
#plt.ion()
fig = plt.figure(figsize=(16,12))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(x,y,z, c='r',marker='o')
ax.set_xlim3d(-255, 255)
ax.set_ylim3d(-255, 255)
ax.set_zlim3d(-255, 255)
def generate():
while True:
try:
ser_bytes = ser.readline()
data = str(ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
xyz = data.split(", ")
dx = float(xyz[0])
dy = float(xyz[1])
dz = float(xyz[2].replace(";",""))
x.append(dx);
y.append(dy);
z.append(dz);
graph._offset3d(x,y,z, c='r',marker='o')
except:
print("Keyboard Interrupt")
ser.close()
break
return graph,
ani = matplotlib.animation.FuncAnimation(fig, generate(), interval=1, blit=True)
plt.show()
I get the following error:
Traceback (most recent call last):
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process
proxy(*args, **kwargs)
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__
return mtd(*args, **kwargs)
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1026, in _start
self._init_draw()
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1750, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1772, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
TypeError: 'tuple' object is not callable
Traceback (most recent call last):
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process
proxy(*args, **kwargs)
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__
return mtd(*args, **kwargs)
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1308, in _handle_resize
self._init_draw()
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1750, in _init_draw
self._draw_frame(next(self.new_frame_seq()))
File "C:\Users\bunti\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\animation.py", line 1772, in _draw_frame
self._drawn_artists = self._func(framedata, *self._args)
TypeError: 'tuple' object is not callable
I am receiving x, y, z coordinates from a lidar module connected to an Arduino, which sends the coordinates over serial to the Python script.
A possible problem with your code is that the animation function (generate) is not supposed to run in an infinite loop. Furthermore, you are supposed to pass a reference to that function to FuncAnimate, but instead you are calling the function (i.e. you need to omit the parentheses in FuncAnimation(..., generate, ...)
The second problem is that you are treating graph._offset3d as if it was a function, when it is merely a tuple of lists. You should assign a new tuple to it, instead of trying to call the function (which I believe is what the error message alludes to).
I simplified your code and the following works fine:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
def update_lines(num):
dx, dy, dz = np.random.random((3,)) * 255 * 2 - 255 # replace this line with code to get data from serial line
text.set_text("{:d}: [{:.0f},{:.0f},{:.0f}]".format(num, dx, dy, dz)) # for debugging
x.append(dx)
y.append(dy)
z.append(dz)
graph._offsets3d = (x, y, z)
return graph,
x = [0]
y = [0]
z = [0]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(x, y, z, color='orange')
text = fig.text(0, 1, "TEXT", va='top') # for debugging
ax.set_xlim3d(-255, 255)
ax.set_ylim3d(-255, 255)
ax.set_zlim3d(-255, 255)
# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=200, interval=50, blit=False)
plt.show()

Cannot cast array data... matplotlib and python

I am trying to make a 3d line chart with matplotlib in python. The data set is taken from a measuring station with different sensors. Now i have the code pretty much as i want it, but every time i try to run it it shows this problem:
TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
My code so far:
import csv
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
depth = {
1 : 11, 2 : 35, 3 : -1, 4 : 11, #position of graphs in cm
}
class SetXYZ:
def __init__(self, datalist, NameNum): #datalist: list with data, NameNum: number of variable
self.NameNum = NameNum
self.datalist = datalist
self.Name = datalist[0][NameNum]
self.length = datalist
def X(self): #Creates list with X Variables
Xlist = []
for element in self.datalist:
Xlist.append(element[0])
Xlist.pop(0)
return Xlist
def Y(self): #Creates list with Y Variables
Ylist = []
for element in self.datalist:
Ylist.append(element[self.NameNum])
Ylist.pop(0)
return Ylist
def Z(self): #list with Z variables
Zlist = []
for element in datalist: #Z is the same for every point on one graph
Zlist.append(depth[self.NameNum-1])
Zlist.pop(0)
return Zlist
def csv_to_list(filename): #returns the file as list
with open(filename, 'rb') as data:
data = csv.reader(data, delimiter=';')
datalist = list(data)
return datalist
filename = " " #Filename
datalist = csv_to_list(filename)
Graph1 = SetXYZ(datalist, 1) #creates the graphs
Graph2 = SetXYZ(datalist, 2)
#plots a graph, more or less to test
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(Graph1.X(), Graph1.Y(), Graph1.Z())
plt.show()
a file looks like this:
Time; sensor 1; sensor 2; sensor 3; sensor4
41940.6791666667;16;19.96;4.1;11.52
41940.67986;15.9;20.51;4.07;11.4
41940.67986;15.9;20.53;4.07;11.41
The full error looks like this:
Traceback (most recent call last):
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 338, in resizeEvent
self.draw()
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 148, in draw
FigureCanvasAgg.draw(self)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 461, in draw
self.figure.draw(self.renderer)
File "C:\Anaconda\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1079, in draw
func(*args)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 254, in draw
for col in self.collections]
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 413, in do_3d_projection
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line 208, in proj_transform_clip
return proj_transform_vec_clip(vec, M)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line 165, in proj_transform_vec_clip
vecw = np.dot(M, vec)
TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
When it is finished there can be up to 30 sensors and several thousand times.
Is there a workaround to this, or what am i doing wrong?
Thanks for your help!

AssertionError in matplotlib animation

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!)

Categories

Resources