Note that there are some explanatory texts on larger screens.

plurals
  1. POProblemw with python surface plot
    text
    copied!<p>I am new to programming in python. I am trying to show a surface plot by modifying an example found on the matplotlib website. Can you tell me what's wrong?</p> <p>My code is:</p> <pre><code>import matplotlib.pyplot as plt from matplotlib import cm import numpy def H(n, f, l, delta, H_abs, H_ph): c0 = 2.99796e8 n0 = 1.00027 + 0j n1 = complex(n[0], n[1]) Sum = 0 for i in range(1, delta+1): Sum = Sum + ((n0-n1)*exp(complex(0,-1*2*pi*f*n1*l/c0))/(n1+n0))**i H_Theo = 4*n0*n1*exp(complex(0,2*pi*f*l)*(n0-n1)/c0)*(1+Sum)/(n0+n1) M = abs(H_Theo) - H_abs A = cmath.phase(H_Theo) - H_ph return abs(M) + abs(A) freq = 1213188709257.4814 l_real = 6.77e-4 d = 0 H_abs = 0.798653104536778 H_ph = 2.1845744701729926 n_test = numpy.arange(1.5, 2.5, 0.01).tolist() k_test = numpy.arange(-0.05, 0.05, 0.001).tolist() X = n_test Y = k_test X, Y = numpy.meshgrid(X, Y) errore = [] for i in range(len(n_test)): errore.append(H([X[i],Y[i]], freq, l_real, d, H_abs, H_ph)) Z = errore fig2 = plt.figure() az = fig2.gca(projection='3d') az.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3) cset = az.contour(X, Y, Z, zdir='z', offset=min(Z)-1, cmap=cm.coolwarm) cset = az.contour(X, Y, Z, zdir='x', offset=min(X)-1, cmap=cm.coolwarm) cset = az.contour(X, Y, Z, zdir='y', offset=max(Y)+0.05, cmap=cm.coolwarm) az.set_xlabel('n') az.set_xlim(min(X)-1, max(X)+1) az.set_ylabel('k') az.set_ylim(min(Y)-0.05, max(Y)+0.05) az.set_zlabel('Err') az.set_zlim(min(Z)-1, max(Z)+1) plt.show() </code></pre> <p>The error I get is:</p> <pre><code>n1 = complex(n[0], n[1]) TypeError: only length-1 arrays can be converted to Python scalars </code></pre> <p>I have seen other Q&amp;A on the subject but I was unable to solve the problem, anyway.</p> <p>Maybe the question is trivial but I will really appreciate any help in order to be able to see the graph that, at the end, should turn out to be similar to this one: <a href="http://matplotlib.org/examples/mplot3d/contour3d_demo3.html" rel="nofollow">http://matplotlib.org/examples/mplot3d/contour3d_demo3.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload