Matplotlib How to get length line - python

I use Matplotlib with Python 2.7.6 and my code
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import spline
x = np.array([1,2,3])
y = np.array([1,5,15])
x_smooth = np.linspace(x.min(), x.max(), 100)
y_smooth = spline(x, y, x_smooth)
plt.plot(x_smooth, y_smooth)
plt.show()
When I run it show image
How to get the length of Spline ? Help me

import math
distance = 0
for count in range(1,len(y_smooth)):
distance += math.sqrt(math.pow(x_smooth[count]-x_smooth[count-1],2) + math.pow(y_smooth[count]-y_smooth[count-1],2))
in cartesian geometry the distance between two points is calculated as
p1(x,y), p2(a,b)
[p1p2] = sqrt((a-x)^2 + (b-y)^2)
I'm sure there is a more elegant way to do this, probably in a one-liner

Related

Generate values for y from a linear equation and plot on a graph

I am trying to plot the following function for values of x between -10 and 10
𝑦=2.5𝑥2+7𝑥+4.2
Click here to see the Maths Equation
I have managed to create an array 𝑥 consisting of 100 equally spaced values between -10 and 10 using the function linspace from numpy.
I'm struggling with how I can substitute the X values generated from the linspace function into the equation to find values for Y and then plot these on graph with a dashed black line instead of a solid blue line.
I know how to do this on paper, but I am struggling to understand which libraries to use in Python for this.
Here is what I have so far:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(start = -10, stop = 10, num = 100)
y = 2.5*x[0]**2 + 7*x[0] + 4.2
Is this what you are looking for? :)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(start = -10, stop = 10, num = 100)
y = 2.5*x**2 + 7*x + 4.2
plt.plot(x, y, '--', linewidth=2, markersize=12)
plt.show()
It should output this graph
Please write me back if you further face any issues. Cheers...

I am not able to display graph in matplotlib

I'm trying to print a logistic differential equation and I'm pretty sure the equation is written correctly but my graph doesn't display anything.
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
for num in range(len(xList)-1):
plt.plot(xList[num], eq(con,x))
x=eq(con,x)
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show()
You get nothing in your plot because you are plotting points.
In plt you need to have x array and y array (that have the same length) in order to make a plot.
If you want to do exactly what you are doing I suggest to do like this:
import matplotlyb.pyplot as plt # just plt is sufficent
import numpy as np
def eq(con,x):
return con*x*(1-x)
xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4
y = np.zeros(len(xList)) # initialize an array with the same lenght as xList
for num in range(len(xList)-1):
y[num] = eq(con,x)
x=eq(con,x)
plt.figure() # A good habit is always to use figures in plt
plt.plot(xList, y) # 2 arrays of the same lenght
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")
plt.show() # now you should get somthing here
I hope that this helps you ^^

Smooth curved line between 3 points in plot

I have 3 data points on the x axis and 3 on the y axis:
x = [1,3,5]
y=[0,5,0]
I would like a curved line that starts at (1,0), goes to the highest point at (3,5) and then finishes at (5,0)
I think I need to use interpolation, but unsure how. If I use spline from scipy like this:
import bokeh.plotting as bk
from scipy.interpolate import spline
p = bk.figure()
xvals=np.linspace(1, 5, 10)
y_smooth = spline(x,y,xvals)
p.line(xvals, y_smooth)
bk.show(p)
I get the highest point before (3,5) and it looks unbalanced:
The issue is due to that spline with no extra argument is of order 3. That means that you do not have points/equations enough to get a spline curve (which manifests itself as a warning of an ill-conditioned matrix). You need to apply a spline of lower order, such as a cubic spline, which is of order 2:
import bokeh.plotting as bk
from scipy.interpolate import spline
p = bk.figure()
xvals=np.linspace(1, 5, 10)
y_smooth = spline(x,y,xvals, order=2) # This fixes your immediate problem
p.line(xvals, y_smooth)
bk.show(p)
In addition, spline is deprecated in SciPy, so you should preferably not use it, even if it is possible. A better solution is to use the CubicSpline class:
import bokeh.plotting as bk
from scipy.interpolate import CubicSpline
p = bk.figure()
xvals=np.linspace(1, 5, 10)
spl = CubicSpline(x, y) # First generate spline function
y_smooth = spl(xvals) # then evalute for your interpolated points
p.line(xvals, y_smooth)
bk.show(p)
Just to show the difference (using pyplot):
As can be seen, the CubicSpline is identical to the spline of order=2
use pchip_interpolate():
import numpy as np
from scipy import interpolate
x = [1,3,5]
y=[0,5,0]
x2 = np.linspace(x[0], x[-1], 100)
y2 = interpolate.pchip_interpolate(x, y, x2)
pl.plot(x2, y2)
pl.plot(x, y, "o")
the result:
You can use quadratic interpolation. This is possible by making use of scipy.interpolate.interp1d.
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
x = [1, 3, 5]
y = [0, 5, 0]
f = interp1d(x, y, kind='quadratic')
x_interpol = np.linspace(1, 5, 1000)
y_interpol = f(x_interpol)
plt.plot(x_interpol, y_interpol)
plt.show()
Check the documentation for more details.

Algorithm equalivence from Matlab to Python

I've plotted a 3-d mesh in Matlab by below little m-file:
[x,n] = meshgrid(0:0.1:20, 1:1:100);
mu = 0;
sigma = sqrt(2)./n;
f = normcdf(x,mu,sigma);
mesh(x,n,f);
I am going to acquire the same result by utilization of Python and its corresponding modules, by below code snippet:
import numpy as np
from scipy.integrate import quad
import matplotlib.pyplot as plt
sigma = 1
def integrand(x, n):
return (n/(2*sigma*np.sqrt(np.pi)))*np.exp(-(n**2*x**2)/(4*sigma**2))
tt = np.linspace(0, 20, 2000)
nn = np.linspace(1, 100, 100)
T = np.zeros([len(tt), len(nn)])
for i,t in enumerate(tt):
for j,n in enumerate(nn):
T[i, j], _ = quad(integrand, -np.inf, t, args=(n,))
x, y = np.mgrid[0:20:0.01, 1:101:1]
plt.pcolormesh(x, y, T)
plt.show()
But the output of the Python is is considerably different with the Matlab one, and as a matter of fact is unacceptable.
I am afraid of wrong utilization of the functions just like linespace, enumerate or mgrid...
Does somebody have any idea about?!...
PS. Unfortunately, I couldn't insert the output plots within this thread...!
Best
..............................
Edit: I changed the linespace and mgrid intervals and replaced plot_surface method... The output is 3d now with the suitable accuracy and smoothness...
From what I see the equivalent solution would be:
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
x, n = np.mgrid[0:20:0.01, 1:100:1]
mu = 0
sigma = np.sqrt(2)/n
f = norm.cdf(x, mu, sigma)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x, n, f, rstride=x.shape[0]//20, cstride=x.shape[1]//20, alpha=0.3)
plt.show()
Unfortunately 3D plotting with matplotlib is not as straight forward as with matlab.
Here is the plot from this code:
Your Matlab code generate 201 points through x:
[x,n] = meshgrid(0:0.1:20, 1:1:100);
While your Python code generate only 20 points:
tt = np.linspace(0, 19, 20)
Maybe it's causing accuracy problems?
Try this code:
tt = np.linspace(0, 20, 201)
The seminal points to resolve the problem was:
1- Necessity of the equivalence regarding the provided dimensions of the linespace and mgrid functions...
2- Utilization of a mesh with more density to make a bee line into a high degree of smoothness...
3- Application of a 3d plotter function, like plot_surf...
The current code is totally valid...

Using scipy.spatial.Delaunay in place of matplotlib.tri.Triangulation's built-in version

It appears as if matplotlib.tri.Triangulation uses a buggy and possibly incorrect implementation of Delaunay triangulation that is due to be replaced by qHull.
I'm trying to plot a trisurf using mpl_toolkits.mplot3d.plot_trisurf() and running into a bunch of exceptions that are unhelpful (IndexErrors and KeyErrors mostly, with no indication of what exactly went wrong).
Since scipy.spatial.Delaunay already uses qHull, I was wondering if there was a way to build a matplotlib.tri.Triangulation object for use with mpl_toolkits.mplot3d.plot_trisurf() using scipy's implementation of Delaunay triangulation.
I've tried passing the delaunay.points directly to matplotlib.tri.Triangulate via the triangles parameter, but this results in a ValueError: triangles min element is out of bounds.
http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.spatial.Delaunay.html
http://matplotlib.org/dev/api/tri_api.html
So you need to pass both the points and the triangles from qhull to the Triangulation constructor:
import numpy as np
import scipy.spatial
import matplotlib
import math
import matplotlib.tri as mtri
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# First create the x and y coordinates of the points.
n_angles = 20
n_radii = 10
min_radius = 0.15
radii = np.linspace(min_radius, 0.95, n_radii)
angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)
angles[:, 1::2] += math.pi/n_angles
x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()
# Create the Delaunay tessalation using scipy.spatial
pts = np.vstack([x, y]).T
tess = scipy.spatial.Delaunay(pts)
# Create the matplotlib Triangulation object
x = tess.points[:, 0]
y = tess.points[:, 1]
tri = tess.vertices # or tess.simplices depending on scipy version
triang = mtri.Triangulation(x=pts[:, 0], y=pts[:, 1], triangles=tri)
# Plotting
z = x*x + y*y
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_trisurf(triang, z)
plt.show()
output (with matplotlib current master):
#Marco was curious to know how to run this for a 2d array. I hope this would be useful. The list of vertices according to coordinates should be made an array and can be tessellated using mtri.Triangulation.
Sample code below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
verts = np.array([[0.6,0.8],[0.2,0.9],[0.1,-0.5],[0.2,-2]])
triang = mtri.Triangulation(verts[:,0], verts[:,1])
plt.triplot(triang, marker="o")
plt.show()`enter code here`

Categories