Extending lists with the same value on Python - python

I am very new to python, and I was trying to create a graph with matplotlib. I could create the first line with no problem, I defined 2 lists with x and y values that had the same length. For the second line, I was trying to plot a graph that displays the maximum y value of the first line for each x. My question is, do I have to explicitly write max(y1) x amount of times, or is there a way to automate the process?
x1 = [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
y1 = [36.37, 36.18, 36.31, 36.24, 36.31, 36.37, 36.18, 36.43, 36.12, 36.24, 36.06, 36.31, 36.12, 36.49, 36.74, 36.74]
plt.plot(x1, y1, color='black', marker='o', markerfacecolor='black', markersize='10', label='line1')
# I could just write x1 but this was a little easier for me to structure my code
x2 = x1
# Do I have to write [max(y1), max(y1), max(y1) ..., max(y1)] here or can I make a list with fewer items to represent one line (in this case y=36.74)
y2 = [max(y1)]
plt.plot(x2, y2, color='#30C3BF', linestyle='dashed', label='line2')

You can multiply lists.
y2 = [max(y1)] * len(y1)
results in a single list of the same length as y1 with all values set to the maximum in y1

Related

How to get data points after plot it? (Python)

I just used scipy.odeint to solve a diff_equation system, and use matplotlib to plot it. I got the graphs. My question is can I get some specific data points, like when t = 1, what is x1, x2, x3. I need when t = 1,2,3,4..., what value of concentration is. Thank you.
import matplotlib.pyplot as plt
from scipy.integrate import odeint
Dose = 100
V = 43.8
k12 = 1.2 # rate of central -> peripheral
k21 = 1.4 # rate of peripheral -> central
kel = 0.20 # rate of excrete from plasma
def diff(d_list, t):
x1, x2, x3, = d_list
# X1(t), X2(t), X3(t)
return np.array([(-k12*x1-kel*x1+k21*x2),
(k12*x1-k21*x2),
(kel*x1)])
t = np.linspace(0, 24, 960)
result = odeint(diff, [(Dose/V), 0, 0], t)
plt.plot(t, result[:, 0], label='x1: central')
plt.plot(t, result[:, 1], label='x2: tissue')
plt.plot(t, result[:, 2], label='x3: excreted')
plt.legend()
plt.xlabel('t (hr)')
plt.ylabel('Concentration (mg/L)')
plt.show()
This is not related to matplotlib or scipy. You can either interpolate or get the closest data point.
Interpolated value
If you need to get the x1, x2 and x3 for values of t which do not correspond to a data point (you mentioned 1,2,3,4 which are not in your t array), you will need to interpolate. To get x1, x2 and x3 at t=1, you can do (at the end of your script):
valuesAt1 = [np.interp(1, t, result[:,col]) for col in range(result.shape[1])]
The output of print(valuesAt1) is then:
[1.1059703843218311, 0.8813129004034452, 0.2958217381057726]
If you only need x1, just do
valuesAt1 = np.interp(1, t, result[:,0])
then, the output of print(valuesAt1) is:
1.1059703843218311
Closest data point
If you do not want to do interpolation but want the value of x1, x2 and x3 for the value of the t array element which is the closest from 1, do:
valuesAtClosestPointFrom1 = result[ np.argmin(np.abs(t-1))]
The output from print(valuesAtClosestPointFrom1) is:
[1.10563546 0.88141641 0.29605315]
This can be done by interpolation and using scipy.interpolate.InterpolatedUnivariateSpline as follows:
from scipy.interpolate import InterpolatedUnivariateSpline
splx1 = InterpolatedUnivariateSpline(t, result[:,0])
splx2 = InterpolatedUnivariateSpline(t, result[:,1])
splx3 = InterpolatedUnivariateSpline(t, result[:,2])
Firstly, you need to pass the x and y data that you want to interpolate. Secondly, create a list for x for which you want the desired values of y.
import numpy as np
desired_time = np.arange(1,25)
x1 = splx1(desired_time)
x2 = splx2(desired_time)
x3 = splx3(desired_time)
Lastly, pass it to the respective spline object to get the desired values. For example, a desired_time array from 1 to 24 using np.arange is created and passed to the spline objects in the example above.

How do you interpolate 2D data with unique y-arrays for each x-array value in Python?

I'm struggling to produce an interpolation function for some 2-dimensional data I have. My data isn't standard as each value in the x-array corresponds to a unique y-array. For example:
x = [0.1, 0.2]
y1 = [13.719, 10.488, 9.885, 9.704] #Corresponding to x=0.1
y2 = [13.34, 10.259, 9.275, 8.724] #Corresponding to x=0.2
z1 = [1395., 2209., 2411., 2555.] #Corresponding to y1
z2 = [1570., 2261., 2519., 2682.] #Corresponding to y2
Ideally I would like to generate a function, f(x, y) that will return an interpolated value of z.
So far my only attempts have been through using:
from scipy.interpolate import interp2d
interpolation = interp2d(x, [y1, y2], [z1, z2])
Which, not unsurprisingly, results in the following error message:
ValueError: x and y must have equal lengths for non rectangular grid
I understand why I'm getting this message and appreciate that interp2d is not the function I should be using, but I'm unsure where to go from here.
The problem is that interp2d works with data arranged on a rectangular grid. You only have 8 data points that are not arranged in a rectangular xy grid.
You can consider a rectangle 2x8 that consists of all possible combinations of your x and y data, but you only have 8 data points (z values).
Below is an example solution with more generic scipy.interpolate.griddata function:
x = [0.1, 0.2]
y1 = [13.719, 10.488, 9.885, 9.704] #Corresponding to x=0.1
y2 = [13.34, 10.259, 9.275, 8.724] #Corresponding to x=0.2
z1 = [1395., 2209., 2411., 2555.] #Corresponding to y1
z2 = [1570., 2261., 2519., 2682.] #Corresponding to y2
y=np.concatenate((y1,y2)) # collapse all y-data into a single array
# obtain x- and y- grids
grid_x, grid_y =np.meshgrid(np.array(x), y)[0].T, np.meshgrid(np.array(x), y)[1].T
points=np.stack((np.repeat(x,4).T,y)) #obtain xy corrdinates for data points
values=np.concatenate((z1,z2)) #obtain values
grid_z0 = griddata(points.T, values, (grid_x, grid_y), method='nearest') #Nearest neighbour interpolation
You can generalize this code for other interpolation options / denser grids and so on.

Given 2 coordinates, draw a line between them

I have given two coordinates (x1,y1) and (x2,y2) and would like to draw a line between them given a function drawdot(x,y). So to make it look like I draw a line I just want to draw 100 dots between the two points. How would I do this , I guesse its just a y = ax + b problem but I just can't manage to make it work.
thx
You need to take the unitary vector from point A to point B and then scale it n times.
Ill make a function that returns the points (pair of coordinates) that need to be drawn.
def drawLine(x1, y1, x2, y2, n):
v = (x2 - x1, y2 - y1)
length = math.sqrt(v[0]**2, v[1]**2)
unitary = (v[0]/length, v[1]/length)
return [(x1 + unitary[0]*i, y1 + unitary[1]*i) for i in range(n)]

Error bars in a logarithmic scale plot with Python

I'm working on a long code that produces the following graph.
The problem is that the error bars change their length because of the logarithmic scale, and I would like all of them to appear equal to each other, that is, to have a fixed length. Is there any way to do this easily with Python?
Edit: Here is part of the code, where I generate the error bars.
faztec=[11.4,11.4,7.1,7.7,6.8,6.9,6.2,7.5,5.5,4.0,3.5,3.2,2.9,2.9]
flaboca=[9.8,7.3,6.8,8.2,6.8,8.1,6.7,11.0,10.6,4.2,7.0,7.1,5.0,5.3]
err1 = [1.5,1.5,1.7,1.8,1.4,1.5,1.7,3.0,2.7,1.4,1.9,1.9,1.4,1.8]
err2 = [0.7,0.7,0.7,0.8,0.7,0.7,0.7,0.9,0.7,0.7,0.7,0.7,0.7,0.7]
newErr1x = []
newErr1y = []
for i in range(0, len(y)):
x1 = x[i]
y1 = (flaboca[i]-err1[i])/(faztec[i]+err2[i])
x2 = x[i]
y2 = (flaboca[i]+err1[i])/(faztec[i]-err2[i])
pl.plot([x1, x2], [y1, y2])
correction = False
# when end of segment is near 0, we must change it (because of logx)
if x2 < endBarLen:
x2 = 0.03
endBarLen = endBarLen / 10
correction = True
pl.plot([x1+endBarLen, x2-endBarLen], [y1, y1], '-k')
pl.plot([x1+endBarLen, x2-endBarLen], [y2, y2], '-k')
if correction:
correction = False
endBarLen = endBarLen*10
pl.show()
You are likely passing in an array as your error, but instead you should pass in the same value for every error bar if you wish for them to be the same length. It appears as if your y axis is not log-scale - if you wish to transform your values from log-space to linear space, you could take the exponent or log of your error data to adjust it accordingly. I would be able to provide a more specific answer if given more specific information

How to compute which way data points continue beyond an intersection?

Let's say you have two arrays of data values from a calculation, that you can model with a continuos, differentiable function each. Both "lines" of data points intersect at (at least) one point and now the question is whether the functions behind these datasets are actually crossing or anticrossing.
The image below shows the situation, where I know (from the physics behind it) that at the upper two "contact points" the yellow and green lines actually should "switch color", whereas at the lower one both functions go out of each others way:
To give an easier "toy set" of data, take this code for example:
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(-10,10,.5)
y1=[np.absolute(i**3)+100*np.absolute(i) for i in x]
y2=[-np.absolute(i**3)-100*np.absolute(i) for i in x][::-1]
plt.scatter(x,y1)
plt.scatter(x,y2,color='r')
plt.show()
Which should produce the following image:
Now how could I extrapolate whether the trend behind the data is crossing (so the data from the lower left continues to the upper right) or anti-crossing (as indicated with the colors above, the data from the lower left continues to the lower right)?
So far I was able to find the "contact point" between these to datasets by looking at the derivative of the Difference between them, roughly like this:
closePoints=np.where(np.diff(np.diff(array_A - array_B) > 0))[0] + 1
(which probably would be faster to evaluate with something like scipy's cKDTree).
Should I go on and (probably very inefficiently) check the derivative on both sides of the intersection? Or can I somehow check if the extrapolation of the data on the left side fits better to crossing or anticrossing?
I understood your problem as:
You have two sequences of points in a 2D plane.
The true curves can be approximated by straight lines between consecutive points of the sequences.
You want to know how often and where the two curves intersect (not only come into contact but really cross each other) (polygon intersection).
A potential solution is:
You look at each combination of a line segment of one curve with a line segment of another curve.
Combinations where the bounding boxes of the line segments have an overlap can potentially contain intersection points.
You solve a linear equation system to compute if and where an intersection between two lines occurs
In case of no solution to the equation system the lines are parallel but not overlapping, dismiss this case
In case of one solution check that it is truly within the segments, if so record this crossing point
In case of infinitely many intersections the lines are identical. This is also no real crossing and can be dismissed.
Do this for all combinations of line segments and eliminate twin cases, i.e. where the two curves intersect at a segment start or end
Let me give some details:
How to check if two bounding-boxes (rectangles) of the segments overlap so that the segments potentially can intersect?
The minimal x/y value of one rectangle must be smaller than the maximal x/y value of the other. This must hold for both.
If you have two segments how do you solve for intersection points?
Let's say segment A has two points (x1, y1) and (x2, y2) and segment B has two points (x2, y3) and (x4, y4).
Then you simply have two parametrized line equations which have to be set equal:
(x1, y1) + t * (x2 - x1, y2 - y1) = (x3, y3) + q * (x4 - x3, y4 - y3)
And you need to find all solutions where t or q in [0, 1). The corresponding linear equation system may be rank deficient or not solvable at all, best is to use a general solver (I chose numpy.linalg.lstsq) that does everything in one go.
Curves sharing a common point
Surprisingly difficult are cases where one point is common in the segmentation of both curves. The difficulty lies then in the correct decision of real intersection vs. contact points. The solution is to compute the angle of both adjacent segments of both curves (gives 4 angles) around the common point and look at the order of the angles. If both curves come alternating when going around the equal point then it's an intersection, otherwise it isn't.
And a code example based on your data:
import math
import matplotlib.pyplot as plt
import numpy as np
def intersect_curves(x1, y1, x2, y2):
"""
x1, y1 data vector for curve 1
x2, y2 data vector for curve 2
"""
# number of points in each curve, number of segments is one less, need at least one segment in each curve
N1 = x1.shape[0]
N2 = x2.shape[0]
# get segment presentation (xi, xi+1; xi+1, xi+2; ..)
xs1 = np.vstack((x1[:-1], x1[1:]))
ys1 = np.vstack((y1[:-1], y1[1:]))
xs2 = np.vstack((x2[:-1], x2[1:]))
ys2 = np.vstack((y2[:-1], y2[1:]))
# test if bounding-boxes of segments overlap
mix1 = np.tile(np.amin(xs1, axis=0), (N2-1,1))
max1 = np.tile(np.amax(xs1, axis=0), (N2-1,1))
miy1 = np.tile(np.amin(ys1, axis=0), (N2-1,1))
may1 = np.tile(np.amax(ys1, axis=0), (N2-1,1))
mix2 = np.transpose(np.tile(np.amin(xs2, axis=0), (N1-1,1)))
max2 = np.transpose(np.tile(np.amax(xs2, axis=0), (N1-1,1)))
miy2 = np.transpose(np.tile(np.amin(ys2, axis=0), (N1-1,1)))
may2 = np.transpose(np.tile(np.amax(ys2, axis=0), (N1-1,1)))
idx = np.where((mix2 <= max1) & (max2 >= mix1) & (miy2 <= may1) & (may2 >= miy1)) # overlapping segment combinations
# going through all the possible segments
x0 = []
y0 = []
for (i, j) in zip(idx[0], idx[1]):
# get segment coordinates
xa = xs1[:, j]
ya = ys1[:, j]
xb = xs2[:, i]
yb = ys2[:, i]
# ax=b, prepare matrices a and b
a = np.array([[xa[1] - xa[0], xb[0] - xb[1]], [ya[1] - ya[0], yb[0]- yb[1]]])
b = np.array([xb[0] - xa[0], yb[0] - ya[0]])
r, residuals, rank, s = np.linalg.lstsq(a, b)
# if this is not a
if rank == 2 and not residuals and r[0] >= 0 and r[0] < 1 and r[1] >= 0 and r[1] < 1:
if r[0] == 0 and r[1] == 0 and i > 0 and j > 0:
# super special case of one segment point (not the first) in common, need to differentiate between crossing or contact
angle_a1 = math.atan2(ya[1] - ya[0], xa[1] - xa[0])
angle_b1 = math.atan2(yb[1] - yb[0], xb[1] - xb[0])
# get previous segment
xa2 = xs1[:, j-1]
ya2 = ys1[:, j-1]
xb2 = xs2[:, i-1]
yb2 = ys2[:, i-1]
angle_a2 = math.atan2(ya2[0] - ya2[1], xa2[0] - xa2[1])
angle_b2 = math.atan2(yb2[0] - yb2[1], xb2[0] - xb2[1])
# determine in which order the 4 angle are
if angle_a2 < angle_a1:
h = angle_a1
angle_a1 = angle_a2
angle_a2 = h
if (angle_b1 > angle_a1 and angle_b1 < angle_a2 and (angle_b2 < angle_a1 or angle_b2 > angle_a2)) or\
((angle_b1 < angle_a1 or angle_b1 > angle_a2) and angle_b2 > angle_a1 and angle_b2 < angle_a2):
# both in or both out, just a contact point
x0.append(xa[0])
y0.append(ya[0])
else:
x0.append(xa[0] + r[0] * (xa[1] - xa[0]))
y0.append(ya[0] + r[0] * (ya[1] - ya[0]))
return (x0, y0)
# create data
def data_A():
# data from question (does not intersect)
x1 = np.arange(-10, 10, .5)
x2 = x1
y1 = [np.absolute(x**3)+100*np.absolute(x) for x in x1]
y2 = [-np.absolute(x**3)-100*np.absolute(x) for x in x2][::-1]
return (x1, y1, x2, y2)
def data_B():
# sine, cosine, should have some intersection points
x1 = np.arange(-10, 10, .5)
x2 = x1
y1 = np.sin(x1)
y2 = np.cos(x2)
return (x1, y1, x2, y2)
def data_C():
# a spiral and a diagonal line, showing the more general case
t = np.arange(0, 10, .2)
x1 = np.sin(t * 2) * t
y1 = np.cos(t * 2) * t
x2 = np.arange(-10, 10, .5)
y2 = x2
return (x1, y1, x2, y2)
def data_D():
# parallel and overlapping, should give no intersection point
x1 = np.array([0, 1])
y1 = np.array([0, 0])
x2 = np.array([-1, 3])
y2 = np.array([0, 0])
return (x1, y1, x2, y2)
def data_E():
# crossing at a segment point, should give exactly one intersection point
x1 = np.array([-1,0,1])
y1 = np.array([0,0,0])
x2 = np.array([0,0,0])
y2 = np.array([-1,0,1])
return (x1, y1, x2, y2)
def data_F():
# contacting at one segment point, should give no intersection point
x1 = np.array([-1,0,-1])
y1 = np.array([-1,0,1])
x2 = np.array([1,0,1])
y2 = np.array([-1,0,1])
return (x1, y1, x2, y2)
x1, y1, x2, y2 = data_F() # select the data you like here
# show example data
plt.plot(x1, y1, 'b-o')
plt.plot(x2, y2, 'r-o')
# call to intersection computation
x0, y0 = intersect_curves(x1, y1, x2, y2)
print('{} intersection points'.format(len(x0)))
# display intersection points in green
plt.plot(x0, y0, 'go')
plt.show() # zoom in to see that the algorithm is correct
I tested it extensively and should get most (all) border cases right (see data_A-F in code). Some examples:
Some Comments:
The assumption about the line approximation is crucial. Most true curves might only be to some extent be approximable to lines locally. Because of this places where the two curves come close but to not intersect with a distance in the order of the distance of consecutive sampling points of your curve - you may obtain false positives or false negatives. The solution is then to either use more points or to use additonal knowledge about the true curves. Splines might give a lower error rate but also require more computations, better sampling of the curves would be preferable then.
Self-intersection is trivially included when taking two times the same curve and let them intersect
This solution has the additional advantage that it isn't restricted to curves of the form y=f(x) but it's applicable to arbitrary curves in 2D.
You could use a spline interpolation for the difference function g(x) = y1(x) - y(2). Finding the minimum of the square g(x)**2 would be a contact or crossing point. Looking at the first and second derivative you could decide if it is a contact point( g(x) has minimum, g'(x)==0, g''(x) != 0) or a crossing point (g(x) is a stationary point, g'(x)==0, g''(x)==0).
The following code searches for a minimum of g(x)**2 in constrained interval and then plot the derivatives. The use of a constrained interval is to find multiple points successively by excluding intervals in which previous points were.
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as sopt
import scipy.interpolate as sip
# test functions:
nocrossingTest = True
if nocrossingTest:
f1 = lambda x: +np.absolute(x**3)+100*np.absolute(x)
f2 = lambda x: -np.absolute(x**3)-100*np.absolute(x)
else:
f1 = lambda x: +np.absolute(x**3)+100*x
f2 = lambda x: -np.absolute(x**3)-100*x
xp = np.arange(-10,10,.5)
y1p, y2p = f1(xp), f2(xp) # test array
# Do Interpolation of y1-y2 to find crossing point:
g12 = sip.InterpolatedUnivariateSpline(xp, y1p - y2p) # Spline Interpolator of Difference
dg12 = g12.derivative() # spline derivative
ddg12 = dg12.derivative() # spline derivative
# Bounded least square fit to find minimal distance
gg = lambda x: g12(x)*g12(x)
rr = sopt.minimize_scalar(gg, bounds=[-1,1]) # search minium in Interval [-1,1]
x_c = rr['x'] # x value with minimum distance
print("Crossing point is at x = {} (Distance: {})".format(x_c, g12(x_c)))
fg = plt.figure(1)
fg.clf()
fg,ax = plt.subplots(1, 1,num=1)
ax.set_title("Function Values $y$")
ax.plot(xp, np.vstack([y1p,y2p]).T, 'x',)
xx = np.linspace(xp[0], xp[-1], 1000)
ax.plot(xx, np.vstack([f1(xx), f2(xx)]).T, '-', alpha=0.5)
ax.grid(True)
ax.legend(loc="best")
fg.canvas.draw()
fg = plt.figure(2)
fg.clf()
fg,axx = plt.subplots(3, 1,num=2)
axx[0].set_title("$g(x) = y_1(x) - y_2(x)$")
axx[1].set_title("$dg(x)/dx$")
axx[2].set_title("$d^2g(x)/dx^2$")
for ax,g in zip(axx, [g12, dg12, ddg12]):
ax.plot(xx, g(xx))
ax.plot(x_c, g(x_c), 'ro', alpha=.5)
ax.grid(True)
fg.tight_layout()
plt.show()
The difference function show that the difference is not smooth:

Categories