I am doing some analysis on some simple data, and I am trying to plot auto-correlation and partial auto-correlation. Using these plots, I am trying to find the P and Q value to plot in my ARIMA model.
I can see on the graphs, but I am wondering if I can explicitly find, for each graph, where the plot crosses the axhline?
plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
So in the above code, can I find, and show, where the lag_pacf plot crosses the axhlines that I have predetermined?
Thanks
You'll need to calculate the intersections between the line segments of lag_pacf and y's:
from matplotlib import pyplot as plt
import numpy as np
lag_pacf = np.random.randint(-10,10,30)
log_moving_average_difference = [i for i in range(30)]
#plt.subplot(122)
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1]) #Typo was here
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
return None
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y
def near(a, b, rtol=1e-5, atol=1e-8):
return abs(a - b) < (atol + rtol * abs(b))
def crosses(line1, line2):
"""
Return True if line segment line1 intersects line segment line2 and
line1 and line2 are not parallel.
"""
(x1,y1), (x2,y2) = line1
(u1,v1), (u2,v2) = line2
(a,b), (c,d) = (x2-x1, u1-u2), (y2-y1, v1-v2)
e, f = u1-x1, v1-y1
denom = float(a*d - b*c)
if near(denom, 0):
# parallel
return False
else:
t = (e*d - b*f)/denom
s = (a*f - e*c)/denom
# When 0<=t<=1 and 0<=s<=1 the point of intersection occurs within the
# line segments
return 0<=t<=1 and 0<=s<=1
plt.plot(lag_pacf)
plt.axhline(y=0, linestyle = '--', color = 'grey')
plt.axhline(y=-1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--',color = 'red')
plt.axhline(y=1.96/np.sqrt(len(log_moving_average_difference)),linestyle = '--', color = 'green')
plt.title('Partial Autocorelation Function')
yys = [0,-1.96/np.sqrt(len(log_moving_average_difference)),1.96/np.sqrt(len(log_moving_average_difference))]
xx, yy = [],[]
xo,yo = [k for k in range(30)],lag_pacf
d = 20
for i in range(1,len(lag_pacf)):
for k in yys:
p1 = np.array([xo[i-1],yo[i-1]],dtype='float')
p2 = np.array([xo[i],yo[i]],dtype='float')
k1 = np.array([xo[i-1],k],dtype='float')
k2 = np.array([xo[i],k],dtype='float')
if crosses((p2,p1),(k1,k2)):
seg = line_intersection((p2,p1),(k1,k2))
if seg is not None:
xx.append(seg[0])
yy.append(seg[1]-d)
plt.scatter(seg[0],seg[1],c='red')
plt.xlim(0,30)
plt.ylim(-10,10)
plt.show()
, for this completely randomized example:
I obtained this:
Related
I want to add text on 3d boxes but i cant see them, when i rotate camera, i can see but, it looks like remains inside of box. how can see show them front of box? should i make boxes transparent or something?
you can see "li" on left.
but it should shown like this :
( stopped creation of box when create 1st)
summary is:
xz_sizes = np.array(boyutlar)
xz_sizes[:,1] = 0
print(xz_sizes)
label_pos = (np.array(pozisyon) + xz_sizes/2 ).tolist()
labels = ["limegreen","blue","1","1","1","1","1","1","1"]
# labels = ['12', '24']
for pos, label in zip(label_pos, labels):
axGlob.text( *pos, label, 'x', ha='center', va='center')
counter2 += 2
if counter2 == 100:
break ```
Fullcode=class Painter:
def __init__(self,bins):
self.items = bins.items
self.width = bins.width
self.height = bins.height
self.depth = bins.depth
def _plotCube(self, ax, x, y, z, dx, dy, dz, color='red',mode=2):
# stoppause = 5
""" Auxiliary function to plot a cube. code taken somewhere from the web. """
xx = [x, x, x+dx, x+dx, x]
yy = [y, y+dy, y+dy, y, y]
kwargs = {'alpha': 1, 'color': color,'linewidth':1 }
if mode == 1 :
ax.plot3D(xx, yy, [z]*5, **kwargs)
ax.plot3D(xx, yy, [z+dz]*5, **kwargs)
ax.plot3D([x, x], [y, y], [z, z+dz], **kwargs)
ax.plot3D([x, x], [y+dy, y+dy], [z, z+dz], **kwargs)
ax.plot3D([x+dx, x+dx], [y+dy, y+dy], [z, z+dz], **kwargs)
ax.plot3D([x+dx, x+dx], [y, y], [z, z+dz], **kwargs)
else :
text='sadsadas'
p = Rectangle((x,y),dx,dy,fc=color,ec='black')
p2 = Rectangle((x,y),dx,dy,fc=color,ec='black',label = 'adad')
p3 = Rectangle((y,z),dy,dz,fc=color,ec='black')
p4 = Rectangle((y,z),dy,dz,fc=color,ec='black')
p5 = Rectangle((x,z),dx,dz,fc=color,ec='black')
p6 = Rectangle((x,z),dx,dz,fc=color,ec='black')
ax.add_patch(p)
# ax.annotate(text, (x+dx/2,y))
ax.add_patch(p2)
# ax.annotate(text, (x+dx/2,y))
ax.add_patch(p3)
ax.add_patch(p4)
ax.add_patch(p5)
ax.add_patch(p6)
# ax.text(x+dx, y+dy/2, z, "red", color='red',rotation_mode= None,verticalalignment = 'top',animated = 'b')
# ax.text
ax.annotate(text, (x,y))
art3d.pathpatch_2d_to_3d(p, z=z, zdir="z")
# art3d.text(x+0.8,y+0.8,z+0.8, 'label', zdir="x")
art3d.pathpatch_2d_to_3d(p2, z=z+dz, zdir="z")
art3d.pathpatch_2d_to_3d(p3, z=x, zdir="x")
art3d.pathpatch_2d_to_3d(p4, z=x + dx, zdir="x")
art3d.pathpatch_2d_to_3d(p5, z=y, zdir="y")
art3d.pathpatch_2d_to_3d(p6, z=y + dy, zdir="y")
def plotBoxAndItems(self,title=""):
counter2 = 0
with open('boyutlar.csv', newline='') as f:
reader = csv.reader(f)
boyutlar = list(reader)
boyutlar = [e for e in boyutlar if e]
# boyutlar = [[float(value) for value in sublist] for sublist in boyutlar]
boyutlar = [list(map(float, y)) for y in boyutlar]
with open('pozisyon.csv', newline='') as f:
reader2 = csv.reader(f)
pozisyon = list(reader2)
pozisyon = [e for e in pozisyon if e]
# print(pozisyon)
# pozisyon = [[float(value) for value in sublist] for sublist in pozisyon]
pozisyon = [[float(value) for value in sublist] for sublist in pozisyon]
print(pozisyon)
""" side effective. Plot the Bin and the items it contains. """
fig = plt.figure()
axGlob = plt.axes(projection='3d')
stoppause = 1
counter=0
# fit rotation type
for item in self.items:
rt = item.rotation_type
x,y,z = item.position
[w,h,d] = item.getDimension()
color = item.color
# countt=countt+1
if item.typeof == 'cube':
# plot item of cube
self._plotCube(axGlob, float(x), float(y), float(z), float(w),float(h),float(d),color=color,mode=2)
elif item.typeof == 'cylinder':
# plot item of cylinder
self._plotCylinder(axGlob, float(x), float(y), float(z), float(w),float(h),float(d),color=color,mode=2)
counter = counter + 1
if counter == stoppause:
break
# plot bin
self._plotCube(axGlob,0, 0, 0, float(self.width), float(self.height), float(self.depth),color='black',mode=1)
plt.title('result')
self.setAxesEqual(axGlob)
xz_sizes = np.array(boyutlar)
xz_sizes[:,1] = 0
print(xz_sizes)
label_pos = (np.array(pozisyon) + xz_sizes/2 ).tolist()
labels = ["limegreen","blue","1","1","1","1","1","1","1"]
# labels = ['12', '24']
for pos, label in zip(label_pos, labels):
axGlob.text( *pos, label, 'x', ha='center', va='center')
counter2 += 1
if counter2 == 1:
break
plt.show()
I have a fits datacube in Python where I need to fit the spectra of each pixel in the cube. My code for fitting just one spectrum works great, but for some reason I can't figure out how to translate that into fitting each pixel (100 pixels total for a 10by10).
ngc2617 = fits.open('NGC2617.fits')[1]
cube2617 = SpectralCube.read(ngc2617)
data2617 = ngc2617.data
nz,ny,nx = np.shape(data2617)
obs_2617 = cube2617.spectral_axis
z_2617 = 0.014326 #simbad
rest_2617 = np.asarray(obs_2617 / (1+z_2617))
flux_2617 = data2617[:,172,142]
flux_2617[np.isnan(flux_2617)]=0
halpha_chans = np.asarray(np.where((rest_2617 > 6365) & (rest_2617 < 6715)))
halphawaves = np.asarray(rest_2617[halpha_chans]).T
halphaflux = np.asarray(flux_2617[halpha_chans]).T
cont_chans = np.asarray(np.where((rest_2617 > 5500) & (rest_2617 < 5510)))
cont_2617 = np.asarray(flux_2617[cont_chans]).T
x_halpha = halphawaves.flatten()
y_halpha = halphaflux.flatten()
c = 2.998 * 10**5 #km/s
plt.figure(figsize=[10,8])
plt.subplot(2,1,1)
plt.plot(x_halpha,y_halpha, '--', label = 'data')
def Gauss(x, A, x0, fwhm):
sigma = (fwhm * x0) / (2.355 * c)
return A * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
def func(x, A0, A1, x0, fwhm, fwhm1, cont):
param1 = cont
param2 = Gauss(x, A0, x0 - 14.75, fwhm) + Gauss(x, A1, x0, fwhm) + Gauss(x, A0*3, x0 + 20.66, fwhm) + Gauss(x, A1, x0, fwhm1)
return param1 + param2
cont_init = np.average(cont_2617)
A0_init = np.max(y_halpha[0:150])-cont_init
A1_init = np.max(y_halpha)-cont_init
x0_init = x_halpha[np.where(y_halpha==np.max(y_halpha))][0]
fwhm_init = (x_halpha[np.where(y_halpha==np.max(y_halpha))][0] - x_halpha[np.where((np.min(abs(np.max(y_halpha)/2 - y_halpha))))][0])*2
fwhm1_init = (x_halpha[np.where(y_halpha==np.max(y_halpha))][0] - x_halpha[np.where((np.min(abs(np.max(y_halpha)/2 - y_halpha[0:150]))))][0])*2
parameters, covariance = curve_fit(func, x_halpha, y_halpha, p0=[A0_init, A1_init, x0_init, fwhm_init, fwhm1_init, cont_init])
fit_y = func(x_halpha, *parameters)
fit1_NII = Gauss(x_halpha, A0_init, parameters[2]-14.75, parameters[3]) + parameters[5]
fit2_NII = Gauss(x_halpha, A0_init, parameters[2]+20.66, parameters[3]) + parameters[5]
fit_Halph = Gauss(x_halpha, A1_init, parameters[2], parameters[3]) + parameters[5]
plt.plot(x_halpha, fit_y, '-', label='full fit')
plt.plot(x_halpha, fit1_NII, '-', label='NII 6549.86 fit')
plt.plot(x_halpha, fit2_NII, '-', label='NII 6585.27 fit')
plt.plot(x_halpha, fit_Halph, '-', label='H\u03B1 fit')
plt.title('NGC2617 H\u03B1 & NII Spectra of Central Point with Gaussian Fit', fontsize = 15)
plt.xlabel('wavelength [$\AA$]', fontsize = 12)
plt.ylabel(r'flux [10$^{-20}$ $erg * s^{-1}$ * $cm^{-2}$ * $\AA^{-1}$]', fontsize = 12)
plt.grid(True)
plt.legend()
plt.subplot(2,1,2)
plt.plot(np.linspace(0,0,6800), color = 'r', label = '0 point')
plt.scatter(x_halpha, y_halpha-fit_y, marker = 'o', label='residuals')
plt.xlim(halphawaves[0],halphawaves[-1])
plt.xlabel('wavelength [$\AA$]', fontsize = 12)
plt.ylabel('residuals', fontsize = 12)
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
this is my code for the single spectrum...can anyone help me create a loop that will do this but for a full cube? Thanks!
I'm doing a free fall caluculations (really simple) and would like to plot each instance of height of the objects - that is the height of the object to be displayed as it 'falls' down. I tried running it throught a for loop, but i just get the end result plotted. What would i need to do to dislplay the object as it falls, for each individual - not just the end result.
Here is my code:
#Input parameters
y1 = 490 #starting position
y2 = 0 #ground
g = -9.81 #gravity
VY = 0 #starting speed
import math
import numpy as np
import matplotlib.pyplot as plt
sqrt_part = math.sqrt(VY**2-2*g*(y1-y2))
t1 = - VY - sqrt_part/g
t2 = - VY + sqrt_part/g
if t1 > 0:
t = t1
else:
t = t2
print('t = ' + str(t) + ' ' + 's')
t_space = np.linspace(0,t,50)
y_t = y1 + VY * t_space + 0.5 * g * t_space**2
v_t = abs(y_t[1:] - y_t[0:-1])/abs(t_space[0:-1] - t_space[1:])
plt.plot(t_space, y_t, 'go')
plt.plot(t_space[1:], v_t, 'r--')
for i in range(np.size(t_space)):
plt.plot(t_space[i], y_t[i], 'go')
The for loop displays the same as the plot above it, but i would like it to update and show the 'ro' as it moves thorught time. How would i do that?
On the left is what i get, on the right is what i want
enter image description here
Please, take a look at matplotlib animation api.
#Input parameters
y1 = 490 #starting position
y2 = 0 #ground
g = -9.81 #gravity
VY = 0 #starting speed
import math
import numpy as np
import matplotlib.pyplot as plt
sqrt_part = math.sqrt(VY**2-2*g*(y1-y2))
t1 = - VY - sqrt_part/g
t2 = - VY + sqrt_part/g
if t1 > 0:
t = t1
else:
t = t2
print('t = ' + str(t) + ' ' + 's')
t_space = np.linspace(0,t,50)
y_t = y1 + VY * t_space + 0.5 * g * t_space**2
v_t = np.abs((np.roll(y_t, -1) - y_t) / (np.roll(t_space, -1) - t_space))
v_t = np.roll(v_t, 1)
v_t[0] = 0
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
# create two empty lines
ln_y, = plt.plot([], [], 'go', label="y")
ln_v, = plt.plot([], [], 'r--', label="v")
def init():
ax.set_xlim(0, max(t_space))
ax.set_ylim(0, max(y_t))
ax.set_xlabel("t")
ax.legend()
return ln_y, ln_v
def update(i):
# i represents the index of the slice to use at the current frame
ln_y.set_data(t_space[:i], y_t[:i])
ln_v.set_data(t_space[:i], v_t[:i])
return ln_y, ln_v,
ani = FuncAnimation(fig, update, frames=range(len(v_t)),
init_func=init, blit=False, repeat=False)
plt.show()
I am currently working on a Yee Solver script for uni, but when I try to animate my 3D graph, the graph is not what is expected. It works for a 2D plot, but I can't seem to translate that into 3D. From my understanding, set_data and set_3d_properties need a 1D array to work, which I am inputting.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.widgets import Slider
# Program Variables
wv_lgth_num = 10
graph_type = '3d'
t = 0
# Physical Constants
c = 3e8
mu_r = 1
eps_r = 1
# Source Constants
f = 2e9
omega = 2*f*(np.pi)
amp = 1.0
wv_lgth = c/f
period = 1/f
# Step size
dz = wv_lgth/20
dt = ((c/f)/20)/c
#dt = ((1/f)/20)/1
# Axis Grids
z_grid = np.arange(0,wv_lgth_num*wv_lgth,dz)
t_grid = np.arange(0,10*period,dt)
# Number of steps
num_z = z_grid.size
num_t = t_grid.size
# Coefficients
coe_E = c*dt/(eps_r*dz)
coe_H = c*dt/(mu_r*dz)
# E and H Matricies
E_mat = np.zeros((num_z,num_t))
H_mat = np.zeros((num_z,num_t))
# Generating Values for E and H
for time in range(0,num_t-1):
for pos in range(0,num_z-1):
# Source Wave
if pos == 0:
H_mat[0,time] = amp*np.sin(omega*t_grid[time])
# All cases of Yee Solver
if pos == 1:
if time == 0:
H_mat[1,0] = 0
E_mat[0,0] = 0
else:
H_mat[1,time] = H_mat[1,time-1] + coe_H*(E_mat[1,time-1] - E_mat[0,time-1])
E_mat[0,time] = E_mat[0,time-1] + coe_E*(H_mat[1,time] - H_mat[0,time])
if pos > 1 and pos != num_z-1:
if time == 0:
H_mat[pos,0] = 0
E_mat[pos-1,0] = 0
if time > 0:
H_mat[pos,time] = H_mat[pos,time-1] + coe_H*(E_mat[pos,time-1] - E_mat[pos-1,time-1])
E_mat[pos-1,time] = E_mat[pos-1,time-1] + coe_E*(H_mat[pos,time] - H_mat[pos-1,time])
if pos == num_z-1:
if time == 0:
H_mat[num_z-1,0] = 0
E_mat[num_z-2,0] = 0
E_mat[num_z-1,0] = 0
if time > 0:
H_mat[num_z-1,time] = H_mat[num_z-1,time-1] + coe_H*(E_mat[num_z-1,time-1] - E_mat[num_z-2,time-1])
E_mat[num_z-2,time] = E_mat[num_z-2,time-1] + coe_E*(H_mat[num_z-1,time] - H_mat[num_z-2,time])
E_mat[num_z-1,time] = E_mat[num_z-2,time]
def update(val):
t = slider_time.val
if graph_type == '2d':
a.set_ydata(E_mat[:,t])
b.set_ydata(H_mat[:,t])
if graph_type == '3d':
a.set_3d_properties(E_mat[:,t])
a.set_data(z_grid,np.zeros((num_z,num_t))[:,t])
b.set_3d_properties(np.zeros((num_z,num_t))[:,t])
b.set_data(z_grid,H_mat[:t])
fig.canvas.draw_idle()
print(H_mat)
print(H_mat[:,t].size)
print(z_grid)
print(np.zeros((num_z,num_t))[:,t].size)
# Creating plot
if graph_type == '3d':
fig, ax = plt.subplots()
ax = plt.axes(projection='3d')
b, = ax.plot3D(z_grid,H_mat[:,t],np.zeros((num_z,num_t))[:,t], label='H')
a, = ax.plot3D(z_grid,np.zeros((num_z,num_t))[:,t],E_mat[:,t], label='E')
plt.title('Light Wave')
ax.set_xlabel('z')
ax.set_ylabel('x')
ax.set_zlabel('y')
plt.legend()
ax_time = plt.axes([0.25,0.1,0.65,0.03])
slider_time = Slider(ax_time,'Time',0,num_t-2,valinit=0,valstep=1)
slider_time.on_changed(update)
plt.show()
if graph_type == '2d':
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
a, = plt.plot(z_grid,E_mat[:,t], label='E (yz plane)')
b, = plt.plot(z_grid,H_mat[:,t], label='H (xz plane)')
plt.title('Light Wave')
plt.xlabel('z')
plt.ylabel('x')
plt.legend()
ax_time = plt.axes([0.25,0.1,0.65,0.03])
slider_time = Slider(ax_time,'Time',0,num_t-2,valinit=0,valstep=1)
slider_time.on_changed(update)
plt.show()
Any help would be appreciated. The middle for loop is just generating my functions, using the Yee Method.
When I run my code I create a figure, then I create a subplot in that figure. Then when I try to add a title to it using ax.set_title("title") it sometimes shows up for a split second then goes away. I have tried using plot.title aswell with no luck.
I tried recreating the error in a small example but for some reason it worked just fine there, so here is the entire source code of the code.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.style as style
import plotgen
from matplotlib.widgets import Button
class plotWindow():
def __init__(self):
style.use("bmh")
self.dp = 30
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1, label="ax1")
self.cax = 1
self.maxax = 2
self.minax = 1
plotgen.clear("plot1.txt")
plotgen.clear("plot2.txt")
axnext = plt.axes([0.80, 0.01, 0.06, 0.06])
axprev = plt.axes([0.73, 0.01, 0.06, 0.06])
bnext = Button(axnext, 'Next >')
bnext.on_clicked(self.changePlotNext)
bprev = Button(axprev, "< Previous")
bprev.on_clicked(self.changePlotPrev)
ani = animation.FuncAnimation(self.fig, self.animate, interval=500)
plt.show()
def changePlotNext(self, i):
if self.cax < self.maxax:
self.cax += 1
self.ax.set_title("Pump " + str(self.cax))
def changePlotPrev(self, i):
if self.cax > self.minax:
self.cax -= 1
self.ax.set_title("Pump " + str(self.cax))
def animate(self, i):
if self.cax == 1:
plotgen.generate("plot1.txt")
graph_data = open('plot1.txt', 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(float(y))
self.ax.clear()
lx = len(xs)
ly = len(ys)
if len(xs) < self.dp:
pxs = xs
pys = ys
else:
pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
pys = ys[(ly - (self.dp - 1)):(ly - 1)]
self.ax.plot(pxs, pys, "r")
elif self.cax == 2:
plotgen.generate("plot2.txt")
graph_data = open('plot2.txt', 'r').read()
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(float(y))
self.ax.clear()
lx = len(xs)
ly = len(ys)
if len(xs) <= self.dp:
pxs = xs
pys = ys
else:
pxs = xs[(lx - (self.dp - 1)):(lx - 1)]
pys = ys[(ly - (self.dp - 1)):(ly - 1)]
self.ax.plot(pxs, pys)
plotWindow()
As you can see in my changePlotNext and changePlotPrev functions I'm trying to change the title. Sometimes they display for a split second when I change, but then it goes away. And I am very aware that I have not set a title to display before I change the plot.
In animate, you have self.ax.clear(), which is removing all artists, texts, etc. on the Axes, including the title.
A simple option, then, is to reset the title after you clear the axes. So if you add:
self.ax.set_title("Pump " + str(self.cax))
in both places, immediately after you call self.ax.clear(), your titles will still be shown.
Another option would be to stop clearing the axes, but just remove the items you need to remove. I think this is just the lines you have plotted? So, for example, you could remove the call to self.ax.clear(), and add:
for line in self.ax.lines:
line.remove()
in its place. That will remove just the plotted line, but retain the title.