python - loop through numpy arrays, plot all arrays to single figure (matplotlib) -


the functions below each plot single numpy array
plot1d, plot2d, , plot3d take arrays 1, 2, , 3 columns, respectively

import numpy np  import matplotlib.pyplot plt  mpl_toolkits.mplot3d import axes3d   def plot1d(data):     x=np.arange(len(data))     plot2d(np.hstack((np.transpose(x), data)))  def plot2d(data):     # type: (object) -> object     #if 2d, make scatter     plt.plot(data[:,0], data[:,1], *args, **kwargs)  def plot3d(data):     #if 3d, make 3d scatter     fig = plt.figure()     ax = fig.add_subplot(111, projection='3d')     ax.plot(data[:,0], data[:,1], data[:,2], *args, **kwargs) 

i ability input list of 1, 2, or 3d arrays , plot arrays list onto 1 figure

i have added looping elements, unsure how hold figure , add additional plots...

def plot1d_list(data):     in range(0, len(data)):         x=np.arange(len(data[i]))         plot2d(np.hstack((np.transpose(x), data[i])))  def plot2d_list(data):     # type: (object) -> object     #if 2d, make scatter     in range(0, len(data)):         plt.plot(data[i][:,0], data[i][:,1], *args, **kwargs)  def plot3d_list(data):     #if 3d, make 3d scatter     in range(0, len(data)):         fig = plt.figure()         ax = fig.add_subplot(111, projection='3d')         ax.plot(data[i][:,0], data[i][:,1], data[i][:,2], *args, **kwargs) 

to plot multiple data sets on same axes, can this:

def plot2d_list(data,*args,**kwargs):     # type: (object) -> object     #if 2d, make scatter     n = len(data)     fig,ax = plt.subplots() #create figure , axes     in range(n):         #now plot data set         ax.plot(data[i][:,0], data[i][:,1], *args, **kwargs) 

your other functions can generalised in same way. here's example of using above function 5 sets of randomly generated x-y coordinates, each length 100 (each of 5 data sets appears different color):

import numpy np  x = np.random.randn(5,100,2) plot2d_list(x,'o') plt.show() 

enter image description here


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -