What is wrong with this code?
valavel last edited by ccc
Hi I am trying to learn Python and Pythonista trying to run this downloaded code, but I get a too many values error. Any help here or in understand 3D vector fields will be appreciated.
BTW the link to this forum from the Pythonista console does not work.
# The first step is to import the required packages. The aliases `plt` and # `np` are coding conventions. # * `Axes3D` allows adding 3d objects to a 2d matplotlib plot. # * The `pyplot` submodule from the **matplotlib** library, a python 2D # plotting library which produces publication quality figures. # * The `numpy` library for efficient numeric-array manipulation from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np # Now we set up the data to plot, the `x`, `y` and `z` arrays are the # coordinates for the start points of each arrow, whilst the `u`, `v` and # `w` arrays are the coordinates of the endpoints. x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2), np.arange(-0.8, 1, 0.2), np.arange(-0.8, 1, 0.8)) u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z) v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z) w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z)) # Now we create the plot with `quiver()`. The 1.4.x version of newer of the # **matplotlib** library is required for this command to work. For the colour # of the arrows it is possible to use either html hexadecimal codes, html # colour names or a 3-tuple of rgb values. fig = plt.figure() ax = fig.gca(projection='3d') ax.quiver(x, y, z, u, v, w, # data length=0.15, # arrow length color='Tomato' # arrow colour ) ax.set_title('3D Vector Field') # title ax.view_init(elev=18, azim=30) # camera elevation and angle ax.dist=8 # camera distance plt.show()
MartinPacker last edited by
Not to try to answer your question but just to observe you're (mostly) using a large bold font in this post.
Only the code snippets are normal. I don't know if you can edit the post but anyway...
ccc last edited by ccc
Your code works beautifully on Pythonista 3 (and produces a cool graph!) when running in both Python 2.7 mode and in Python 3.5 mode.
Your code does not work in Pythoista 2 which runs an older version of Matplotlib. I suspect that the signature of the
quiver()method changed between Matplotlib 1.3 and 1.4.0 and that your code was written for 1.4 and later.
import matplotlib print(matplotlib.__version__) # 1.3.1 in Pythonista 2 and 1.4.0 in Pythonista 3
For posting code to this forum, please use the three backticks trick. Enter:
- <a blank line>
- ```python <nothing else on this line>
- <your code>
- ``` <nothing else on this line>
You can find the backtick character (`) by doing a press and hold on the ' key.
Thank you MartinPacker in future I will try to format my posted code properly - actually it was a copy/paste situation from another source.
Thank you ccc I upgraded to Pythonista 3 and indeed the nice 3D vector plot appeared :)
Hi After upgrading to Pythonista 3 I ended up with two apps on the ipad the old blue Py 2 and the new green Py 3. Shall I delete the Py 2 app and start all over with Py 3? BTW Some code I have been working on in Py 3 seems to have been lost.
dgelessus last edited by
Yes, Pythonista 2 and 3 are two separate apps, because Pythonista 3 is a paid upgrade. If you want to move your code from Pythonista 2 to 3, go into the Pythonista 3 settings and turn on "Pythonista 2 Files". Then a "Pythonista 2" folder will appear in the file list, and you can move the files from Pythonista 2 into Pythonista 3.
Thanks its a very good app