Plotting subplots from a nested for loop in Python - python

I am trying to plot line plots(Drifted brownian motion) for different values of mu and sigma, I have a function that iterates a list of possible mu values and possible sigma values and it's supposed to then return the resulting plots. The problem is I am unsure how to make the subplots return the required number of rows. I have given it the correct nrows and ncols but the problem comes in with the indexing. Does anyone have a trick to solve this?
I have provided the code and the error message below,
# Drifted BM for varying values mu and sigma respectively
def DriftedBMTest2(nTraj=50,T=5.0,dt=0.01,n=5, sigma = [0.1,1.0,2], mulist=[0,0.5,1,1.5], ValFSize=(18,14)):
nMu = len(mulist)
nSigma = len(mulist)
# Discretize, dt = time step = $t_{j+1}- t_{j}$
dt = T/(n-1)
# Loop on different value sigma
for z in range(nSigma):
# Loop on different value Mu
for k in range(nMu):
n=int(T/dt)
x=np.zeros(n+1,float)
# Create plot space
temp = nSigma*nMu/2
plt.subplot(temp,2,k+1)
plt.title("Drifted BM $\sigma$={}, $\mu$={}".format(sigma[z],mulist[k]))
plt.xlabel(r'$t$')
plt.ylabel(r'$W_t$');
# Container for colours for each trajectory
colors = plt.cm.jet(np.linspace(0,1,nTraj))
# Generate many trajectories
for j in range(nTraj):
# Time simulation
# Add the time * constant(mu)
for i in range(n):
x[i+1]=x[i]+np.sqrt(dt)*np.random.randn() + i*mulist[k]
# Scale Each Tradjectory
x = x * sigma[z]
# Plot trajectory just computed
plt.plot(np.linspace(0,T,n+1),x,'b-',alpha=0.3, color=colors[j], lw=3.0)
DriftedBMTest2( sigma = [1,2], mulist=[-2,1] )
I then get the first two plots but not all of them and the error below.
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
Sorry if this is a bad question, I am new to Python but any help would be appreciated.

Try adding fig = plt.figure() between the two for loops
for z in range(nSigma):
# Loop on different value Mu
fig = plt.figure() # <---- Line added here
for k in range(nMu):
If that doesn't give the desired layout, you can try moving it to the inner for loop as
for z in range(nSigma):
# Loop on different value Mu
for k in range(nMu):
fig = plt.figure() # <---- Line added here

Related

Plot overwriting in for loop

I'm using FeniCS to solve a PDE at different time-steps which I then store into various lists and plot in python using matplotlib. I'm having problems trying to create and save multiple (three) plots in a loop. I can only manage to save one plot without them overwriting. Neglecting necessary details, my code looks like this
for n in range(num_steps):
#Update current time
t += dt
#Solve
solve(a_form == L_form, u)
#Store times
t_vals.append(t)
#Solve PDE, gives solution u
solve(u)
#Create empty lists
u_vals_x = []
u_vals_y = []
u_vals_z = []
#Set constant
xyz_fixed_density = 1000
#Store u values varying x, y and z held equal to 1
for n in np.linspace(x0,x1,xyz_fixed_density):
u_vals_x.append(u(n,1,1))
#Store u values varying y, x and z held equal to 1
for n in np.linspace(y0,y1,xyz_fixed_density):
u_vals_y.append(u(1,n,1))
#Store u values varying z, x and y held equal to 1
for n in np.linspace(z0,z1,xyz_fixed_density):
u_vals_z.append(u(1,1,n))
#First plot
plt.scatter(np.linspace(x0,x1,xyz_fixed_density),u_vals_x,s=1)
plt.legend(t_vals)
plt.xlabel('$x$')
plt.ylabel('$u(t,x,1,1)$')
plt.savefig('u_vs_x.png')
#Second plot
plt.scatter(np.linspace(y0,y1,xyz_fixed_density),u_vals_y,s=1)
plt.legend(t_vals)
plt.xlabel('$y$')
plt.ylabel('$u(t,1,y,1)$')
plt.savefig('u_vs_y.png')
#Third plot
plt.scatter(np.linspace(z0,z1,xyz_fixed_density),u_vals_z,s=1)
plt.legend(t_vals)
plt.xlabel('$z$')
plt.ylabel('$u(t,1,1,z)$')
plt.savefig('u_vs_z.png')
It's probably a simple fix but I can't seem to get it to work. Thanks in advance.
Use the current iteration (n) as part of the filenames; e.g. replace
plt.savefig('u_vs_x.png')
with
plt.savefig(f'u_vs_x_{n}.png')
This uses the f-string syntax to format the code. If you’re using an older Python version which does not support f-strings yet, use format explicitly:
plt.savefig('u_vs_x_{}.png'.format(n))
You’ll also need to create a new plot each time, e.g. via
plt.figure()

plotting equations with increasing variables python

i have been trying to create a function (Pmotion in the code below) that with several parameters gives me real and imaginary parts of the equation(that part is ok)
but in the next step i want to run the function for an increasing variable(in this case time(t) going up in jumps of 0.1 all the way to 2) and be able to plot the all these samples in an plot of the real part(Up_real in the y axis) and t in the x axis
how can i get to increase while still retaining the possibility of an initial t input?
any help would be amazing
def Pmotion(x,t,A,alpha,f):
w=2*np.pi*f
k1 = (w/alpha)
theta = k1*x-w*t
Up = k1*A*complex(-np.sin(theta),np.cos(theta))
Up_real = Up.real
Up_imag = Up.imag
plt.plot([t],[UP_real]) #here i want these to be in the x and y axis
plt.show()
#Pmotion(x=0,t=0,A=1,alpha=6000,f=2)
First of all, divide your code in small independent blocks (high cohesion) as such create a function with the desired calculation:
def Pmotion(x,t,A,alpha,f):
w=2*np.pi*f
k1 = (w/alpha)
theta = k1*x-w*t
Up = k1*A*complex(-np.sin(theta),np.cos(theta))
Up_real = Up.real
Up_imag = Up.imag
return Up_real, Up_imag
Then you can begin to think of a plotting method. e.g.
def plot_Pmotion_t():
t_range = np.arange(0,2,0.1)
reals = [Pmotion(0,t,1,6000,2) for t in t_range]
plt.plot(t_range, reals)
plt.show()
You can now freely alter or add inputs to the plot function without changing the Pmotion function.
Note: You are now plotting both real and imaginary values, change it to reals = [Pmotion(0,t,1,6000,2)[0] for t in t_range]
to only plot the real part.
Hope this helps!

Simulation with RK4, update ODE variable as the simulation goes

Problem :
I’m currently making a python app that simulates a set of coupled ordinary differentials equations that depends on a variable, let’s call it « X ».
As for now, I’m basically simulating this set of ODE with RK4 for given time then I’m plotting the graph on an animated plot with « matplotlib animation » embedded in tkinter.
The fact is that I would like to be able to modify « X » as the equations are resolved so that the simulation can change as we modify this variable.
Context :
The solution changes from that time on. To put things in context, these are the equations for the xenon and iodine abundance as well as neutrons flow in a nuclear reactor. The variable that change is "big sigma_b" that governs the control bars.
ODE :
xenon and iodine abundance ODE :
neutrons flow ODE :
Summary :
To summarize, « X » belongs to the range [1.0, 2.0], let’s say we want to run 400 hours of simulations :
We launch the simulation of ODE
We update the animated plot every second (which is equal to 1 hour of simulation on the graph)
We modify « X » with the help of a slider for instance (in fact "X" is not directly modified, there is a dynamic process that progressively move the initial "X" value toward its chosen value).
The RK4 algorithm take that into account
We can see that change on the updated graph
etc…
When the simulation is finished, we stop the plot updating
Hopefully, it’s clear enough.
How could I do that ?
Translating the ideas of the comments into a mockup gives some code to experiment with
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import matplotlib.animation as anim
import numpy as np
from scipy.integrate import odeint
# model is dampened oscillation with control variable u
# representing the equilibrium position
# x'' + 0.4*x + x == u
def model(x,u): return np.array([x[1], u - 0.4*x[1] - x[0]])
# integrate over about 10 periods
N=250
t = np.linspace(0, 12*np.pi, N+1) # time
# arrays to hold the computed values
X = np.zeros([N+1,2])
U = np.zeros([N+1])
# initial values
X[0] = [3, 2]
U[0] = 2;
# initialize plot
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
# initialize graphs
l1, = plt.plot(t[0], X[0,0], '-+b', ms=2 )
l2, = plt.plot(t[0], U[0], '-or', ms=2, lw=1 )
plt.xlim(t[0], t[-1]); plt.ylim(-1,3)
# construct slider
axcolor = 'black'
ax_U = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
sU = Slider(ax_U, 'U', 1.0, 2.0, valinit=U[0])
def animate(i):
# read the slider value
U[i] = sU.val
# integrate over the sub-interval
X[i] = odeint(lambda x,t: model(x,U[i]), X[i-1], [t[i-1],t[i]], atol=1e-4, rtol=1e-8)[-1];
# set the data to plot
l1.set_data(t[0:i+1],X[0:i+1,0]);
l2.set_data(t[0:i+1],U[0:i+1]);
return l1,l2
# start the animation, one should set the parameter to keep it from repeating
anim = anim.FuncAnimation(fig, animate, frames = range(1,N+1), blit=True, interval = 100 )
plt.show()

Unequal width binned histogram in python

