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()
Related
I am trying to plot the following:
#Time
for t in np.arange(1,10,1):
#Raidus
for r in np.arange(1,5,1):
#Velocity in theta direction
V = C/r*(1-np.exp(-r**2/(4*v*t)))
print(r,V)
#Vorticity
Z = C*((1/(2*v*t))*np.exp(-r**2/(4*v*t))-(1-np.exp(-r**2/(4*v*t)))/r**2)
plt.plot(r,V)
When I print(r,V) python does show 9 tables (each for a different t) with radius from 1-4.
However, when I plot, the plot looks completely empty.
Thanks for the help.
Save the r and the v in two lists.
Append the list in each loop with a new r and a new v.
After the interior loop, pass the lists to the plot and empty them.
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!
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
I'm having some trouble using matplotlib to plot the path of something.
Here's a basic version of the type of thing I'm doing.
Essentially, I'm seeing if the value breaks a certain threshold (6 in this case) at any point during the path and then doing something with it later on.
Now, I have 3 lists set-up. The end_vector will be based on the other two lists. If the value breaks past 2 any time during a single simulation, I will add the last position of the object to my end_vector
trajectories_vect is something I want to keep track of my trajectories for all 5 simulations, by keeping a list of lists. I'll clarify this below. And, timestep_vect stores the path for a single simulation.
from random import gauss
from matplotlib import pyplot as plt
import numpy as np
starting_val = 5
T = 1 #1 year
delta_t = .1 #time-step
N = int(T/delta_t) #how many points on the path looked at
trials = 5 #number of simulations
#main iterative loop
end_vect = []
trajectories_vect = []
for k in xrange(trials):
s_j = starting_val
timestep_vect = []
for j in xrange(N-1):
xi = gauss(0,1.0)
s_j *= xi
timestep_vect.append(s_j)
trajectories_vect.append(timestep_vect)
if max(timestep_vect) > 5:
end_vect.append(timestep_vect[-1])
else:
end_vect.append(0)
Okay, at this part if I print my trajectories, I get something like this (I only posted two simulations, instead of the full 5):
[[ -3.61689976e+00 2.85839230e+00 -1.59673115e+00 6.22743522e-01
1.95127718e-02 -1.72827152e-02 1.79295788e-02 4.26807446e-02
-4.06175288e-02] [ 4.29119818e-01 4.50321728e-01 -7.62901016e-01
-8.31124346e-02 -6.40330554e-03 1.28172906e-02 -1.91664737e-02
-8.29173982e-03 4.03917926e-03]]
This is good and what I want to happen.
Now, my problem is that I don't know how to plot my path (y-axis) against my time (x-axis) properly.
First, I want to put my data into numpy arrays because I'll need to use them later on to compute some statistics and other things which from experience numpy makes very easy.
#creating numpy arrays from list
#might need to use this with matplotlib somehow
np_trajectories = np.array(trajectories_vect)
time_array = np.arange(1,10)
Here's the crux of the issue though. When i'm putting my trajectories (y-axis) into matplotlib, it's not treating each "list" (row in numpy) as one path. Instead of getting 5 paths for 5 simulations, I am getting 9 paths for 5 simulations. I believe I am inputing stuff wrong hence it is using the 9 time intervals in the wrong way.
#matplotlib stuff
plt.plot(np_trajectories)
plt.xlabel('timestep')
plt.ylabel('trajectories')
plt.show()
Here's the image produced:
Obviously, this is wrong for the aforementioned reason. Instead, I want to have 5 paths based on the 5 lists (rows) in my trajectories. I seem to understand what the problem is but don't know how to go about fixing it.
Thanks in advance for the help.
When you call np_trajectories = np.array(trajectories_vect), your list of trajectories is transformed into a 2d numpy array. The information about its dimensions is stored in np_trajectories.shape, and, in your case, is (5, 9). Therefore, when you pass np_trajectories to plt.plot(), the plotting library assumes that the y-values are stored in the first dimension, while the second dimension describes individual lines to plot.
In your case, all you need to do is to transpose your np_trajectories array. In numpy, it is as simple as
plt.plot(np_trajectories.T)
plt.xlabel('timestep')
plt.ylabel('trajectories')
plt.show()
If you want to plot the x-axis as time, instead of steps of one, you have to define your time progression as a list or an array. In numpy, you can do something like
times = np.linspace(0, T, N-1)
plt.plot(times, np_trajectories.T)
plt.xlabel('timestep')
plt.ylabel('trajectories')
plt.show()
which produces the following figure:
I want to plot an approximation of the number "pi" which is generated by a function of two uniformly distributed random variables. The goal is to show that with a higher sample draw the function value approximates "pi".
Here is my function for pi:
def pi(n):
x = rnd.uniform(low = -1, high = 1, size = n) #n = size of draw
y = rnd.uniform(low = -1, high = 1, size = n)
a = x**2 + y**2 <= 1 #1 if rand. draw is inside the unit cirlce, else 0
ac = np.count_nonzero(a) #count 1's
af = np.float(ac) #create float for precision
pi = (af/n)*4 #compute p dependent on size of draw
return pi
My problem:
I want to create a lineplot that plots the values from pi() dependent on n.
My fist attempt was:
def pipl(n):
for i in np.arange(1,n):
plt.plot(np.arange(1,n), pi(i))
print plt.show()
pipl(100)
which returns:
ValueError: x and y must have same first dimension
My seocond guess was to start an iterator:
def y(n):
n = np.arange(1,n)
for i in n:
y = pi(i)
print y
y(1000)
which results in:
3.13165829146
3.16064257028
3.06519558676
3.19839679359
3.13913913914
so the algorithm isn't far off, however i need the output as a data type which matplotlib can read.
I read:
http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html#routines-array-creation
and tried tom implement the function like:
...
y = np.array(pi(i))
...
or
...
y = pi(i)
y = np.array(y)
...
and all the other functions that are available from the website. However, I can't seem to get my iterated y values into one that matplotlib can read.
I am fairly new to python so please be considerate with my simple request. I am really stuck here and can't seem to solve this issue by myself.
Your help is really appreciated.
You can try with this
def pipl(n):
plt.plot(np.arange(1,n), [pi(i) for i in np.arange(1,n)])
print plt.show()
pipl(100)
that give me this plot
If you want to stay with your iterable approach you can use Numpy's fromiter() to collect the results to an array. Like:
def pipl(n):
for i in np.arange(1,n):
yield pi(i)
n = 100
plt.plot(np.arange(1,n), np.fromiter(pipl(n), dtype='f32'))
But i think Numpy's vectorize would be even better in this case, it makes the resulting code much more readable (to me). With this approach you dont need the pipl function anymore.
# vectorize the function pi
pi_vec = np.vectorize(pi)
# define all n's
n = np.arange(1,101)
# and plot
plt.plot(n, pi_vec(n))
A little side note, naming a function pi which does not return a true pi seems kinda tricky to me.