0

I use numpy.meshgrid method to draw a 3D plot in Python. the drawing is over variables d1, d2 but I have a function consist of array with 7 elements which is 3 elements are independent from d1, d2. When I try to draw my graph I face this error:

operands could not be broadcast together with shapes (7,) (120,120)

When I debug my code I see the array elements which are independent from d1, d2 has shape of 1 but the other ones have shape of 120. How I can draw such a function using numpy.meshgrid method ?

Thanks. The whole code is like as follows:

import numpy
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

def function(a, b, c, d1, d2):
    Q = numpy.ones(7)

    EV = []
    EV.append(1 / (a + b))
    EV.append(1 / (a + b + c))
    EV.append(1 / (a + b + c))
    EV.append((1 - numpy.exp(-c * d1)) / c)
    EV.append((1 - numpy.exp(-c * d2)) / (c + a))
    EV.append((1 - numpy.exp(-c * d1)) / c)
    EV.append((1 - numpy.exp(-c * d2)) / (c + a))

    return numpy.sum(numpy.multiply(Q, EV) / numpy.sum(numpy.multiply(Q, EV)))


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
d1 = numpy.arange(0, 6, 0.05)
d2 = numpy.arange(0, 6, 0.05)
X, Y = numpy.meshgrid(d1, d2)

# Plot the surface.
ax.set_zlim(2.00, 8.00)
surf = ax.plot_surface(X, Y, function(1, 1, 1, X, Y), cmap='viridis', linewidth=0, antialiased=False)

# Customize the z axis.
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
4
  • 1
    Your function produces an array of 7 numbers for each input; e.g. function(1, 1, 1, 0, 0) is array([0.42857143, 0.28571429, 0.28571429, 0. , 0. , 0. , 0. ]). How would you want this to be plotted? Commented Oct 11, 2019 at 9:37
  • sorry, you are right! this is a part of a bigger code. I edit the code recheck it, Imagine I plot the element wise sum of the output array. the error is not because of what you said but I change the code to have a 1D output Commented Oct 11, 2019 at 9:43
  • 1
    what's the relation to np.meshgrid? the error occurs in numpy.sum(numpy.multiply(Q, EV) / numpy.sum(numpy.multiply(Q, EV))). not knowing what it is you want to calculate, anyhow, the result should be of shape (120,120) so you can call it in plot_surface(X, Y, Z) as Z. Commented Oct 11, 2019 at 10:14
  • The point is that my function accept originally single variables not arrays, but when I use numpy.meshgrid it produces a 2D array of all possible occurrence for d1, d2 and pass it to the function. If you watch my function precisely you could see it produce 7 numbers which is 3 is independent of d1, d2 and the other ones has d1, d2 so the dimension of result is strange. The out is a list of 3 single number and 4 array of 120 x 120. Commented Oct 11, 2019 at 14:35

1 Answer 1

1

You could do something like

Z = np.array([[function(1, 1, 1, x, y) for x in d1] for y in d2])
surf = ax.plot_surface(X, Y, Z, cmap='viridis', linewidth=0, antialiased=False)

The plot will not be particularly interesting though: First of all, element-wise multiplication by an array of ones is the identity operation, so np.multiply(Q, EV) is the same as turning EV into an array. For any array a, np.sum(a / np.sum(a)) is equal to 1 (since you can take the constant factor np.sum(a) outside of the outer sum). Therefore, function is constantly equal to one.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much for your answer which is completely right. But because matrix calculation has good performance in view of speed I don't want to use for loop. Do you know where is the point, I know it always has the value of 1 but it is not the point. The point is that my function accept originally single variables not arrays, but when I use numpy.meshgrid it produces a 2D array of all possible occurrence for d1, d2 and pass it to the function.
If you watch my function precisely you could see it produce 7 numbers which is 3 is independent of d1, d2 and the other ones has d1, d2 so the dimension of result is strange. The output is a list of 3 single number and 4 array of 120 x 120.
After your edit, the output is a single number, which is, modulo rounding issues, always 1.
Dear @fuglede this is not a math problem, The error is because the output is always 1 ?? No I want to know how to use meshgrid with unbalanced functions.
I never said that; only that in this particualr case, it causes the plot to be what it ends up being. The method applies in complete generality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.