Skip to content

Instantly share code, notes, and snippets.

@Bowenislandsong
Created March 15, 2022 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bowenislandsong/fd4877c50feceaa224e91d0313556de0 to your computer and use it in GitHub Desktop.
Save Bowenislandsong/fd4877c50feceaa224e91d0313556de0 to your computer and use it in GitHub Desktop.
This graph generator plots different step signals and show them in a 3d plot with colorful views.
from matplotlib import pyplot as plt
from matplotlib.collections import PolyCollection
import numpy as np
# Graph Auxilary
'''
Graph_runsteps_3D super positions different running step signals and show them on the same
timeline. Each signal has a different color and provide a general view of the signals.
mtx_time: time of a signal. (optional) If black, plot them by samples. Dimention should be the same as mtx_signal
mtx_signal: array of signals. Each run step is represented by a array of values (signal). The mtx layout is run-step X signal.
ax is the axis of the plotting figure. It can be a subfigure or a single figure axis.
See demo in https://matplotlib.org/2.0.2/examples/mplot3d/polys3d_demo.html
'''
def Graph_runsteps_3D(mtx_signal:list,mtx_time:list=[],xlabel="Time",ylabel="Steps",zlabel="Magnitude",title="Step Alignment"):
assert len(mtx_signal) != 0, "input signal matrix to Graph_runsteps_3D should not be zero."
assert len(mtx_time)==0 or (len(mtx_signal) == len(mtx_time) and np.shape(mtx_signal) == np.shape(
mtx_time)), "input signal matrix should have the same dimention as its time matrix."
if len(mtx_time) == 0:
for sig in mtx_signal:
mtx_time.append(np.arange(1,len(sig)+1))
zs = np.arange(1, len(mtx_signal)+1)
vals = []
xlim = [0,1]
zlim = [0, 1]
ylim = [0, len(mtx_signal)+2]
color = []
def extend_lim(lim,low,high):
if lim[-1]< high:
lim[-1] = high
if lim[0] > low:
lim[0] = low
for i in range(len(mtx_signal)):
vals.append(list(zip(mtx_time[i], mtx_signal[i])))
extend_lim(zlim, min(mtx_signal[i]),max(mtx_signal[i]))
extend_lim(xlim, min(mtx_time[i]), max(mtx_time[i]))
color.append(np.random.random((1, 3)))
fig = plt.figure()
ax = fig.gca(projection='3d')
poly = PolyCollection(vals, facecolors=color)
poly.set_alpha(0.7)
ax.add_collection3d(poly, zs=zs, zdir='y')
ax.set_xlabel(xlabel)
ax.set_xlim3d(*xlim)
ax.set_ylabel(ylabel)
ax.set_ylim3d(*ylim)
ax.set_zlabel(zlabel)
ax.set_zlim3d(*zlim)
plt.show()
@Bowenislandsong
Copy link
Author

step
Example graph of left shank signals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment