i need to create a higher resolution of my plot with the linspace function but i can't figure out how to implement it into my code. Maybe someone has a better understanding of this and can help me.
import numpy as np
import matplotlib.pyplot as plt
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16])
ct = c/1000
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = np.zeros(n)
k = np.zeros(n)
for j in range(0,n):
A[j] = (ct[j] - ct0)/(cmax - ct0)
for j in range(0, n):
if j < 3:
k[j] = -(np.log(1 - A[j])) / N[j]
else:
k[j] = 1
MWk = np.mean(k)
Amod = np.zeros(n)
for j in range(0,n):
Amod[j] = 1 - np.exp((-N[j]) * MWk)
print(ct)
print(A)
print(k)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.plot(N, A, "g", label = "Aufschlussgrad")
plt.plot(N, Amod, "k", label = "Modelfunktion")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
There is no need to interpolate Amod, since it is a function you defined. On the other hand, it is necessary to perform an interpolation (either on A or on the original data c) in order to add more points to the graph. With only 4 or 5 points, the interpolation will not be very meaningful. In this case I choose to interpolate A.
The code was not taking profit of numpy's arrays, so I pythonized it a little (it looked like C)
import numpy as np
import matplotlib.pyplot as plt
def Amod(x, MWk):
return 1 - np.exp((-x) * MWk)
def k(x, A):
rv = -np.log(1 - A) / x
rv[np.nonzero(A==1)] = 1
return rv
# No real changes here: only a little 'pythonization'
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16])
ct = c/1000
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = (ct - ct0) / (cmax-ct0)
MWk = np.mean(k(N, A))
print(ct)
print(A)
print(k)
# we now interpolate A
# numpy's interpolation is linear... it is not useful for this case
from scipy.interpolate import interp1d
# interp1d returns a function that interpolates the data we provide
# in this case, i choose quadratic interpolation
A_interp_fun = interp1d(N, A, 'quadratic')
# Let's increase the number of points
new_x = np.linspace(N[0], N[-1])
A_xtra = A_interp_fun(new_x)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.scatter(N, A, label = "Aufschlussgrad - data")
plt.plot(new_x, A_xtra, "g", label="Aufschlussgrad - interpolation")
plt.scatter(N, Amod(N, MWk), label="Modelfunktion - data")
plt.plot(new_x, Amod(new_x, MWk), "k", label="Modelfunktion - function")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
Related
How could I get a list of all the zeroes of a matplotlib graph? By zeroes I mean every coordinate where the y-value of my function is at 0 (very close to zero also works for what I'm trying to accomplish). My code and the graph it generates are down below.
THE .WAV FILE: https://drive.google.com/drive/folders/11nE0nyd9UPViicSIKNeiBqSNbdWGRbIl?usp=sharing
MY CODE:
from scipy.io import wavfile
import matplotlib.pyplot as plt
import numpy as np
samplerate, data = wavfile.read(r'C:\Users\jack_l\Downloads\louduntitled.wav')
length = data.shape[0] / samplerate
import matplotlib.pyplot as plt
import numpy as np
time = np.linspace(0., length, data.shape[0])
plt.plot(time, data[:, 1], label="Right channel")
plt.legend()
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()
GRAPH:
I believe you want to find all times where the y-value is zero.
What about finding zero-crossings of the array data[:, 1] (here a), and use the returned indices to find the interpolated time values where y-value is zero.
CODE
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
# load data
samplerate, data = wavfile.read(r'C:\Users\jack_l\Downloads\louduntitled.wav')
length = data.shape[0] / samplerate
time = np.linspace(0., length, data.shape[0])
a = data[:, 1] # here put your data
zc_idxs = np.where(np.diff(np.sign(a)))[0] # indices of element before zero crossing
t_zero = []
for zc_i in zc_idxs: # interpolate each zero crossing
t1 = time[zc_i]
t2 = time[zc_i + 1]
a1 = a[zc_i]
a2 = a[zc_i + 1]
t_zero.append(t1 + (0 - a1) * ((t2 - t1) / (a2 - a1)))
plt.plot(time, a, label="Right channel")
plt.plot(t_zero, np.zeros((len(t_zero), 1)), 'o')
We can't access to your data, so I used the cos function to have a similar shape.
Here is my code that perform a linear interpolation (there's a small error due to the non-linear behavior of the cos function)
import numpy as np
n = 2000
x = np.linspace(0, 10, n)
y = np.cos(x)
res = []
is_positive = y[0] > 0
for i in range(n):
if y[i] == 0:
res.append(x[i])
elif (is_positive and y[i] < 0) or (not is_positive and y[i] > 0):
x_gap = x[i] - x[i-1]
coef_gap = y[i]/(y[i] - y[i-1])
res.append(x[i] + x_gap*coef_gap)
is_positive = not is_positive
analytic_res = [i*np.pi/2 for i in range(1,6,2)]
print(res)
print(analytic_res)
Output :
[1.5807794610537442, 4.722328378160516, 7.863877295269648]
[1.5707963267948966, 4.71238898038469, 7.853981633974483]
NB : if the very first value is zero, the code could have a weird behavior
I have made the below in order to create a cube by giving different X, Y, Z values, but when I give for example (6,3,2) I don't receive 6 blocks on X-axis, 3 blocks on Y-axis, and 2 blocks on the Z-axis, but I received a cube 6x6x4, why?
import matplotlib.pyplot as plt
import numpy as np
def make(X,Y,Z):
x, y, z = np.indices((X,Y,Z))
cube2 = (x==0) & (y==0) & (z==0)
for i in range (X):
for j in range (Y):
for k in range(Z):
cube1 = (x==i) & (y==j) & (z==k)
cube2 = cube2|cube1
colors = np.ones(cube2.shape, dtype=object)
from matplotlib import cm
for i in range (X):
for j in range (Y):
for k in range(Z):
if 0<i<=3:
colors[i][j][k] = cm.gray(((i+j+k)/X),alpha=0.8)
elif 3<i<=6:
colors[i][j][k] = cm.winter(((i+j+k)/Y),alpha=0.8)
else:
colors[i][j][k] = cm.copper(((i+j+k)/Z),alpha=0.8)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.voxels(cube2, facecolors=colors, edgecolor=None)
plt.show()
make(6,3,2,)
I also need your help with something else. I have created a bezier line according to the code.
#bezier line
import numpy as np
import matplotlib.pyplot as plt
A = np.array([10,20])
B = np.array([15,8])
C = np.array([8,22])
A = A.reshape(2,1)
B = B.reshape(2,1)
C = C.reshape(2,1)
t = np.arange(0.0, 1.0, 0.1).reshape(1,-1)
P0 = A * t + (1 - t) * B
P1 = B * t + (1 - t) * C
Pfinal = P0 * t + (1 - t) * P1
x=np.transpose(Pfinal)
x1= x[:, 0]
print(x1)
plt.plot(x1)
Is it possible to implement this to the above first code in order to choose different colors instead of what I have done in the if statement? For example, on the Z axis at the right of this bezier line to have cm.winter colors, at the left cm.copper or something similar.
Your code produces the expected plots. I don't see a problem. This is not an answer but a means of showing you the plot I get with your code. What are you looking for?
make(6,3,2)
I've written a code for my school project. The u and v matrices are generated already. I've made a 2D contour, but I wish to plot u and v in 3D.
import numpy as np
#import matplotlib.pyplot as plt
#inputs
m = 40
n = 20
Uinf = 2
delX = 0.05
delY = 0.05
nu = 0.8e-2
u= np.zeros((m+1,n))
v= np.zeros((m+1,n))
#intializing boundry conditions
u[:,0] = v[:,0] = v[:,n-1] = v[0,:] = 0
u[:,n-1] = u[0,:] = Uinf
#main program
for i in range(0,m):
for j in range(1,n-1):
a = (nu*delX) / (u[i,j]*delY**2)
b = (v[i,j]*delX) / (2*u[i,j]*delY)
u[i+1,j] = (a-b)*u[i,j+1]+(1-2*a)*u[i,j]+(a+b)*u[i,j-1]
v[i+1,j] = v[i+1,j-1]-(delY/(2*delX))*(u[i+1,j]-u[i,j]+u[i+1,j-1]-u[i,j-1])
I have to mention that I don't wanna generate an arbitrary function; u[i,j] and v[i,j] are present.
Thanks!
I need to integrate this function using trapezoidal rule in python:
theta = .518/r^2 * dr/(sqrt(2*1.158 + 2/r - .518^2/2r^2))
I have written my code and I should be seeing an ellipsoidal structure when plotted. theta should run from 0 to 2pi and r_min = .16 & r_max = .702
import numpy as np
import matplotlib.pyplot as plt
def trapezoidal(f, a, b, n):
h = float(b-a)/n
result = 0.5*f(a) + 0.5*f(b)
for i in range(1, n):
result += f(a + i*h)
result *= h
return result
intg =[]
v = lambda r: (0.5108/(r**2))* (1./np.sqrt(2*1.158+(2/r)-.5108**2/(2*r**2)))
n = np.arange(1,1000,100)
theta = np.arange(0,2*np.pi,100)
for j in n:
numerical = trapezoidal(v, .16,.702 , j)
intg.append(numerical)
plt.plot(numerical,theta)
plt.show()
I am doing some very elementary mistake I guess, because I am getting no plot out of it. I think the trapezoidal routine is correct, because it worked for other functions. your help is very appreciated
Alternatively, you could use quadpy (a project of mine).
import numpy as np
import quadpy
val = quadpy.line_segment.integrate_split(
lambda r: 0.5108/r**2 / np.sqrt(2*1.158 + 2/r - 0.5108**2/(2*r**2)),
0.15, 0.702, 100,
quadpy.line_segment.Trapezoidal()
)
print(val)
gives 0.96194633532. The trapezoidal formula is mostly implemented for historical purposes, however. A better and equally simple rule is quadpy.line_segment.Midpoint. An even better approach is certainly adaptive quadrature
val, error_estimate = quadpy.line_segment.integrate_adaptive(
lambda r: 0.5108/r**2 / np.sqrt(2*1.158 + 2/r - 0.5108**2/(2*r**2)),
[0.15, 0.702],
1.0e-10
)
print(val)
which gives the more accurate 0.961715309492, or even tanh-sinh quadrature
val, error_estimate = quadpy.line_segment.tanh_sinh(
lambda r: 0.5108/r**2 / np.sqrt(2*1.158 + 2/r - 0.5108**2/(2*r**2)),
0.15, 0.702,
1.0e-30
)
print(val)
which gives 0.9617153094932353183036398697528.
There are a couple of issues here.
First one is that the third argument in np.arrange is not the number of values to be generated but the step. This means that theta will have only one value and that n and thus intg will have 10 instead of 100 values.
Assuming that was your intention (100 values) you can do this
intg =[]
v = lambda r: (0.5108/(r**2))* (1./np.sqrt(2*1.158+(2/r)-.5108**2/(2*r**2)))
n = np.arange(1,1000,10)
theta = np.arange(0,2*np.pi,2*np.pi/100)
#print theta
for j in n:
numerical = trapezoidal(v, .16,.702 , j)
intg.append(numerical)
Then you're plotting numerical which is basically a single number and what you probably wanted to plot was the integral value intg - to do so you also need to convert intg from a list into np.array:
intg = np.array(intg)
With these changes the program works as intended,
plt.plot(intg,theta)
plt.show()
If you print the lengths of your numerical and theta, you will see that they are empty lists/arrays.
Try the following:
import numpy as np
import matplotlib.pyplot as plt
def trapezoidal(f, a, b, n):
h = float(b-a)/n
result = 0.5*f(a) + 0.5*f(b)
for i in range(1, n):
result += f(a + i*h)
result *= h
return result
intg =[]
v = lambda r: (0.5108/(r**2))* (1./np.sqrt(2*1.158+(2/r)-.5108**2 /(2*r**2)))
n = np.arange(1, 1001)
theta = np.linspace(0,2.*np.pi,1000)
for j in n:
numerical = trapezoidal(v, .16,.702 , j)
intg.append(numerical)
plt.plot(intg,theta)
plt.show()
I have created a vector (v) and would like to perform the rotMatrix function on it. I cannot figure out how to call the function rotMatrix with a degree of 30 on the vector (v). I am also plotting the vectors.
Here is my code:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("white")
import math
def rotMatrix(angle):
return np.array([[np.cos(np.degrees(angle)), np.arcsin(np.degrees(angle))], [np.sin(np.degrees(angle)), np.cos(np.degrees(angle))]])
v = np.array([3,7])
v30 = rotMatrix(np.degrees(30)).dot(v)
plt.arrow(0,0,v[0],v[1], head_width=0.8, head_length=0.8)
plt.arrow(0,0,v30[0],v30[1],head_width=0.8, head_length=0.8)
plt.axis([-5,5,0,10])
plt.show()
In your rotMatrix function you have used the arcsin() function. You want to use -sin() You should also convert your degrees value to radians
return np.array([[np.cos(np.radians(angle)),
-np.sin(np.radians(angle))],
[np.sin(np.radians(angle)),
np.cos(np.radians(angle))]])
Or slightly improve efficiently and readability by
c = np.cos(np.radians(angle))
s = np.sin(np.radians(angle))
return np.array([[c, -s], [s, c]])
and the call with
rotMatrix(30).dot(v)
-sin and arcsin are very different.
When in doubt, play around with the calculations in an interactive Python/numpy session.
In [23]: 30/180*np.pi # do it yourself convsion - easy
Out[23]: 0.5235987755982988
In [24]: np.radians(30) # degrees to radians - samething
Out[24]: 0.52359877559829882
In [25]: np.sin(np.radians(30)) # sin(30deg)
Out[25]: 0.49999999999999994
enter image description here import numpy as np
import matplotlib.pyplot as plt
A = np.array([[3],[-3]])
n = np.linspace(0,2*np.pi,100)
def rotate_me(A,n):
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols = 2, figsize=(18, 16))
buf1 = []
buf11 = []
buf12 = []
buf13 = []
buf1p = []
buf2 = []
# t = []
for theta in n:
x=2
x1=3
x2=2
x3=3
# xp=3
# theta = 1/p
origin = [0],[0]
R = np.array([[x*np.cos(theta),-np.sin(theta)],
[np.sin(theta),np.cos(theta)]])
R1 = np.array([[np.cos(theta),-np.sin(theta)],
[np.sin(theta),np.cos(theta)*x1]])
R2 = np.array([[np.cos(theta),-np.sin(theta)],
[x2*np.sin(theta),np.cos(theta)]])
R3 = np.array([[np.cos(theta),-np.sin(theta)*x3],
[np.sin(theta),np.cos(theta)]])
Rp = np.array([[np.cos(theta),-np.sin(theta)],
[np.sin(theta),np.cos(theta)]])
V = R.dot(A)
V1 = R1.dot(A)
V2 = R2.dot(A)
V3 = R3.dot(A)
Vp = Rp.dot(A)
buf1.append(np.linalg.norm(V))
buf11.append(np.linalg.norm(V1))
buf12.append(np.linalg.norm(V2))
buf13.append(np.linalg.norm(V3))
buf1p.append(np.linalg.norm(Vp))
# buf2.append(np.linalg.norm(A))
ax1.quiver(*origin,V[0,:],V[1,:], scale=21,color ='r',label='cos')
ax1.quiver(*origin,V1[0,:],V1[1,:], scale=21,color ='g',label='cos')
ax1.quiver(*origin,V2[0,:],V2[1,:], scale=21,color ='m',label='sin')
ax1.quiver(*origin,V3[0,:],V3[1,:], scale=21,color ='b',label='-sin')
ax1.quiver(*origin,Vp[0,:],Vp[1,:], scale=21,color ='k',label='pure')
# print(theta)
# ax1.legend()
ax2.plot(n,buf1,color ='r')
ax2.plot(n,buf11,color ='g')
ax2.plot(n,buf12,color ='m')
ax2.plot(n,buf13,color ='b')
ax2.plot(n,buf1p,color ='k')
# ax2.plot(n,buf2,color ='b')
ax2.set_xlabel('angle')
ax2.set_ylabel('magnitude')
plt.show()
# return buf1,buf2
rotate_me(A,n)