Related
import numpy as np
import pandas as pd
import matplotlib
# matplotlib.use("Agg")
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
t_start = 2 #start frame
t_end = 1711 #end frame
data = pd.read_csv('Sub6_cylinder0009_index_flex_processed.csv')
df = data.loc[t_start:t_end,'FT1X':'CMC5Z']
df_minmax = pd.DataFrame(index=list('xyz'),columns=range(2))
for i in list('xyz'):
c_max = df.max().max()
c_min = df.min().min()
df_minmax.loc[i] = np.array([c_min,c_max])
df_minmax = 3.3*df_minmax
df.columns = np.repeat(range(22),3)
N_tag = int(df.shape[1]/3) # nr of tags used (all)
N_trajectories = N_tag
t = np.linspace(0,data.Time[t_end],df.shape[0])
x_t = np.zeros(shape=(N_tag,df.shape[0],3))
for tag in range(22):
x_t[tag,:,:] = df[tag]
x_t = x_t[:, :, [0, 2, 1]]
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
ax.axis('on')
#choose a different color for each trajectory
colors = plt.cm.jet(np.linspace(0, 1, N_trajectories))
# 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], [])
#set up lines which create the stick figures
stick_defines = [
(0, 13),
(13, 14),
(14, 19),
(1, 5),
(2, 6),
(3, 7),
(4, 8),
(5, 9),
(6, 10),
(7, 11),
(8, 12),
(9, 15),
(10, 16),
(11, 17),
(12, 18),
(18, 21),
(15, 20)
]
stick_lines = [ax.plot([], [], [], 'k-')[0] for _ in stick_defines]
print(stick_lines)
ax.set_xlim3d([np.nanmin(x_t[:, :, 0]), np.nanmax(x_t[:, :, 0])])
ax.set_ylim3d([np.nanmin(x_t[:, :, 1])-400, np.nanmax(x_t[:, :, 1])+400])
ax.set_zlim3d([np.nanmin(x_t[:, :, 2]), np.nanmax(x_t[:, :, 2])])
ax.set_xlabel('X [mm]')
ax.set_ylabel('Y [mm]')
ax.set_zlabel('Z [mm]')
# set point-of-view: specified by (altitude degrees, azimuth degrees)
#ax.view_init(30, 25)
# initialization function: plot the background of each frame
def init():
for line, pt in zip(lines, pts):
line.set_data(np.array([]), np.array([]))
line.set_3d_properties(np.array([]))
pt.set_data(np.array([]),np.array([]))
pt.set_3d_properties(np.array([]))
return lines + pts
# animation function. This will be called sequentially with the frame number
def animate(i):
i = (5 * i) % x_t.shape[1]
for pt, xi in zip(pts, x_t):
x, y, z = xi[:i].T
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)
fig.canvas.draw()
return lines + pts + stick_lines
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=500, interval=30, blit=True, repeat=False)
# Save as mp4. This requires mplayer or ffmpeg to be installed
#anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))
plt.show()
anim.save('test.mp4', writer='ffmpeg')
This does not save animation. However,
anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))
Does save the animation but the rendering is not really sharp and quite blurry. How do I change that ?
The other points are:
How do I change all the scatter points to a single color ?eg dark grey or black
How do I change the index finger only to a different color? eg Index finger color red
best wishes,
PS: Attached is the animation
import numpy as np
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
#=============================================================================================
t_start = 2# start frame
t_end = 1711# end frame
data = pd.read_csv('Sub6_cylinder0009_index_flex_processed.csv') # only coordinate data
df = data.loc[t_start:t_end,'FT1X':'CMC5Z']
# Find max and min values for animation ranges
df_minmax = pd.DataFrame(index=list('xyz'),columns=range(2))
for i in list('xyz'):
c_max = df.max().max()
c_min = df.min().min()
df_minmax.loc[i] = np.array([c_min,c_max])
df_minmax = 3.3*df_minmax # increase by 30% to make animation look better
df.columns = np.repeat(range(22),3) # store cols like this for simplicity
print(df.columns)
N_tag = int(df.shape[1]/3) # nr of tags used (all)
N_trajectories = N_tag
t = np.linspace(0,data.Time[t_end],df.shape[0]) # pseudo time-vector for first walking activity
x_t = np.zeros(shape=(N_tag,df.shape[0],3)) # empty animation array (3D)
for tag in range(22):
# # store data in numpy 3D array: (tag,time-stamp,xyz-coordinates)
x_t[tag,:,:] = df[tag]
x_t = x_t[:, :, [0, 2, 1]]
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection='3d')
ax.axis('on')
#choose a different color for each trajectory
#colors1 = plt.cm.Reds(np.linspace(0, 1, N_trajectories))
#colors2 = plt.cm.gray(np.linspace(0, 1, N_trajectories))
# set up trajectory lines
#lines = sum([ax.plot([], [], [], '-', c=c) for c in colors1], [])
# set up points
#pts = sum([ax.plot([], [], [], 'o--', c=c) for c in colors2], [])
#pts = ax.plot([], [], [], 'o', c=colors2[0])
#pts = ax.plot([], [], [], 'o-', c=colors2[8])
#set up lines which create the stick figures
stick_defines = [
(0, 13), # Thumb Tip
(13, 14),
(14, 19),
(1, 5), #Index Tip
(2, 6), # Middle Tip
(3, 7), # Ring Tip
(4, 8), # Pinky Tip
(5, 9),
(6, 10),
(7, 11),
(8, 12),
(9, 15),
(10, 16),
(11, 17),
(12, 18),
(18, 21),
(15, 20)
# (22, 23),
# (23, 24),
# (22, 26),
# (25,26)
]
stick_defines1 = [
# (0, 13), # Thumb Tip
# (13, 14),
# (14, 19),
(1, 5), #Index Tip
# (2, 6), # Middle Tip
# (3, 7), # Ring Tip
# (4, 8), # Pinky Tip
(5, 9),
# (6, 10),
# (7, 11),
# (8, 12),
(9, 15),
# (10, 16),
# (11, 17),
# (12, 18),
# (18, 21),
(15, 20)
# (22, 23),
# (23, 24),
# (22, 26),
# (25,26)
]
stick_lines = [ax.plot([], [], [], 'ko--', alpha=0.5)[0] for _ in stick_defines]
stick_lines1 = [ax.plot([], [], [], 'ro-')[0] for _ in stick_defines1]
print(stick_lines)
ax.set_xlim3d([np.nanmin(x_t[:, :, 0]), np.nanmax(x_t[:, :, 0])])
ax.set_ylim3d([np.nanmin(x_t[:, :, 1])-400, np.nanmax(x_t[:, :, 1])+400])
ax.set_zlim3d([np.nanmin(x_t[:, :, 2]), np.nanmax(x_t[:, :, 2])])
ax.set_xlabel('X [mm]')
ax.set_ylabel('Y [mm]')
ax.set_zlabel('Z [mm]')
# set point-of-view: specified by (altitude degrees, azimuth degrees)
#ax.view_init(30, 25)
# initialization function: plot the background of each frame
# def init():
# for pt in pts:#zip(pts):
# # line.set_data(np.array([]), np.array([]))
# # line.set_3d_properties(np.array([]))
# pt.set_data(np.array([]), np.array([]))
# pt.set_3d_properties(np.array([]))
# return pts
# animation function. This will be called sequentially with the frame number
def animate(i):
# we'll step two time-steps per frame. This leads to nice results.
i = (5 * i) % x_t.shape[1]
pts = []
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()
for stick_line1, (sp, ep) in zip(stick_lines1, stick_defines1):
stick_line1._verts3d = x_t[[sp,ep], i, :].T.tolist()
#ax.view_init(30, 0.3 * i)
fig.canvas.draw()
return pts + stick_lines+stick_lines1
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate, frames=500, interval=30, blit=True, repeat=False)
plt.show()
# Save as mp4. This requires mplayer or ffmpeg to be installed
anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))
#anim.save('test.mp4', writer='ffmpeg')
I have managed to answer my questions, however, the saving of the animation still remains a problem, the current version works but the video is blurry and is not particularly sharp. The last line of code which is commented out simply does not work for unknown reasons.
If you have a better way to have save the animation with better resolution please let me know.
Attached is the animation.
best wishes,
I am struggling with tweaking a plot, I have been working on.
I am facing to two problems:
The plots should be adjacent and with 0 wspace and hspace. I set both values to zero but still there are some spaces between the plots.
I would like to have one colorbar for all the subplots (they all the same range). Right now, the code adds a colorbar to the last subplot as i understand that it needs the third return value of hist2D.
Here is my code so far:
def plot_panel(pannel_plot):
fig, ax = plt.subplots(3, 2, figsize=(7, 7), gridspec_kw={'hspace': 0.0, 'wspace': 0.0}, sharex=True, sharey=True)
fig.subplots_adjust(wspace=0.0)
ax = ax.flatten()
xmin = 0
ymin = 0
xmax = 0.19
ymax = 0.19
hist2_num = 0
h =[]
for i, j in zip(pannel_plot['x'].values(), pannel_plot['y'].values()):
h = ax[hist2_num].hist2d(i, j, bins=50, norm=LogNorm(vmin=1, vmax=5000), range=[[xmin, xmax], [ymin, ymax]])
ax[hist2_num].set_aspect('equal', 'box')
ax[hist2_num].tick_params(axis='both', top=False, bottom=True, left=True, right=False,
labelsize=10, direction='in')
ax[hist2_num].set_xticks(np.arange(xmin, xmax, 0.07))
ax[hist2_num].set_yticks(np.arange(ymin, ymax, 0.07))
hist2_num += 1
fig.colorbar(h[3], orientation='vertical', fraction=.1)
plt.show()
And the corrsiponding result:
Result
I would be glad for any heads up that i am missing!
You can use ImageGrid, which was designed to make this kind of things easier
data = np.vstack([
np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(figsize=(4, 6))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(3, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
cbar_mode="single",
cbar_location="right",
cbar_pad=0.1
)
for ax in grid:
h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='vertical')
or
data = np.vstack([
np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(figsize=(4, 6))
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(3, 2), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
cbar_mode="single",
cbar_location="top",
cbar_pad=0.1
)
for ax in grid:
h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='horizontal')
grid.cbar_axes[0].xaxis.set_ticks_position('top')
I have written the following code for the generation of all "triangulations" of a regular or convex polygon:
def getTriangles(points,i,j):
print('i={}, j={}'.format(i,j))
ee = []
if j-i<2:
return []
if j-i==2:
return [[[i,i+1,j]]]
for k in range(i+1,j):
print(' k={}'.format(k))
e1= getTriangles(points,i,k)
e2 = getTriangles(points,k,j)
for x in e1:
for y in e2:
e = [[i,k,j]]
e.extend(x)
e.extend(y)
ee.append(e)
if len(e1)==0:
for y in e2:
e = [[i,k,j]]
e.extend(y)
ee.append(e)
if len(e2)==0:
for a in e1:
e = [[i,k,j]]
e.extend(x)
ee.append(e)
print(' e1={}, e2={}, ee={}'.format(e1,e2,ee))
return ee
n=5
tr = getTriangles(range(1,n+1),0,n-1)
print()
print(tr)
print(len(tr))
For n=3,4 it is correct, and in general "navigates" through the right number of possible triangulations (that is the Catalan number) for n=3,4,5,6,7,8, but the triangulations are not unique. here the formatted output for n=5, consisting of a list of triangles (e.g. three vertices in [0,1,2,3,4]):
[[[0, 1, 4], [1, 2, 4], [2, 3, 4]],
[[0, 1, 4], [1, 3, 4], [1, 2, 3]],
[[0, 2, 4], [0, 1, 2], [2, 3, 4]],
[[0, 3, 4], [0, 2, 3], [0, 1, 2]],
[[0, 3, 4], [0, 2, 3], [0, 1, 2]]]
as you can see the last two are equal. Where is the error?
Intuitively the code is more complex than needed.
EDIT As you can see I'm not in bad company for this error: here is Robert Sedgewick, a computer science professor at Princeton University and in the background you see that the n=5 is correct but for n=6 there are double ;-)
The following code seams to work. The change is in the middle. The algorithm fix the edge [i,j] and k moves from i+1 to j-1. The triangle ijk is fixed and split the polygon in three sub-polygons: itself, the polygon i...k, and the polygon, k..j. If k=i+1 or k=j-1, one of the two polygon is degenerate (empty):
def getTriangles(points,i,j):
print('i={}, j={}'.format(i,j))
ee = []
if j-i<2:
return []
if j-i==2:
return [[[i,i+1,j]]]
for k in range(i+1,j): # k is the vertex such that the triangle ikj split the polygon 1j in 2 subspace plus the triangle ikj
print(' k={}'.format(k))
e1= getTriangles(points,i,k)
e2 = getTriangles(points,k,j)
if k==i+1:
for y in e2:
e = [[i,k,j]]
e.extend(y)
ee.append(e)
elif k==j-+1:
for x in e1:
e = [[i,k,j]]
e.extend(x)
ee.append(e)
else:
for x in e1:
for y in e2:
e = [[i,k,j]]
e.extend(x)
e.extend(y)
ee.append(e)
print(' e1={}, e2={}, ee={}'.format(e1,e2,ee))
return ee
n=5
tr = getTriangles(range(1,n+1),0,n-1)
print()
print(tr)
print(len(tr))
Here's a more Pythonic version of the above:
def genTriangles(i, j):
if j - i < 2:
yield []
return
if j - i == 2:
yield [(i, i+1, j)]
return
for k in range(i + 1, j):
for x in genTriangles(i, k):
for y in genTriangles(k, j):
yield x + y + [(i, k, j)]
n = 5
for k, tr in enumerate(genTriangles(0, n - 1), 1):
print(k, tr)
1 [(2, 3, 4), (1, 2, 4), (0, 1, 4)]
2 [(1, 2, 3), (1, 3, 4), (0, 1, 4)]
3 [(0, 1, 2), (2, 3, 4), (0, 2, 4)]
4 [(1, 2, 3), (0, 1, 3), (0, 3, 4)]
5 [(0, 1, 2), (0, 2, 3), (0, 3, 4)]
I generated this plot and I cannot display a value properly for each bar. Here is my bar plot:
This is my code for creating the above plot:
X_perc = [10, 7, 3, 5, 5]
cols = ['3.1-4.14', '4.14-5.18', '6.22-7.26', '7.26-8.3', '5.18-6.22']
data = np.array([[10, 7, 5, 5, 3],])
fig, ax=plt.subplots()
for i, (name, v) in enumerate(zip(cols, X_perc)):
bottom=np.sum(data[:,0:i], axis=1)
ax.bar(1,data[:,i], bottom=bottom, label="{}".format(name))
ax.text(0.7, (v*i)/v * i + v + i, str(v), fontweight='bold')
plt.legend(framealpha=1)
plt.axis([-10, 10, 0, 31])
plt.tick_params(
axis='x',
which='both',
bottom=False,
top=False,
labelbottom=False)
You can use a cumulative sum of positions to annotate your stacked bars
positions = np.cumsum(data)
fig, ax=plt.subplots()
for i, (name, v) in enumerate(zip(cols, X_perc)):
bottom=np.sum(data[:,0:i], axis=1)
ax.bar(1,data[:,i], bottom=bottom, label="{}".format(name))
ax.text(0.7, positions[i], str(v), fontweight='bold') # Use it here
Input= 2 2 2 1 2 0 1 0 0 0 0 1
first number is X coordinate in normal XY axis (not list), second Y coordinate, third X and so on; so from this input it will look like:
Y
2 *
1* *
0* * *
0 1 2 X
(first*: 2,2, second*: 2,1, third*:2,0 - going from right side).
I need to get output which would look like:
output=
[[0,0,1],
[1,0,1],
[1,1,1]]
So far I tried this, but don't know how to continue:
inp=[2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
maxx=0
maxy=0
for i in range(1,len(inp),2): #yaxis
if inp[i]>maxy:
maxy=inp[i]
continue
else:
continue
for j in range(0,len(inp),2): #xaxis
if inp[j]>maxx:
maxx=inp[j]
continue
else:
continue
part=[]
for i in range(maxy+1):
part.append([0 for j in range (maxx+1)])
for k in range(0,len(inp),2):
for j in range(1,len(inp),2):
for i in range(len(part)):
part[i][j]=
inp = [2,2,2,1,2,0,1, 0, 0, 0, 0, 1]
tuples = [(inp[i], inp[i+1]) for i in range(0, len(inp), 2)]
print(tuples) # [(2, 2), (2, 1), (2, 0), (1, 0), (0, 0), (0, 1)]
# Define the dimensions of the matrix
max_x_value = max([i[0] for i in tuples])+1
max_y_value = max([i[1] for i in tuples])+1
# Build the matrix - fill all cells with 0 for now
res_matrix = [[0 for _ in range(max_y_value)] for _ in range(max_x_value)]
# Iterate through the tuples and fill the 1's into the matrix
for i in tuples:
res_matrix[i[0]][i[1]]=1
print(res_matrix) # [[1, 1, 0], [1, 0, 0], [1, 1, 1]]
# Rotate the matrix by 90 to get the final answer
res = list(map(list, list(zip(*res_matrix))[::-1]))
print(res) # [[0, 0, 1], [1, 0, 1], [1, 1, 1]]