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
Related
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:
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()
I have some code:
import numpy as np
import matplotlib.pyplot as plt
x = [0,1,2,3,4,5,6]
y = [5,6,7,10,100,10000,1000000]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.show()
You see, the biggest value is 1000000, if one value is very big, the other small values seems zero from the chart.
Can i use different steps for the axis Y?
The axis Y like this: small step ... middle step ... big step ...
You could use a logarithmic scale by setting plt.yscale():
import numpy as np
import matplotlib.pyplot as plt
plt.yscale('log')
x = [0,1,2,3,4,5,6]
y = [5,6,7,10,100,10000,1000000]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.show()
I plotted a surface using python and matplotlib but now I want to color it by its statistical significance.
So far I did:
import pylab as pl
import matplotlib.pyplot
import mpl_toolkits.mplot3d.axes3d as p3
from mpl_toolkits.mplot3d import Axes3D
fig = pl.figure()
ax = p3.Axes3D(fig)
x = N.array(intralist)
y = N.array(interlist)
Y,X = pl.meshgrid(y,x)
z = N.zeros((21,21))
for i in range(21):
for j in range(21):
z[i][j]=Fslist[i][j]
ax.plot_surface(X,Y,z)
ax.set_xlabel('Intracorrelation')
ax.set_ylabel('Intercorrelation')
ax.set_zlabel('F-Score')
pl.savefig('F-score.png', dpi=600, format='png')
However, I also test the significance of the correlations, and I want to map the F-score with the significance in the same plot. In summary I want to plot intracorrelation (X axis), intercorrelation (Y axis), F-score (Z axis) and statistical significance (color).
I have been checking in the internet, but I haven't been lucky.