I would like to create a multivariate function that takes the max value of 2 functions and then to plot it. However by using the max function there is an error when applying the function on the meshgrid. I have tried this on other multivariate function without the max function and it worked.
import numpy as np
import pandas as pd
import plotly.graph_objects as go
def f(x,y):
return max(np.cos(x),np.sin(y))
x=np.linspace(0,5,20)
y=np.linspace(-3,2,20)
X, Y = np.meshgrid(x, y)
Z=f(X,Y)
fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z)])
fig.show()
The error I get is : The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
. However, I don't think that the suggestion is adapted to my case. I also tried by defining the max function with if statement but as I expected I get the same error. Does anyone could help?
I have found a very simple alternative without using np.meshgrid
, and put it here in case it could help someone later.
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
x=np.linspace(0,5,20)
y=np.linspace(-3,2,20)
S=np.zeros([x.shape[0],y.shape[0]])
for i in range(x.shape[0]):
for j in range(y.shape[0]):
S[i,j]=f(x[i],y[j])
fig = go.Figure(data=[go.Surface(x=x, y=y, z=S, colorscale='Temps')])
fig.show()