Here is an answer that lets one plot a plane using matplotlib, but if one uses the vector [1, 0, 0], nothing gets plotted! This makes sense, because of the way the code is set up (meshgrid is on X-Y plane, and then Z points determine the surface.
So, how can I plot the X = 0 plane using matplotlib?
This is less generic than the example that you linked, but it does the trick:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
yy, zz = np.meshgrid(range(2), range(2))
xx = yy*0
ax = plt.subplot(projection='3d')
ax.plot_surface(xx, yy, zz)
plt.show()
Related
I'm trying to graph a 3d mesh surface with matplotlib and constrain the limits of the graph. The X and Y axes are correctly constrained, but there is overflow in the Z-Axis.
What am I missing? Here's my code:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(10,10))
x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
X,Y = np.meshgrid(x,y)
def f(x,y):
return x**2 + 3*y
Z = f(X,Y)
ax = plt.axes(projection = '3d')
ax.plot_surface(X,Y,Z,cmap='viridis')
ax.title.set_text("z=x**2+3y")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_zlim3d(zmin=-3,zmax=5)
ax.set_xlim3d(xmin=-6,xmax=6)
ax.set_ylim3d(ymin=-6,ymax=6)
plt.show()
The graph:
Edit:
When I add clipping/min/max to the Z values, the graph is a little better, but it sets z values outside the bounds to the bounds themselves. Both of the following suggestions do this. Perhaps it's because I'm on a mac?
z_tmp = np.maximum(np.minimum(5,Z),-3)
z_temp = np.clip(Z, -3, 5, None)
Your data is outside the axis boundaries. Try rotate the view and you will notice.
z = x**2 + 3*y
If you want to only show a defined area of the data you could add a max() min() limitation on the Z data to exclude the data outside your wanted limitations.
Z = f(X,Y)
z_tmp = np.maximum(np.minimum(5,Z),-3)
ax = plt.axes(projection = '3d')
ax.plot_surface(X,Y,z_tmp,cmap='viridis')
I'm not sure the matplotlib behaves as it should in your default case.
I am trying to visualize a 3D parameter space where I am confident about my parameters with voxels, and where my estimated parameters are with scatters. The goal is to clearly see whether or not the parameters are within this space.
Even though the voxels are not see-through, the scatters are plotted 'on top' of the voxel:
I would like the voxel to at least hide the scatters behind it, but preferably I would like to make the voxels see-through (e.g. alpha 0.5), with the scatters changing color depending on how much voxel the light has travelled through. Is something like this even possible with matplotlib?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x, y, z = np.random.random((3, 100)) * 5
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x, y, z)
cube = np.mgrid[0:1:0.2,0:1:0.2] > 0.5
ax.voxels(cube, edgecolor='k', linewidth=0.)
As #tmdavison pointed out, matplotlib is quite limited for these kind of things. Here is my example using mayavi instead.
import mayavi.mlab
import np as np
x, y, z = np.random.random((3, 100)) * 5
xx, yy, zz = np.where(np.mgrid[0:1:0.2,0:1:0.2] > 0.5)
nodes = mayavi.mlab.points3d(x, y, z,
color=(1, 0, 0),
scale_factor=0.1)
mayavi.mlab.points3d(xx, yy, zz,
mode="cube",
color=(0, 1, 0),
scale_factor=1)
mayavi.mlab.show()
How do I modify the xyz data of a 3d scatter plot in matplotlib for fast on-line animations? In other words where do matplotlib patchcollection3d objects save the xyz coordinates, and how do I set them? For example:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
## generate some random data
pts = np.random.uniform(0,10,(10,20,30))
plt.close('all')
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1],pts[:,2], c='m', marker='o')
What do I do next with patch_collection_instance if, for example, I want to translate all points by a random amount?
The coordinates are stored in the attribute _offsets3d. While there is a get_offsets() method and a set_offsets() method, those appear to be inherited from the 2d version and don't work properly for 3d. _offsets3d contains a tuple of x, y, and z coordinate tuples. Let's say you want to shift every point by 10 in the x direction. You'd add 10 to every number in the x-coordinate tuple, then set the _offsets3d to the new tuple.
I am not sure if this is faster than just clearing the figure and calling scatter again with new coordinates, which should have the same effect.
Example code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
import numpy as np
from copy import copy
## generate some random data
pts = np.random.uniform(0,10,(10,20,30))
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
patch_collection_instance = ax.scatter(pts[:,0],pts[:,1], pts[:,2], c='m', marker='o')
x, y, z = patch_collection_instance._offsets3d
print x
x = [i + 10 for i in x]
offsets = (x, y, z)
patches2 = copy(patch_collection_instance)
patches2._offsets3d = offsets
patches2._facecolor3d = [[0, 0, 1, 1]]
ax.add_collection3d(patches2)
plt.xlim(0, 20)
plt.show()
I have a python program that calculates angles for me and outputs them in a list.
What I would like to do is plot a stack of arrows that are unit vectors pointing in the direction of the angle. So I thought cylindrical coordinates would be best since they only have one angular coordinate.
I've tried pyplot.quiver but I don't think that can do anything in 3D, and a 3D line plot didn't work either.
Is there a way of doing this without laboriously converting each (length, height, angle) into a pair of vectors (a, b, c),(length*cos(angle), length*sin(angle), height)?
If you have a list of angles, you can easily calculate vectors associated with those angles using numpy.
import numpy as np
import matplotlib.pyplot as plt
angles = np.random.rand(100)
length = 1.
vectors_2d = np.vstack((length * np.cos(angles), length * np.sin(angles))).T
for x, y in vectors_2d:
plt.plot([0, x], [0, y])
plt.show()
If you really want it in cylindrical instead of polar coords, then
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
angles = np.random.rand(100)
length = 1.
heights = np.arange(len(angles))
vectors_3d = np.vstack((length * np.cos(angles),
length * np.sin(angles),
heights)).T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for x, y, z in vectors_3d:
ax.plot([0, x], [0, y], zs=[z, z])
plt.show()
Edit: I know how to put arrows on plots using pyplot.quiver. However, I don't think mplot3d plays nicely with quiver. Maybe someone like #tcaswell can help out with a work around. But in 2D, you can do
import numpy as np
import matplotlib.pyplot as plt
angles = np.random.rand(100)
# Define coords for arrow tails (the origin)
x0, y0 = np.zeros(100), np.zeros(100)
# Define coords for arrow tips (cos/sin)
x, y = np.cos(angles), np.sin(angles)
# in case you want colored arrows
colors = 'bgrcmyk'
colors *= colors * (len(x0) / len(colors) + 1)
plt.quiver(x0, y0, x, y, color=colors[:len(x0)], scale=1) #scale sets the length
plt.show()
Teacher in class gave this formula
w = x**2 + y**2 - z**2
and showed its 3d graphic in class seen below. How do I plot this using Matplotlib (minus the intersecting plane)? I guess first a specific value for w needs to be selected, for example 10, otherwise 3d plotting would not be possible. Then should I convert to polar coordinates because of the z**2 in the formula? I tried this and failed. Any help would be appreciated. Also, does this shape have a name?
Got it. Found some good stuff here, and following the formulas presented, I have the Python code below.
http://msenux.redwoods.edu/Math4Textbook/Plotting/ParametricSurfaces.pdf
from __future__ import division
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=plt.figaspect(1)) # Square figure
ax = fig.add_subplot(111, projection='3d')
r=1;
u=np.linspace(-2,2,200);
v=np.linspace(0,2*np.pi,60);
[u,v]=np.meshgrid(u,v);
a = 1
b = 1
c = 1
x = a*np.cosh(u)*np.cos(v)
y = b*np.cosh(u)*np.sin(v)
z = c*np.sinh(u)
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b')
plt.show()