I have an array with probability values stored in it. Some values are 0. I need to plot a histogram such that there are equal number of elements in each bin. I tried using matplotlibs hist function but that lets me decide number of bins. How do I go about plotting this?(Normal plot and hist work but its not what is needed)
I have 10000 entries. Only 200 have values greater than 0 and lie between 0.0005 and 0.2. This distribution isnt even as 0.2 only one element has whereas 2000 approx have value 0.0005. So plotting it was an issue as the bins had to be of unequal width with equal number of elements
The task does not make much sense to me, but the following code does, what i understood as the thing to do.
I also think the last lines of the code are what you really wanted to do. Using different bin-widths to improve visualization (but don't target the distribution of equal amount of samples within each bin)! I used astroml's hist with method='blocks' (astropy supports this too)
Code
# Python 3 -> beware the // operator!
import numpy as np
import matplotlib.pyplot as plt
from astroML import plotting as amlp
N_VALUES = 1000
N_BINS = 100
# Create fake data
prob_array = np.random.randn(N_VALUES)
prob_array /= np.max(np.abs(prob_array),axis=0) # scale a bit
# Sort array
prob_array = np.sort(prob_array)
# Calculate bin-borders,
bin_borders = [np.amin(prob_array)] + [prob_array[(N_VALUES // N_BINS) * i] for i in range(1, N_BINS)] + [np.amax(prob_array)]
print('SAMPLES: ', prob_array)
print('BIN-BORDERS: ', bin_borders)
# Plot hist
counts, x, y = plt.hist(prob_array, bins=bin_borders)
plt.xlim(bin_borders[0], bin_borders[-1] + 1e-2)
print('COUNTS: ', counts)
plt.show()
# And this is, what i think, what you really want
fig, (ax1, ax2) = plt.subplots(2)
left_blob = np.random.randn(N_VALUES/10) + 3
right_blob = np.random.randn(N_VALUES) + 110
both = np.hstack((left_blob, right_blob)) # data is hard to visualize with equal bin-widths
ax1.hist(both)
amlp.hist(both, bins='blocks', ax=ax2)
plt.show()
Output

Python/Matplotlib: Randomly select "sample" scatter points for different marker

Pretty much exactly what the question states, but a little context:
I'm creating a program to plot a large number of points (~10,000, but it will be more later on). This is being done using matplotlib's plt.scatter. This command is part of a loop that saves the figure, so I can later animate it.
What I want to be able to do is randomly select a small portion of these particles (say, maybe 100?) and give them a different marker than the rest, even though they're part of the same data set. This is so I can use them as placeholders to see the motion of individual particles, as well as the bulk material.
Is there a way to use a different marker for a small subset of the same data?
For reference, the particles are uniformly distributed just using the numpy random sampler, but my code for that is:
for i in range(N): # N number of particles
particle_position[i] = np.random.uniform(0, xmax) # Initialize in spatial domain
particle_velocity[i] = np.random.normal(0, 5) # Initialize in velocity space
for i in range(maxtime):
plt.scatter(particle_position, particle_velocity, s=1, c=norm_xvel, cmap=br_disc, lw=0)
The position and velocity change on each iteration of the main loop (there's quite a bit of code), but these are the main initialization and plotting routines.
I had an idea that perhaps I could randomly select a bunch of i values from range(N), and use an ax.scatter() command to plot them on the same axes?
Here is a possible solution to have a subset of your points identified with a different marker:
import matplotlib.pyplot as plt
import numpy as np
SIZE = 100
SAMPLE_SIZE = 10
def select_subset(seq, size):
"""selects a subset of the data using ...
"""
return seq[:size]
points_x = np.random.uniform(-1, 1, size=SIZE)
points_y = np.random.uniform(-1, 1, size=SIZE)
plt.scatter(points_x, points_y, marker=".", color="blue")
plt.scatter(select_subset(points_x, SAMPLE_SIZE),
select_subset(points_y, SAMPLE_SIZE),
marker="o", color="red")
plt.show()
It uses plt.scatter twice; once on the full data set, the other on the sample points.
You will have to decide how you want to select the sample of points - it is isolated in the select_subset function..
You could also extract the sample points from the data set to prevent marking them twice, but numpy is rather inefficient at deleting or resizing.
Maybe a better method is to use a mask? A mask has the advantage of leaving your original data intact and in order.
Here is a way to proceed with masks:
import matplotlib.pyplot as plt
import numpy as np
import random
SIZE = 100
SAMPLE_SIZE = 10
def make_mask(data_size, sample_size):
mask = np.array([True] * sample_size + [False ] * (data_size - sample_size))
np.random.shuffle(mask)
return mask
points_x = np.random.uniform(-1, 1, size=SIZE)
points_y = np.random.uniform(-1, 1, size=SIZE)
mask = make_mask(SIZE, SAMPLE_SIZE)
not_mask = np.invert(mask)
plt.scatter(points_x[not_mask], points_y[not_mask], marker=".", color="blue")
plt.scatter(points_x[mask], points_y[mask], marker="o", color="red")
plt.show()
As you see, scatter is called once on a subset of the data points (the ones not selected in the sample), and a second time on the sampled subset, and draws each subset with its own marker. It is efficient & leaves the original data intact.
The code below does what you want. I have selected a random set v_sub_index of N_sub indices in the correct range (0 to N) and draw those (with _sub suffix) from the larger samples particle_position and particle_velocity. Please note that you don't have to loop to generate random samples. Numpy has great functionality for that without having to use for loops.
import numpy as np
import matplotlib.pyplot as pl
N = 100
xmax = 1.
v_sigma = 2.5 / 2. # 95% of the samples contained within 0, 5
v_mean = 2.5 # mean at 2.5
N_sub = 10
v_sub_index = np.random.randint(0, N, N_sub)
particle_position = np.random.rand (N) * xmax
particle_velocity = np.random.randn(N)
particle_position_sub = np.array(particle_position[v_sub_index])
particle_velocity_sub = np.array(particle_velocity[v_sub_index])
particle_position_nosub = np.delete(particle_position, v_sub_index)
particle_velocity_nosub = np.delete(particle_velocity, v_sub_index)
pl.scatter(particle_position_nosub, particle_velocity_nosub, color='b', marker='o')
pl.scatter(particle_position_sub , particle_velocity_sub , color='r', marker='^')
pl.show()

Categories