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)
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'm trying to plot the graphs of the following equation in Python.
Solution of the radial differential equation of a 2d quantum ring
The beta parameter is
This was my attempt
import numpy as np
from scipy.special import gamma, genlaguerre
import matplotlib.pyplot as plt
from scipy import exp, sqrt
m = 0.067*9.1*10E-31
R = 5E-9
r = np.linspace(0, 20E-9)
#Definição do parâmetro beta
def beta(gama):
flux = np.linspace(0,1.0)
beta = sqrt((m-flux)**2+(gama**4)/4)
return beta
def Rn(n,gama):
raiz = sqrt((gamma(n+1)/((2**beta(gama)) * gamma(n+beta(gama)+1))))
eval_g = genlaguerre((n,beta(gama)),((gama * r/R)**2/2))
exp_g = exp(-((gama * r/R)**2)/4)
return (1/R) * raiz * (gama * r/R)**beta(gama) * exp_g * eval_g
sol1 = Rn(0,1.5)
sol2 = Rn(0,2.0)
sol3 = Rn(0,2.5)
sol4 = Rn(0,3.0)
fig, ax = plt.subplots()
ax.plot(r/R, sol1, color = 'red', label = '$\gamma$ = 1.5')
ax.plot(r/R, sol2, color = 'green', label = '$\gamma$ = 2.0')
ax.plot(r/R, sol3, color = 'blue', label = '$\gamma$ = 2.5')
ax.plot(r/R, sol4, color = 'black', label = '$\gamma$ = 3.0')
ax.legend()
ax.set_xlabel('R/r')
ax.set_ylabel('$R_0(r)$')
erro using genlaguerre
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Here the link to the article
I'm not into this topic, but afaics there are at least the following mistakes (I assume setting silently n=0 was on purpose):
In opposite to the posted image of the beta function, you added a division by 4 into your function definition.
Correct version:
def beta(gama):
return np.sqrt((m-flux)**2+(gama**4))
That's no reason for distributed instead of fixed peaks but I tell what I see here.
Product instead of division because of lacking parantheses in definition of amplitude function:
Correct version:
def amplitude(gama):
return np.sqrt(gamma(1)/((2**beta(gama)*gamma(beta(gama)+1))))
In the definition of Rn function there's the introducing 1/R missing.
However, all this unfortunately does not change the peaks to happen at equal x positions...
Sorry! The radial wave function of the article is wrong! I made the calculations and found the correct answer. The following code is correct
import numpy as np
from scipy.special import genlaguerre, gamma
import numpy as np
import matplotlib.pyplot as plt
m = 0 # magnetic number
flux = 0 # Phi in eqn 8
R = 5 # nm
r = np.linspace(0, 6 * R)
rho = r / R
def R0(n, gama):
beta = np.sqrt((m - flux)**2 + gama**4/4)
return (gama/R*
np.sqrt(gamma(n+ 1) / ( 2**beta * gamma(n + beta + 1))) *
(gama * rho)**beta *
np.exp(-gama**2 * rho**2 / 4) *
genlaguerre(n, beta)(gama**2 * rho**2 / 2))
sol1 = R0(0, 1.5)
sol2 = R0(0, 2.0)
sol3 = R0(0, 2.5)
sol4 = R0(0, 3.0)
plt.plot(rho, sol1, rho, sol2, rho, sol3, rho, sol4)
plt.legend(['$\gamma = 1.5$', '$\gamma = 2$', '$\gamma = 2.5$', '$\gamma = 3$'])
plt.ylabel('$R_{0}(r)$')
plt.xlabel('$r/R$')
plt.show()
I am trying to find the fundamental TE mode of the dielectric waveguide. The way I try to solve it is to compute two function and try to find their intersection on graph. However, I am having trouble get the intersect point on the plot.
My code:
def LHS(w):
theta = 2*np.pi*1.455*10*10**(-6)*np.cos(np.deg2rad(w))/(900*10**(-9))
if(theta>(np.pi/2) or theta < 0):
y1 = 0
else:
y1 = np.tan(theta)
return y1
def RHS(w):
y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
return y
x = np.linspace(80, 90, 500)
LHS_vals = [LHS(v) for v in x]
RHS_vals = [RHS(v) for v in x]
# plot
fig, ax = plt.subplots(1, 1, figsize=(6,3))
ax.plot(x,LHS_vals)
ax.plot(x,RHS_vals)
ax.legend(['LHS','RHS'],loc='center left', bbox_to_anchor=(1, 0.5))
plt.ylim(0,20)
plt.xlabel('degree')
plt.ylabel('magnitude')
plt.show()
I got plot like this:
The intersect point is around 89 degree, however, I am having trouble to compute the exact value of x. I have tried fsolve, solve to find the solution but still in vain. It seems not able to print out solution if it is not the only solution. Is it possible to only find solution that x is in certain range? Could someone give me any suggestion here? Thanks!
edit:
the equation is like this (m=0):
and I am trying to solve theta here by finding the intersection point
edit:
One of the way I tried is as this:
from scipy.optimize import fsolve
def f(wy):
w, y = wy
z = np.array([y - LHS(w), y - RHS(w)])
return z
fsolve(f,[85, 90])
However it gives me the wrong answer.
I also tried something like this:
import matplotlib.pyplot as plt
x = np.arange(85, 90, 0.1)
f = LHS(x)
g = RHS(x)
plt.plot(x, f, '-')
plt.plot(x, g, '-')
idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
print(x[idx])
plt.show()
But it shows:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
First, you need to make sure that the function can actually handle numpy arrays. Several options for defining piecewise functions are shown in
Plot Discrete Distribution using np.linspace(). E.g.
def LHS(w):
theta = 2*np.pi*1.455*10e-6*np.cos(np.deg2rad(w))/(900e-9)
y1 = np.select([theta < 0, theta <= np.pi/2, theta>np.pi/2], [np.nan, np.tan(theta), np.nan])
return y1
This already allows to use the second approach, plotting a point at the index which is closest to the minimum of the difference between the two curves.
import numpy as np
import matplotlib.pyplot as plt
def LHS(w):
theta = 2*np.pi*1.455*10e-6*np.cos(np.deg2rad(w))/(900e-9)
y1 = np.select([theta < 0, theta <= np.pi/2, theta>np.pi/2], [np.nan, np.tan(theta), np.nan])
return y1
def RHS(w):
y = ((np.sin(np.deg2rad(w)))**2-(1.440/1.455)**2)**0.5/np.cos(np.deg2rad(w))
return y
x = np.linspace(82.1, 89.8, 5000)
f = LHS(x)
g = RHS(x)
plt.plot(x, f, '-')
plt.plot(x, g, '-')
idx = np.argwhere(np.diff(np.sign(f - g)) != 0).reshape(-1) + 0
plt.plot(x[idx], f[idx], 'ro')
plt.ylim(0,40)
plt.show()
One may then also use scipy.optimize.fsolve to get the actual solution.
idx = np.argwhere(np.diff(np.sign(f - g)) != 0)[-1]
from scipy.optimize import fsolve
h = lambda x: LHS(x)-RHS(x)
sol = fsolve(h,x[idx])
plt.plot(sol, LHS(sol), 'ro')
plt.ylim(0,40)
plt.show()
Something quick and (very) dirty that seems to work (at least it gave theta value of ~89 for your parameters) - add the following to your code before the figure, after RHS_vals = [RHS(v) for v in x] line:
# build a list of differences between the values of RHS and LHS
diff = [abs(r_val- l_val) for r_val, l_val in zip(RHS_vals, LHS_vals)]
# find the minimum of absolute values of the differences
# find the index of this minimum difference, then find at which angle it occured
min_diff = min(diff)
print "Minimum difference {}".format(min_diff)
print "Theta = {}".format(x[diff.index(min_diff)])
I looked in range of 85-90:
x = np.linspace(85, 90, 500)
So far I have managed to find the particular solution to this equation for any given mass and drag coefficient. I have not however found a way to plot the solution or even evaluate the solution for a specific point. I really want to find a way to plot the solution.
from sympy import *
m = float(raw_input('Mass:\n> '))
g = 9.8
k = float(raw_input('Drag Coefficient:\n> '))
f = Function('f')
f1 = g * m
t = Symbol('t')
v = Function('v')
equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0)
C1 = Symbol('C1')
C1_ic = solve(equation.rhs.subs({t:0}),C1)[0]
equation = equation.subs({C1:C1_ic})
For completeness, you may also use Sympy's plot, which is probably more convenient if you want a "quick and dirty" plot.
plot(equation.rhs,(t,0,10))
Import these libraries (seaborn just makes the plots pretty).
from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
Then tack this onto the end. This will plot time, t, against velocity, v(t).
# make a numpy-ready function from the sympy results
func = lambdify(t, equation.rhs,'numpy')
xvals = np.arange(0,10,.1)
yvals = func(xvals)
# make figure
fig, ax = plt.subplots(1,1,subplot_kw=dict(aspect='equal'))
ax.plot(xvals, yvals)
ax.set_xlabel('t')
ax.set_ylabel('v(t)')
plt.show()
I get a plot like this for a mass of 2 and a drag coefficient of 2.
If I've understood correctly, you want to represent the right hand side of your solution, here's one of the multiple ways to do it:
from sympy import *
import numpy as np
import matplotlib.pyplot as plt
m = float(raw_input('Mass:\n> '))
g = 9.8
k = float(raw_input('Drag Coefficient:\n> '))
f = Function('f')
f1 = g * m
t = Symbol('t')
v = Function('v')
equation = dsolve(f1 - k * v(t) - m * Derivative(v(t)), 0)
C1 = Symbol('C1')
C1_ic = solve(equation.rhs.subs({t: 0}), C1)[0]
equation = equation.subs({C1: C1_ic})
t1 = np.arange(0.0, 50.0, 0.1)
y1 = [equation.subs({t: tt}).rhs for tt in t1]
plt.figure(1)
plt.plot(t1, y1)
plt.show()
I am trying to map surface curvature (mean, gaussian and principle curvature) values to surface faces. I have computed the curvature values for an artificially generated 3D surface (eg. cylinder). The resulting 3D surface that I am trying to get is something like this mean curvature mapped to surface. Can somebody guide me in how to get this?
The code for the surface I am creating is:
import math
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xindex = []
yindex = []
zindex = []
x = []
y = []
z = []
count = 1
surfaceSt = []
import numpy
numpy.set_printoptions(threshold=numpy.nan)
#surfaceStX = numpy.empty((10,36))
#surfaceStY = numpy.empty((10,36))
#surfaceStZ = numpy.empty((10,36))
surfaceStZ = []
surfaceStX = []
surfaceStY = []
for i in range(1,21):
if i < 11:
x = []
y = []
z = []
pt = []
ptX = []
ptY = []
ptZ = []
for t in range(0,360,10):
x = i*math.sin(math.radians(t))
y = i*math.cos(math.radians(t))
z = i-1
ptX.append(x)
ptY.append(y)
ptZ.append(z)
pt.append([x,y,z])
ptX.append(ptX[0])
ptY.append(ptY[0])
ptZ.append(ptZ[0])
surfaceStX.append(ptX)
surfaceStY.append(ptY)
surfaceStZ.append(ptZ)
# numpy.append(surfaceStX,ptX)
# numpy.append(surfaceStY,ptY)
# numpy.append(surfaceStZ,ptZ)
#ax.scatter(x,y,z)
elif i >= 11:
x = []
y = []
z = []
pt = []
ptX = []
ptY = []
ptZ = []
for t in range(0,360,10):
x = (i-count)*math.sin(math.radians(t))
y = (i-count)*math.cos(math.radians(t))
z = i-1
ptX.append(x)
ptY.append(y)
ptZ.append(z)
pt.append([x,y,z])
ptX.append(ptX[0])
ptY.append(ptY[0])
ptZ.append(ptZ[0])
surfaceStX.append(ptX)
surfaceStY.append(ptY)
surfaceStZ.append(ptZ)
count +=2
X = numpy.array(surfaceStX)
Y = numpy.array(surfaceStY)
Z = numpy.array(surfaceStZ)
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,shade = 'True' )
from surfaceCurvature import surface_curvature
Pcurvature,Gcurvature,Mcurvature = surface_curvature(X,Y,Z)
plt.show()
My surface curvature computation is given below (courtesy: https://github.com/sujithTSR/surface-curvature):
def surface_curvature(X,Y,Z):
(lr,lb)=X.shape
#print lr
#print "awfshss-------------"
#print lb
#First Derivatives
Xv,Xu=np.gradient(X)
Yv,Yu=np.gradient(Y)
Zv,Zu=np.gradient(Z)
#Second Derivatives
Xuv,Xuu=np.gradient(Xu)
Yuv,Yuu=np.gradient(Yu)
Zuv,Zuu=np.gradient(Zu)
Xvv,Xuv=np.gradient(Xv)
Yvv,Yuv=np.gradient(Yv)
Zvv,Zuv=np.gradient(Zv)
#2D to 1D conversion
#Reshape to 1D vectors
Xu=np.reshape(Xu,lr*lb)
Yu=np.reshape(Yu,lr*lb)
Zu=np.reshape(Zu,lr*lb)
Xv=np.reshape(Xv,lr*lb)
Yv=np.reshape(Yv,lr*lb)
Zv=np.reshape(Zv,lr*lb)
Xuu=np.reshape(Xuu,lr*lb)
Yuu=np.reshape(Yuu,lr*lb)
Zuu=np.reshape(Zuu,lr*lb)
Xuv=np.reshape(Xuv,lr*lb)
Yuv=np.reshape(Yuv,lr*lb)
Zuv=np.reshape(Zuv,lr*lb)
Xvv=np.reshape(Xvv,lr*lb)
Yvv=np.reshape(Yvv,lr*lb)
Zvv=np.reshape(Zvv,lr*lb)
Xu=np.c_[Xu, Yu, Zu]
Xv=np.c_[Xv, Yv, Zv]
Xuu=np.c_[Xuu, Yuu, Zuu]
Xuv=np.c_[Xuv, Yuv, Zuv]
Xvv=np.c_[Xvv, Yvv, Zvv]
# First fundamental Coeffecients of the surface (E,F,G)
E=np.einsum('ij,ij->i', Xu, Xu)
F=np.einsum('ij,ij->i', Xu, Xv)
G=np.einsum('ij,ij->i', Xv, Xv)
m=np.cross(Xu,Xv,axisa=1, axisb=1)
p=np.sqrt(np.einsum('ij,ij->i', m, m))
n=m/np.c_[p,p,p]
# Second fundamental Coeffecients of the surface (L,M,N), (e,f,g)
L= np.einsum('ij,ij->i', Xuu, n) #e
M= np.einsum('ij,ij->i', Xuv, n) #f
N= np.einsum('ij,ij->i', Xvv, n) #g
# Gaussian Curvature
K=(L*N-M**2)/(E*G-F**2)
K=np.reshape(K,lr*lb)
# Mean Curvature
H = (E*N + G*L - 2*F*M)/((E*G - F**2))
H = np.reshape(H,lr*lb)
# Principle Curvatures
Pmax = H + np.sqrt(H**2 - K)
Pmin = H - np.sqrt(H**2 - K)
#[Pmax, Pmin]
Principle = [Pmax,Pmin]
return Principle,K,H
EDIT 1:
I tried a few things based on the link provided by armatita. Following is my code:
'''
Creat half cylinder
'''
import numpy
import matplotlib.pyplot as plt
import math
ptX= []
ptY = []
ptZ = []
ptX1 = []
ptY1 = []
ptZ1 = []
for i in range(0,10):
x = []
y = []
z = []
for t in range(0,200,20):
x.append(10*math.cos(math.radians(t)))
y.append(10*math.sin(math.radians(t)))
z.append(i)
x1= 5*math.cos(math.radians(t))
y1 = 5*math.sin(math.radians(t))
z1 = i
ptX1.append(x1)
ptY1.append(y1)
ptZ1.append(z1)
ptX.append(x)
ptY.append(y)
ptZ.append(z)
X = numpy.array(ptX)
Y = numpy.array(ptY)
Z = numpy.array(ptZ)
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
from surfaceCurvature import surface_curvature
p,g,m= surface_curvature(X,Y,Z)
n = numpy.reshape(m,numpy.shape(X))
ax.plot_surface(X,Y,Z, rstride=1, cstride=1)
plt.show()
'''
Map mean curvature to color
'''
import numpy as np
X1 = X.ravel()
Y1 = Y.ravel()
Z1 = Z.ravel()
from scipy.interpolate import RectBivariateSpline
# Define the points at the centers of the faces:
y_coords, x_coords = np.unique(Y1), np.unique(X1)
y_centers, x_centers = [ arr[:-1] + np.diff(arr)/2 for arr in (y_coords, x_coords)]
# Convert back to a 2D grid, required for plot_surface:
#Y1 = Y.reshape(y_coords.size, -1)
#X1 = X.reshape(-1, x_coords.size)
#Z1 = Z.reshape(X.shape)
C = m.reshape(X.shape)
C -= C.min()
C /= C.max()
interp_func = RectBivariateSpline(x_coords, y_coords, C.T, kx=1, ky=1)
I get the following error:
raise TypeError('y dimension of z must have same number of y')
TypeError: y dimension of z must have same number of elements as y
All the dimensions are same. Can anybody tell what's going wrong with my implementation?
I think you need to figure out exactly what you need. Looking at your code I notice you are producing variables that have no use. Also you seem to have a function to calculate the surface curvature but than you try to make some calculations using the np.unique function for which I cannot see the purpose here (and that is why that error appears).
So let's assume this:
You have a function that returns the curvature value for each cell.
You have the X,Y and Z meshes to plot that surface.
Using your code, and assuming you m variable is the curvature (again this is in your code), if I do this:
import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import math
# Here would be the surface_curvature function
X = numpy.array(ptX)
Y = numpy.array(ptY)
Z = numpy.array(ptZ)
p,g,m= surface_curvature(X,Y,Z)
C = m.reshape(X.shape)
C -= C.min()
C /= C.max()
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
n = numpy.reshape(m,numpy.shape(X))
ax.plot_surface(X,Y,Z,facecolors = cm.jet(C), rstride=1, cstride=1)
plt.show()
, I obtain this:
Which is a value mapped to color in a matplotlib surface. If that C you've built is not the actual curvature you need to replace it by the one that is.