Plotting a curve that is fed by a function - python

import numpy as np
import scipy.stats as stats
import math
import ipywidgets as widgets
from ipywidgets import interactive
import seaborn as sns
import matplotlib.pyplot as plt
mu_0 = 50
mu_1 = mu_0*1.1
#mu_2 = mu_0*1.5
n= 3
sigma=4.32/math.sqrt(n)
horizontal_values=np.linspace(55, 75, num=101)
def critical_value(mu_1,sigma, alpha=0.04):
c=stats.norm.ppf(1-alpha,mu_0,sigma)
return c
c= critical_value(mu_1,sigma)
power = stats.norm.sf(c,mu_1,sigma)
print (power)
print(c)
Hello,
I need to plot a graph from these data: so when you enter different mu_0 you get different powers
I need to enter every element in that array(horizontal values) to that function(the one that calculates the power so we can see the power in accordance to the speed)
And after that I want to draw a curve accordingly.
TLDR I want to change mu_0 between 55 and 75 and use the results to draw a graph. However I dont know how to go about it.

I think this is what you are looking for.
import numpy as np
import scipy.stats as stats
import math
import ipywidgets as widgets
from ipywidgets import interactive
import seaborn as sns
import matplotlib.pyplot as plt
def critical_value(mu_1,sigma, alpha=0.04):
c=stats.norm.ppf(1-alpha,mu_0,sigma)
return c
def func(mu_0): # function for calculating power
mu_1 = mu_0*1.1
#mu_2 = mu_0*1.5
n = 3
sigma=4.32/math.sqrt(n)
c = critical_value(mu_1,sigma)
power = stats.norm.sf(c,mu_1,sigma)
return power
horizontal_values=np.linspace(55, 75, num=101)
power = [func(mu) for mu in horizontal_values] # calculates power for different mu_0
plt.plot(horizontal_values, power) # plot
plt.xlabel('mu')
plt.ylabel('Power')
plt.show()

Related

How to use loop and range function in python to visualize plot?

Im trying to understand How to use loop and range function in python( matplotlib package ) to visualize the cdf of the Poisson distribution in a single density plot??
𝜆 = 3,4,5,6,7
import numpy as np
import matplotlib.pyplot as plt
plt.gcf().set_size_inches(12,6)
for l in range(3,8,1):
seq = np.arrange(0,20)
How can i build the rest of the code ?
import numpy as np
You define the range of values to calculate the cdf outside the loop, and for every iteration, you have the values as x, and the cdf as y:
import matplotlib.pyplot as plt
from scipy.stats import poisson
Seq = np.arange(0,20)
plt.gcf().set_size_inches(12,6)
for l in range(3,8,1):
plt.plot(Seq,poisson.cdf(Seq,l),label=l)
plt.legend(loc="lower right")

How to plot the orbit which spirals into centre of orbit by using loops?

Hi i have written some codes when i try to plot the orbit,b=beta=3,G=1, i cant get the orbit which spirals into the centre by using loops it shows an incomplete orbit.
below is the code with b=beta=3 and G=1 :
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def dbydt(t,v):
x,y,vx,vy=v
rsq=x**2+y**2
G=1
b=3
beta=3
return vx,vy,-G*x/rsq**((b+1)/2),-G*y/rsq**((beta+1)/2)
result=solve_ivp(dbydt,[0,2],[1,0,0,1],t_eval=np.linspace(0,2,100))
plt.scatter(result.y[0],result.y[1])
plt.scatter(0,0,color='yellow',s=200)
plt.show()
If you increase your final time to more than 2, eg. 25 in this case:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def dbydt(t,v):
x,y,vx,vy=v
rsq=x**2+y**2
G=1
b=3
beta=3
return vx,vy,-G*x/rsq**((b+1)/2),-G*y/rsq**((beta+1)/2)
t_final = 25
result=solve_ivp(dbydt,[0,t_final],[1,0,0,1],t_eval=np.linspace(0,t_final,512))
plt.scatter(result.y[0],result.y[1])
plt.scatter(0,0,color='yellow',s=200)
plt.show()

matplotlib Boxplot drawer function stats mean

Boxplot drawer function at https://matplotlib.org/gallery/statistics/bxp.html has the following example:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
# fake data
np.random.seed(19680801)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list('ABCD')
# compute the boxplot stats
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
for n in range(len(stats)):
stats[n]['med'] = np.median(data)
stats[n]['mean'] *= 2
print(list(stats[0]))
There is a line of code stats[n]['mean'] *= 2 within the for loop that I can't understand. Is it wrong or does it mean something?

How to use numpy to build a 3D-model?

Original(2018.11.01)
I have 3 numpy:x、y、z,created by my laser scanner(40 degree / 1 step).
I want to used them to build a 3D model.
I think it must should be use matplotlib.tri
But I have no idea to decide triangulated data
Here is my data :https://www.dropbox.com/s/d9p62kv9jcq9bwh/xyz.zip?dl=0
And Original model:https://i.imgur.com/XSyONff.jpg
Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
tri = #I have no idea...
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(x_all,y_all,z_all,triangles=tri.triangles)
Thank so much.
Update(2018.11.02)
I try this way to decide triangulated data
Delaunay Triangulation of points from 2D surface in 3D with python?
code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from stl import mesh
x_all=np.load("x.npy")
y_all=np.load("y.npy")
z_all=np.load("z.npy")
model=np.vstack((x_all,y_all,z_all))
model=np.transpose(model)
model -= model.mean(axis=0)
rad = np.linalg.norm(model, axis=1)
zen = np.arccos(model[:,-1] / rad)
azi = np.arctan2(model[:,1], model[:,0])
tris = mtri.Triangulation(zen, azi)
plt.show()
And my model looks like:
https://i.stack.imgur.com/KVPHP.png
https://i.stack.imgur.com/LLQsQ.png
https://i.stack.imgur.com/HdzFm.png
Even though it has better surface on it,but there is a big hole over my model.Any idea to fixs it?
Assuming you want to reduce the complexity, i.e find triangles in your files to reduce the complexity. You may look into fitting a convex hull to your points, see here fore more info
Based on the file you provided this produces a surf plot of the object.
from numpy import load, stack
from matplotlib.pyplot import subplots
from mpl_toolkits.mplot3d import Axes3D
from scipy import spatial
x = load("x.npy")
y = load("y.npy")
z = load("z.npy")
points = stack((x,y,z), axis = -1)
v = spatial.ConvexHull(points)
fig, ax = subplots(subplot_kw = dict(projection = '3d'))
ax.plot_trisurf(*v.points.T, triangles = v.simplices.T)
fig.show()

How do you quantize a simple input using python

I am using the below codes to quantise the input signal for quantisation interval of 0.5 and this should give me staircase signal.The algorithm used here is same as used in Simulink.Could any one help me plot the quantised signal.
import numpy as np
import matplotlib.pyplot as plt
for i in range(0,10):
q=0.5;
x=q*np.round(i/q);
plt.plot(i,x)
plt.xlim([0,10])
plt.ylim([0,10])
plt.hold()
plt.grid()
plt.show()
Do you mean something like this?
import numpy as np
import matplotlib.pyplot as plt
q = 0.5
x = np.linspace(0, 10, 1000)
y = q * np.round(x/q)
plt.plot(x,y)

Categories