Im currently working on graphing a square wave in python using numpy and pylot.
How would I plot a square wave function over multiple periods of T?
I currently have:
from scipy import signal
import numpy as np
from scipy.fftpack import fft
#Initialize Parameters
p_0 = 2
A = np.sqrt(1/(2*p_0))
t = [-A,A]
plt.plot(t,[A,A])
plt.show()
which just gives me a straight line.
The end game is to take the Fourier transform of the square wave function
You could use the square function from scipy.signal
from scipy import signal
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500, endpoint=False)
plt.plot(t, signal.square(2 * np.pi * 5 * t),'b')
plt.ylim(-2, 2)
plt.grid()
plt.show()
This code below will do the trick:
import math, numpy
from matplotlib import pyplot as plt
x = numpy.linspace(0, 10, 1000)
y = numpy.array([1 if math.floor(2 * t) % 2 == 0 else 0 for t in x])
plt.plot(x,y)
plt.show()
If the math.floor(2 * t) % 2 == 0 returns True plot 1 else plot 0.
Output:
Related
In MATLAB I can do ezplot("x.*tan(x)") to plot the following:
While in Symbolab it will be like this
While in Python I use Numpy or Sympy, but still, the graph is so different
from sympy import symbols
from sympy.functions.elementary.trigonometric import tan
from sympy.plotting.plot import plot
x = symbols('x')
eqn = tan(x)*x
plot(eqn, (x, 0, 10), ylim=(-20, 20))
How can I get an image with Python equivalent to that in MATLAB and Symbolab?
You can try fplot for a continuous uncertainty. Then limit the plot for x and y axis.
fplot(#(x)x.*tan(x))
xlim([-10,10])
ylim([-5 10])
grid on
Matlab plot
The dashed line in plot are the same as the vertical lines in sympy plots.
In python you may use pyplot with markers instead of sympy plot to avoid continuity vertical lines.
from string import whitespace
from sympy import symbols
from sympy import *
from sympy.functions.elementary.trigonometric import tan
from sympy.plotting.plot import plot
import numpy as np
import matplotlib.pyplot as plt
x = symbols('x')
eqn = tan(x)*x
xx = np.linspace(-100, 100, 10000)
yy = lambdify(x, eqn)(xx)
plt.plot(xx,yy,'.')
plt.ylim(-5, 10)
plt.xlim(-20, 20)
plt.grid(True)
plt.show()
Outcome Pyplot image
I have a sympy function, I want to plot it and color the area beneath the curve, how can I do it?
Code
import sympy as sy
x = sy.symbols('x')
f = sy.sin(x)
sy.plot(f, (x, 0, sy.pi))
Plot
i have created the same output without using matplotlib directly(sympy already uses sympy)
import sympy as sy
import numpy as np
from sympy.plotting import plot
x = sy.symbols('x')
f = sy.sin(x)
x_array = np.linspace(0, np.pi, 1000)
f_array = sy.lambdify(x, f)(x_array)
plot(f, (x, 0, sy.pi), fill={'x': x_array,'y1':f_array,'color':'green'})
i got the output
You can use plt.fill_between (documentation) but you need to convert your sympy function in a numpy array with sy.lambdify (documentation) before, because plt.fill_between takes in arrays.
Check this code as a reference:
import sympy as sy
import numpy as np
import matplotlib.pyplot as plt
x = sy.symbols('x')
f = sy.sin(x)
x_array = np.linspace(0, np.pi, 1000)
f_array = sy.lambdify(x, f)(x_array)
fig, ax = plt.subplots()
ax.plot(x_array, f_array, color = 'red')
ax.fill_between(x_array, f_array, facecolor = 'red', alpha = 0.3)
plt.show()
I'm looking for a way to plot a corrugated circle in Python.
My attempt doesn't produce the correct output:
from matplotlib import pyplot as plt
import numpy as np
from math import pi
x=np.linspace(-10,10,100)
y=x
X, Y = np.meshgrid(x,y)
circle = (X-np.cos(2*pi*0.2*Y))**2 + (Y-np.sin(2*pi*0.2*X))**2 - 5.
plt.contour(X,Y,circle,[0])
plt.show()
theta = np.linspace(-pi,pi,100)
courbure = np.sin(theta*10)
plt.plot(theta,courbure)
plt.show()
circle2 = (X-(courbure*np.cos(theta)))**2 + (Y-np.sin(theta)*courbure)**2 - courbure**2
plt.contour(X,Y,circle2)
plt.show()
Thank you.
I have plot a corrugate circle using a sinusoidal wave of a frequency egal to 10. The radius oscillate between 0.9 et 1. because i take the negative absolute part of the sinus. The amplitude is divided by 10.
For increase the corrugation you have to increase the frequency.
Here f=10.
from matplotlib import pyplot as plt
import numpy as np
from math import pi
x=np.linspace(-10,10,1000)
y=x
X, Y = np.meshgrid(x,y)
circle = (X)**2 + (Y)**2 - (1+ -np.abs(np.sin(np.arctan(Y/X)*10))/10)
plt.contour(X,Y,circle)
plt.show()
The goal is to display a nice waterfall from an existing fft
Start from an existing fft which can be found at https://docs.scipy.org/doc/scipy/reference/tutorial/fftpack.html
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from scipy.fftpack import fft
from mpl_toolkits.mplot3d import Axes3D
N = 600
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.show()
The result is the expected spectrum. Now we want to plot a waterfall as depicted at https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#wireframe-plots
We want something like that :
x, y = (xf, np.arange(N))
X,Y=np.meshgrid(x,y)
Z = yf
Axes3D.plot_wireframe(X, Y, Z,rstride=1,cstride=len(xf), lw=.5, alpha=0.5)
plt.show()
where X is the freq range of the Fft, Y the plot number axis and Z the 2D array with Fft data. But we get this error :
TypeError: plot_wireframe() missing 1 required positional argument: 'Z'
What is the problem ?
Thanks for the help.
To display a waterfall, you need a 2D array of FFTs (usually of different time windows), not just one FFT result.
I would like to plot the Poisson function in Python using Matplotlib. The function is (exp(-5)*5^x)/factorial(x)
import numpy as np
import math
import matplotlib.pyplot as plt
t = np.arange(0, 20, 0.1)
d = []
for i in t:
p = pow(5,i)
q = p/math.factorial(i)
d.append(q)
plt.plot( t, np.exp(-5)*d, 'bs')
plt.show()
But I get this error."Only size^1 arrays can be converted to Python scalars". How can I plot this graph? Thanks in advance
i think your function is not right: it's exp(-5)
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import factorial
t = np.arange(0, 20, 0.1)
d = np.exp(-5)*np.power(5, t)/factorial(t)
plt.plot(t, d, 'bs')
plt.show()
The immediate problem is probably that you are using 't' instead of 'i' in your loop. However, you may want to avoid mixing python lists with numpy arrays. You could do it like,
import numpy as np
import scipy.misc
import math
import matplotlib.pyplot as plt
t = np.arange(0, 20, 0.1)
x = np.power(t, 5)
y = scipy.misc.factorial(t)
plt.plot( t, x / y, 'bs')
plt.show()