For context, I have a 2D Random walker function that outputs two 2d arrays. The two arrays represent the x position and the y position. Both these arrays are 2D because the No of columns represents the No of particles (100 columns is 100 particles) and the No of rows represents the steps each particle took (100 steps is 100 rows).
The question: how do I select just the x position 2d array to plot the 50th row (50 steps) and all the columns (500) for that 50th row, in order to plot on a histogram?
Below is the attempt I have made. Where particle_motion_2D(500,50,20) is the function. 500 is No of particles, 50 steps, 20 box width (which is unimportant for the question)
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import random as rd
import math as m
def particle_motion_2D(P_xy,Steps,bwidth):
X_Steps=[0,-1,1]
Y_Steps=[0,-1,1]
P_x= np.zeros(P_xy)
P_y= np.zeros(P_xy)
States_Px=np.array([P_x])
States_Py=np.array([P_y])
for a in range(Steps):
Box_Max_x=np.where(P_x >= bwidth/2)
Box_Min_x=np.where(P_x <= -bwidth/2)
Box_Max_y=np.where(P_y >= bwidth/2)
Box_Min_y=np.where(P_y <= -bwidth/2)
P_Mnt_x=np.array([rd.choice(X_Steps) for i in P_x])
P_Mnt_y=np.array([rd.choice(Y_Steps) for i in P_y])
P_Mnt_x[Box_Max_x]=-1
P_Mnt_x[Box_Min_x]=+1
P_Mnt_y[Box_Max_y]=-1
P_Mnt_y[Box_Min_y]=+1
P_x= P_x + P_Mnt_x
P_y= P_y + P_Mnt_y
States_Px = np.append(States_Px,[P_x],axis=0)
States_Py = np.append(States_Py,[P_y],axis=0)
return States_Px, States_Py
Particle_RdWalk2D=particle_motion_2D(500,50,20)
Histo2d=Particle_RdWalk2D[50][0]
fig = plt.figure()#Plotting.
ax1.hist(Histo2d,bins=20,density=True)
I think this is a solution:
Particle_RdWalk2D=particle_motion_2D(500,50,20)
Histo2d=Particle_RdWalk2D[0]
This selects the 1st output of the f() as python indexes from 0
To select the rows and columns we want we use indexing (for the 2d array)
Rows/columns_we_want= Histo2d[50][:500]
[50] is the row which is step 50 & [:500] where : means all the columns up to 500 (i.e. 500 particles).
fig = plt.figure().
ax1.hist(Rows/columns,bins=20,density=True)