I could not remotely figure out how to define a plot over a triangular region in matplotlib. That is the first problem I have.
As a workaround, I thought to define the function using a conditional expression, to avoid problems where the function is not defined.
def f(x,y):
for a in x:
for b in y:
if a>b:
g = log(a-b)
else:
g = 0
return
x = np.linspace(0.1, 1000, 30)
y = np.linspace(0.1, 3, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
but I get the error message
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
which at least made me understand (sort of) what meshgrid does.
To summarise:
1) what is the neatest way to plot a function over a triangle?
2) what is the advantages of plotting over a meshgrid, instead of defining a single array as ([X1,Y1], [X1,Y2], ..., [X1,YN], [X2, Y1], [X2, Y2], ..)?
Thanks a lot
The problem that you encounter is using a normal Python function for NumPy arrays. This does not work always as expected, especially when you use conditionals like <. You can simplify f as:
import math
def f(x,y):
if x > y:
return math.log(x-y)
else:
return 0.0 # or math.nan
and than make a numpy ufunc from it using np.frompyfunc:
f_np = np.frompyfunc(f,2,1)
Now you can do:
Z = f_np(X,Y)
Notes: If you intend to use plt.contourf(X,Y,Z), everything will clearer if you use y = np.linspace(0.1, 1000, 30) instead of y = np.linspace(0.1, 3, 30) so that the triangle is exactly half of the plot. If you use math.nan in f instead of 0.0, the triangle will be left blank in the contourplot.
Related
I have 2 numpy arrays with data, say x,y, and I apply plt.step() and get a continues (step) curve of it.
I would like to be able to create this function by my own, meaning I want to have an (zero order hold) step approximation to the value of y for x that does not actually exist in the original x array.
For example, in the following link I want to have the 'new' actual rectangle sine values, and not only plotted:
https://matplotlib.org/gallery/lines_bars_and_markers/step_demo.html#sphx-glr-gallery-lines-bars-and-markers-step-demo-py
You can use scipy's interp1d to create a step function. Default the interpolation is 'linear', but you can change it to 'next', 'previous' or 'nearest' for a step function.
A standard step function is obtained from step_fun = interp1d(x, y, kind='previous') and then calling it as step_fun(new_x).
The following code compares different types of "interpolation":
from matplotlib import pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
x = np.random.uniform(0.1, 0.7, 20).cumsum()
y = np.sin(x)
kinds = ['linear', 'previous', 'next', 'nearest', 'cubic']
for i, kind in enumerate(kinds):
function_from_points = interp1d(x, y + i, kind=kind)
x_detailed = np.linspace(x[0], x[-1], 1000)
plt.plot(x_detailed, function_from_points(x_detailed), color='dodgerblue')
plt.scatter(x, y + i, color='crimson')
plt.yticks(range(len(kinds)), kinds)
plt.show()
You can choose tick values and corresponding function values whichever you want. This is an example not equally spaced arguments and their values:
x = np.arange(20) + np.random.random(20)/2
y = np.sin(x / 2)**2 + np.random.random(20)/5
Remark: these two arrays must have equal size. If you want your own custom function, you can use np.vectorise:
x = np.arange(20) + np.random.random(20)/2
func = np.vectorize(lambda x: np.sin(x) + np.random.random()/5)
y = func(x)
I tried to plot pairs (x,y) which are solutions to the equation y^2 + y = x^3 + y(x^2) using the following code:
import numpy as np
y = np.arange(0,12,.01)
x = np.arange(0,18,.01)
for i in enumerate(y):
for j in enumerate(x):
if (i**2)+i == j**3 + i*(j**2):
plot(i,j)
However, I got the following error:
"TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'"
Anybody can help me to figure out how can I plot solutions?
I have also another question: Is it possible to store each pair of solution in a variable so we can use specific pairs of solutions in the future?
enumerate yields (index, value) tuples, you can use unpacking to get each one separately in the loop header:
for i_ind, i in enumerate(y):
for j_ind, j in enumerate(x):
I suggest you modify your formulation and plot the square difference between LHS and RHS instead:
x = np.linspace(-1, 1, 100)
y = np.linspace(-1, 1, 100)
X, Y = np.meshgrid(x, y)
def F(p):
x, y = p
v = y**2 + y - x**3 - y * (x**2)
return v**2
Z = F((X, Y))
plt.contourf(X, Y, Z)
plt.colorbar()
idx = np.where(np.abs(Z) < 1e-4)
plt.scatter(X[idx], Y[idx], marker='+', color='w')
You can also use scipy.optimize.fmin(F, [1, 1]) to get the exact solution of F=0, where [1, 1] is your initial guess. In this case, different initial guess will result in different solution.
while the other answer describes the mechanical transform needed to stop that exception being thrown, it's not going to get any points in your plot. Numpy is a numeric library, while you want/need to solve the equation.
numpy.arange(0, 12, 0.01) generates a finite series of numbers starting at 0 and incrementing by 0.01 until it hits 12. you give an equation above which is mostly translated into Python code, and evaluate it on these finite series of numbers and ask it to plot values where both expressions evaluate to the same number. floats in Python are 64-bit floats so this equality is checked to approx 15 decimal digits, which will basically not happen for your numbers.
instead of doing what you're attempting to do you want to use something like Sympy which would actually help you. to get started we do:
from sympy import symbols, solveset, simplify, lambdify
x, y = symbols('x y')
eq1 = y**2 + y
eq2 = x**3 + x*y**2
ss = solveset(eq1 - eq2, y)
which names a pair of symbols, puts them into some equations and solves for when eq1 == eq2 (equivalently when their difference is zero).
next you want to plot these solutions, which can be done in this case:
import numpy as np
import matplotlib.pyplot as plt
xx = np.linspace(-5, 4, 101)
for eq in ss:
fn = lambdify(x, eq)
yy = fn(xx)
# hide complex solutions
yy[~np.isreal(yy)] = np.nan
plt.plot(xx, np.real(yy), label=simplify(eq))
plt.legend()
this starts by pulling in numpy and matplotlib, then sampling points on the x-axis at which we want to evaluate the functions, then loop through solutions turning them into Python functions that we can evaluate to get numeric answers, next we evaluate this on our sampled points, discard complex solutions, and plot the results.
this gives:
the gap around x = -4.5 is due to sampling at this point being sparse. we can check by evaluating our equations at x=-4.5, y=10, and I get ~110 for both equations
I have been trying to get this to work for a while now, but still not finding a way. I am trying to compute the Look ahead estimate density of a piecewise gaussian function. I'm trying to estimate the stationary distribution of a piecewise normally distributed function. is there a way to avoid the error type:
Error-type: the truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
for instance y=np.linspace(-200.0,200.0,100) and x = np,linspace(-200.0,200.0,100). then verify the condition as stated in the code below?
import numpy as np
import sympy as sp
from numpy import exp,sqrt,pi
from sympy import Integral, log, exp, sqrt, pi
import math
import matplotlib.pyplot as plt
import scipy.integrate
from scipy.special import erf
from scipy.stats import norm, gaussian_kde
from quantecon import LAE
from sympy.abc import q
#from sympy import symbols
#var('q')
#q= symbols('q')
## == Define parameters == #
mu=80
sigma=20
b=0.2
Q=80
Q1=Q*(1-b)
Q2=Q*(1+b)
d = (sigma*np.sqrt(2*np.pi))
phi = norm()
n = 500
#Phi(z) = 1/2[1 + erf(z/sqrt(2))].
def p(x, y):
# x, y = np.array(x, dtype=float), np.array(y, dtype=float)
Positive_RG = norm.pdf(x-y+Q1, mu, sigma)
print('Positive_R = ', Positive_RG)
Negative_RG = norm.pdf(x-y+Q2, mu, sigma)
print('Negative_RG = ', Negative_RG)
pdf_0= (1/(2*math.sqrt(2*math.pi)))*(erf((x+Q2-mu)/(sigma*np.sqrt(2)))-erf((x+Q1-mu)/(sigma*np.sqrt(2))))
Zero_RG =norm.pdf
print('Zero_RG',Zero_RG)
print ('y',y)
if y>0.0 and x -y>=-Q1:
#print('printA', Positive_RG)
return Positive_RG
elif y<0.0 and x -y>=-Q2:
#print('printC', Negative_RG)
return Negative_RG
elif y==0.0 and x >=-Q1:
#print('printB', Zero_RG)
return Zero_RG
return 0.0
Z = phi.rvs(n)
X = np.empty(n)
for t in range(n-1):
X[t+1] = X[t] + Z[t]
#X[t+1] = np.abs(X[t]) + Z[t]
psi_est = LAE(p, X)
k_est = gaussian_kde(X)
fig, ax = plt.subplots(figsize=(10,7))
ys = np.linspace(-200.0, 200.0, 200)
ax.plot(ys, psi_est(ys), 'g-', lw=2, alpha=0.6, label='look ahead estimate')
ax.plot(ys, k_est(ys), 'k-', lw=2, alpha=0.6, label='kernel based estimate')
ax.legend(loc='upper left')
plt.show()
See all those ValueError questions in the side bar????
This error is produced when a boolean array is used in a scalar boolean context, such as if or or/and.
Try your y or x in this test, or even simpler one. Experiment in a interactive shell.
if y>0.0 and x -y>=-Q1: ....
if y>0:
(y>0.0) and (x-y>=10)
will all produce this error with your x and y.
Notice also that I edited your question for clarity.
Error starts with quantecon.LAE(p, X), which expects a vectorized function p. Your function isn't vectorized, which is why everything else doesn't work. You copied some vectorized code, but left a lot of things as sympy style functions which is why the numpy folks were confused about what you wanted.
In this case "vectorized" means transforming two 1D arrays with length n into a 2D n x n array. In this case, you don't want to return 0.0, you want to return out a 2d ndArray which has the value 0.0 in locations out[i,j] where a boolean mask based on a function of x[i], y[j] is false.
You can do this by broadcasting:
def sum_function(x,y):
return x[:, None] + y[None, :] # or however you want to add them, broadcasted to 2D
def myFilter(x,y):
x, y = x.squeeze(), y.squeeze()
out=np.zeros((x.size,y.size))
xyDiff = x[:, None] - y[None, :]
out=np.where(np.bitwise_and(y[None, :] => 0.0, xyDiff >= -Q1), sum_function(x, y), out) # unless the sum functions are different
out=np.where(np.bitwise_and(y[None, :] < 0.0, xyDiff >= -Q2), sum_function(x, y), out)
return out
I am new to python and trying to 3d plot a piecewise function. I am trying to 3d plot the 'mainformula' function below on the z-axis as it varies with x and y ranging from 0 to 10, and constant = 1. But I can't quite seem to figure out the plotting method here.
from sympy import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
def mainformula(x,y,constant):
return Piecewise((subformula1(x,y,constant), y >= 0 and y < 3),(subformula2(x,y,constant),y>=3 and y <= 10))
def subformula1(x,y,constant):
return x + y + constant
def subformula2(x,y,constant):
return x - y - constant
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(0, 10, 0.25)
Y = np.arange(0, 10, 0.25)
constant = 1
X, Y = np.meshgrid(X, Y)
Z = mainformula(X,Y,constant)
surf = ax.plot_surface(X, Y, Z)
plt.show()
The error I get when I run that code is: "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
You are working on arrays so it's never going to work to use array > 3 in a boolean context (for example with and) this will always give you the error you received. But you can always define your conditions as boolean masks and operate your formula on the appropriate elements:
def mainformula(x,y,constant):
z = np.zeros_like(x)
# Condition 1 indexes all elements where subformula 1 is valid
condition1 = np.logical_and(y >= 0, y < 3)
# condition1 = (y >= 0) & (y < 3) # is another way of writing it
z[condition1] = x[condition1] + y[condition1] + constant
# now do it in the range where subformula 2 is valid
condition2 = np.logical_and(y >= 3, y <= 10)
# condition1 = (y >= 3) & (y <= 10) # is another way of writing it
z[condition2] = x[condition2] - y[condition2] - constant
return z
This doesn't use sympy.Piecewise but works alright when only trying to plot. If you want seperate functions instead of doing it all in the main formula you need to change it a bit:
z[condition1] = subformula1(x[condition1], y[condition1], constant)
and similar for condition2.
The problem is you are trying to make logic assertions into arrays that do not have a direct one. It's difficult for Python to assert what this is:
y >= 0 and y < 3
So you'll have to change somewhat your code into something it can understand:
def mainformula(x,y,constant):
y2 = y[y>=0]
y2 = y2[y2<3]
y3 = y[y>=3]
y3 = y2[y2<=10]
return Piecewise((subformula1(x,y,constant), y2),(subformula2(x,y,constant),y3))
The problem is Piecewise function also does not seem to accept some arrays into one of the arguments you have. You'll have to rethink your problem, perhaps by building a loop for the Piecewise function in order to be launched at every element.
What is a good way to produce a numpy array containing the values of a function evaluated on an n-dimensional grid of points?
For example, suppose I want to evaluate the function defined by
def func(x, y):
return <some function of x and y>
Suppose I want to evaluate it on a two dimensional array of points with the x values going from 0 to 4 in ten steps, and the y values going from -1 to 1 in twenty steps. What's a good way to do this in numpy?
P.S. This has been asked in various forms on StackOverflow many times, but I couldn't find a concisely stated question and answer. I posted this to provide a concise simple solution (below).
shorter, faster and clearer answer, avoiding meshgrid:
import numpy as np
def func(x, y):
return np.sin(y * x)
xaxis = np.linspace(0, 4, 10)
yaxis = np.linspace(-1, 1, 20)
result = func(xaxis[:,None], yaxis[None,:])
This will be faster in memory if you get something like x^2+y as function, since than x^2 is done on a 1D array (instead of a 2D one), and the increase in dimension only happens when you do the "+". For meshgrid, x^2 will be done on a 2D array, in which essentially every row is the same, causing massive time increases.
Edit: the "x[:,None]", makes x to a 2D array, but with an empty second dimension. This "None" is the same as using "x[:,numpy.newaxis]". The same thing is done with Y, but with making an empty first dimension.
Edit: in 3 dimensions:
def func2(x, y, z):
return np.sin(y * x)+z
xaxis = np.linspace(0, 4, 10)
yaxis = np.linspace(-1, 1, 20)
zaxis = np.linspace(0, 1, 20)
result2 = func2(xaxis[:,None,None], yaxis[None,:,None],zaxis[None,None,:])
This way you can easily extend to n dimensions if you wish, using as many None or : as you have dimensions. Each : makes a dimension, and each None makes an "empty" dimension. The next example shows a bit more how these empty dimensions work. As you can see, the shape changes if you use None, showing that it is a 3D object in the next example, but the empty dimensions only get filled up whenever you multiply with an object that actually has something in those dimensions (sounds complicated, but the next example shows what i mean)
In [1]: import numpy
In [2]: a = numpy.linspace(-1,1,20)
In [3]: a.shape
Out[3]: (20,)
In [4]: a[None,:,None].shape
Out[4]: (1, 20, 1)
In [5]: b = a[None,:,None] # this is a 3D array, but with the first and third dimension being "empty"
In [6]: c = a[:,None,None] # same, but last two dimensions are "empty" here
In [7]: d=b*c
In [8]: d.shape # only the last dimension is "empty" here
Out[8]: (20, 20, 1)
edit: without needing to type the None yourself
def ndm(*args):
return [x[(None,)*i+(slice(None),)+(None,)*(len(args)-i-1)] for i, x in enumerate(args)]
x2,y2,z2 = ndm(xaxis,yaxis,zaxis)
result3 = func2(x2,y2,z2)
This way, you make the None-slicing to create the extra empty dimensions, by making the first argument you give to ndm as the first full dimension, the second as second full dimension etc- it does the same as the 'hardcoded' None-typed syntax used before.
Short explanation: doing x2, y2, z2 = ndm(xaxis, yaxis, zaxis) is the same as doing
x2 = xaxis[:,None,None]
y2 = yaxis[None,:,None]
z2 = zaxis[None,None,:]
but the ndm method should also work for more dimensions, without needing to hardcode the None-slices in multiple lines like just shown. This will also work in numpy versions before 1.8, while numpy.meshgrid only works for higher than 2 dimensions if you have numpy 1.8 or higher.
import numpy as np
def func(x, y):
return np.sin(y * x)
xaxis = np.linspace(0, 4, 10)
yaxis = np.linspace(-1, 1, 20)
x, y = np.meshgrid(xaxis, yaxis)
result = func(x, y)
I use this function to get X, Y, Z values ready for plotting:
def npmap2d(fun, xs, ys, doPrint=False):
Z = np.empty(len(xs) * len(ys))
i = 0
for y in ys:
for x in xs:
Z[i] = fun(x, y)
if doPrint: print([i, x, y, Z[i]])
i += 1
X, Y = np.meshgrid(xs, ys)
Z.shape = X.shape
return X, Y, Z
Usage:
def f(x, y):
# ...some function that can't handle numpy arrays
X, Y, Z = npmap2d(f, np.linspace(0, 0.5, 21), np.linspace(0.6, 0.4, 41))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z)
The same result can be achieved using map:
xs = np.linspace(0, 4, 10)
ys = np.linspace(-1, 1, 20)
X, Y = np.meshgrid(xs, ys)
Z = np.fromiter(map(f, X.ravel(), Y.ravel()), X.dtype).reshape(X.shape)
In the case your function actually takes a tuple of d elements, i.e. f((x1,x2,x3,...xd)) (for example the scipy.stats.multivariate_normal function), and you want to evaluate f on N^d combinations/grid of N variables, you could also do the following (2D case):
x=np.arange(-1,1,0.2) # each variable is instantiated N=10 times
y=np.arange(-1,1,0.2)
Z=f(np.dstack(np.meshgrid(x,y))) # result is an NxN (10x10) matrix, whose entries are f((xi,yj))
Here np.dstack(np.meshgrid(x,y)) creates an 10x10 "matrix" (technically a 10x10x2 numpy array) whose entries are the 2-dimensional tuples to be evaluated by f.
My two cents:
import numpy as np
x = np.linspace(0, 4, 10)
y = np.linspace(-1, 1, 20)
[X, Y] = np.meshgrid(x, y, indexing = 'ij', sparse = 'true')
def func(x, y):
return x*y/(x**2 + y**2 + 4)
# I have defined a function of x and y.
func(X, Y)