Related
I would like to rotate an array about the axis that is normal to a given location on the surface of a sphere, but unsure how to go about it.
As an example, the below code creates a series of points (a dipole field line), shifts it away from the centre of a sphere and rotates to some angle in the xy plane.
At this location, I would like to rotate the field line to any angle around the axis that is normal to the sphere surface.
import numpy as np
import matplotlib.pyplot as plt
field=np.linspace(-np.pi/2,np.pi/2,100)
circle=np.linspace(0,2*np.pi,100)
theta=60*np.pi/180
r_shift=0.9
r=1.5*np.sin(field+np.pi/2)**2
x0=r*np.cos(field)+r_shift
y0=r*np.sin(field)
# rotate around y-axis
x=x0*np.cos(theta)
y=y0
fig,ax=plt.subplots()
ax.set_box_aspect(1)
ax.plot(np.cos(circle),np.sin(circle),color='k')
ax.plot(x,y)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
plt.show()
I think it could be done with Euler angles or Rodriguez' formula, but I'm not familiar enough to implement this.
I'm trying to generate a 3d plot from a few datapoints. My goal is it, to compare two different datasets and show how good they match at different points. Right now I'm working on the first surface and my supervisor is unhappy with the visualization.
I use the following code at the moment:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
# Create the figure and axes objects
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Define the data for the first surface
x1 = [25,35,40,45,50,55,60]
y1 = [1300,4000,5000,5400]
z1 = [8.06,5.81,5.10,4.55,4.1,3.01,2.51,6.46,4.93,4.4,4.03,3.15,2.83,2.4,5.95,4.6,3.87,3.19,2.91,2.7,2.36,5.69,4.29,3.63,3.1,2.85,2.65,2.33]
# Convert the z1 data to 2D arrays
x, y = np.meshgrid(x1, y1)
z1 = np.array(z1).reshape(x.shape)
# Plot the first surface
ax.plot_surface(x, y, z1)
# Show the plot
plt.show()
And as a result the following plot is displayed:
enter image description here
My supervisor wants it to look something like this:
enter image description here
Note that this is a completly different diagram with a different dataset and also different axes.
I wonder if it is even possible to generate such a high resolution of a grid with so few datapoints.
Has is something to do with the way the points are connected in the diagram? In my diagram it looks like a linear interpolation. Is it possible to influence the interpolation?
I would be glad if anyone has an idea and is able to help me.
Thanks, and all the best!
I have a data-set of 3D points (x,y,z) projected onto a plane and i'd like to transform them into a simple 2D plot by looking at the points from an orthogonal direction to that plane. Any python explanation are much appreciated!
You can use this :
import pylab
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
x = y = z = [1, 2, 3]
sc = ax.scatter(x,y,z)
#####################
x2, y2, _ = proj3d.proj_transform(1, 1, 1, ax.get_proj())
print x2, y2 # project 3d data space to 2d data space
print ax.transData.transform((x2, y2)) # convert 2d space to screen space
#####################
def on_motion(e):
# move your mouse to (1,1,1), and e.xdata, e.ydata will be the same as x2, y2
print e.x, e.y, e.xdata, e.ydata
fig.canvas.mpl_connect('motion_notify_event', on_motion)
pylab.show()
Depending on how the data were projected, and how perfectly planar they are, one way could be to use a PCA
Example on a fabricated dataset (please, next time, provide such a fabricated dataset. It is better, because I did mine by surmising how yours may look).
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.decomposition import PCA
# Fabrication of the dataset : a bunch of 10000 random points distributed in a ring (like Saturn's rings)
radius=np.random.uniform(100,200,(10000,))
angles=np.random.uniform(0,2*np.pi,(10000,))
x1=radius*np.cos(angles)
y1=radius*np.sin(angles)
# Just to see how the original data look like
plt.figure()
plt.scatter(x1,y1)
plt.show()
# Note that those are "secret" data.
# We are not supposed to know how the 2D data are
# What we will work on are 3D data fabricated as follows:
# generate 3D data, that are those, on a plane
# with some noise
# we just use vectors (1,1,1) and (1,-1,1) as generator of the plane.
# So a planar point (x,y) will be sent do a
# 3d point x(1,1,1)+y(1,-1,1).
# Plus some noise
xyz=x1.reshape(-1,1)#[[1,1,1]] + y1.reshape(-1,1)#[[1,-1,1]] + np.random.normal(0,2,(10000,3))
fig=plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(xyz[:,0],xyz[:,1],xyz[:,2])
plt.show()
So, that is the data we will work on. 3D data that are mainly on a plane. We want the 2D dataset of that plane. But, of course, we can't access to x1, y1, since we pretend to know only of xyz
pca=PCA(n_components=2)
xy=pca.fit_transform(xyz)
# xy are the projection on the best possible plane
# of xyz data.
plt.figure()
plt.scatter(xy[:,0], xy[:,1])
plt.show()
You may also know what are the axis of this plane
pca.components_
#array([[-0.70692992, 0.02117576, -0.70696653],
# [ 0.01489184, 0.99977576, 0.01505521]])
So, roughly (-√2/2,0,-√2/2) and (0,1,0).
Not the same axis we've used (1,1,1) and (1,-1,1).
But see that one basis generate the other : (1,1,1) is -√2(-√2/2,0,-√2/2)+(0,1,0). And (1,-1,1) is -√2(-√2/2,0,-√2/2)-(0,1,0).
Or, the other way round : (-√2/2,0,-√2/2) = -√2/4(1,1,1)-√2/4(1,-1,1); (0,1,0)=½(1,1,1)-½(1,-1,1)
So, it is the same plane. Just not the same axis in that plane, but that is normal: nothing in a 3D data of planar points can tell how the 3D data were built.
Note that this method is well suited if 3D data are a little bit noisy. If not, you could achieve the same result with simple Gram-Schmidt method. Choosing extreme points
Starting from another xyz without the noise
# same xyz but without the noise
xyzClean=x1.reshape(-1,1)#[[1,1,1]] + y1.reshape(-1,1)#[[1,-1,1]]
# One (randomly chosen. So why not the 1st) point
# of the dataset
m0=xyzClean[0]
# Choose m1 so that it is further from m0 as possible
dm0=((xyzClean-m0)**2).sum(axis=1)
idx1=np.argmax(dm0)
m1=xyzClean[idx1]
# Choose m2 as far as both m0 and m1 as possible
dm1=((xyzClean-m1)**2).sum(axis=1)
idx2=np.argmax(np.minimum(dm0,dm1))
m2=xyzClean[idx2]
# Gram-Schmidt process to get two orthogonal
# vectors from origin m0
v1=m1-m0
v1=v1/np.sqrt(v1#v1) # normalization
v2=(m2-m0) - ((m2-m0)#v1)*v1
v2=v2/np.sqrt(v2#v2)
# v1=[ 0.70700705, -0.01679433, 0.70700705]
# v2=[0.01187538, 0.99985897, 0.01187538]
# Note that 1.39721978*v1+1.02360973*v2
= (1,1,1)
# And 1.43080844*v1-0.9761082*v2 = (1,-1,1)
# So, again, same plane
# The advantage of having orthogonal basis, is that
# projection on this basis is now easy
projDataV1 = xyzClean#v1
projDataV2 = xyzClean#v2
plt.figure()
plt.scatter(projDataV1, projDataV2)
plt.show()
That second method is well suited if you have no noise, and your 3d data are exactly planar.
Not that the 1st one wouldn't work (it would work anytime). But this one is faster. And could be even faster, if instead of selecting m0, m1 and m2 as far as possible from each other, I had just selected them "far enough" from each other.
I have two 3-D arrays of ground penetrating radar data. Each array is basically a collection of time-lapse 2-D images, where time is increasing along the third dimension. I want to create a 3-D plot which intersects a 2-D image from each array.
I'm essentially trying to create a fence plot. Some examples of this type of plot are found on these sites:
http://www.geogiga.com/images/products/seismapper_3d_seismic_color.gif
http://www.usna.edu/Users/oceano/pguth/website/so461web/seismic_refl/fence.png
I typically use imshow to individually display the 2-D images for analysis. However, my research into the functionality of imshow suggests it doesn't work with the 3D axes. Is there some way around this? Or is there another plotting function which could replicate imshow functionality but can be combined with 3D axes?
There might be better ways, but at least you can always make a planar mesh and color it:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# create a 21 x 21 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,21), np.linspace(0,1,21))
# create some dummy data (20 x 20) for the image
data = np.random.random((20, 20))
# create vertices for a rotated mesh (3D rotation matrix)
X = np.sqrt(1./3) * xx + np.sqrt(1./3) * yy
Y = -np.sqrt(1./3) * xx + np.sqrt(1./3) * yy
Z = np.sqrt(1./3) * xx - np.sqrt(1./3) * yy
# create the figure
fig = plt.figure()
# show the reference image
ax1 = fig.add_subplot(121)
ax1.imshow(data, cmap=plt.cm.BrBG, interpolation='nearest', origin='lower', extent=[0,1,0,1])
# show the 3D rotated projection
ax2 = fig.add_subplot(122, projection='3d')
ax2.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.cm.BrBG(data), shade=False)
This creates:
(Please note, I was not very careful with the rotation matrix, you will have to create your own projection. It might really be a good idea to use a real rotation matrix.)
Just note that there is a slight problem with the fence poles and fences, i.e. the grid has one more vertex compared to the number of patches.
The approach above is not very efficient if you have high-resolution images. It may not even be useful with them. Then the other possibility is to use a backend which supports affine image transforms. Unfortunately, you will then have to calculate the transforms yourself. It is not hideously difficult, but still a bit clumsy, and then you do not get a real 3D image which could be rotated around, etc.
For this approach, see http://matplotlib.org/examples/api/demo_affine_image.html
Alternateively, you can use OpenCV and its cv2.warpAffine function to warp your image before showing it with imshow. If you fill the surroundings with transparent color, you can then layer images to get a result which looks like your example iamge.
Just to give you an idea of the possibilities of plot_surface, I tried to warp Lena around a semi-cylinder:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# create a 513 x 513 vertex mesh
xx, yy = np.meshgrid(np.linspace(0,1,513), np.linspace(0,1,513))
# create vertices for a rotated mesh (3D rotation matrix)
theta = np.pi*xx
X = np.cos(theta)
Y = np.sin(theta)
Z = yy
# create the figure
fig = plt.figure()
# show the 3D rotated projection
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=plt.imread('/tmp/lena.jpg')/255., shade=False)
She indeed bends well, but all operations on the image are quite slow:
If you're happy to contemplate using a different plotting library (ie not matplotlib) then it might be worth considering mayavi / tvtk (although the learning curve is a little steep). The closest I've seen to what you want is the scalar cut planes in
http://wiki.scipy.org/Cookbook/MayaVi/Examples
The bulk of the documentation is at:
http://docs.enthought.com/mayavi/mayavi/index.html
There is no way of doing this with matplotlib. #DrV's answer is an approximation. Matplotlib does not actually show each individual pixel of the original image but some rescaled image. rstride and cstride allow you to help specify how the image gets scaled, however, the output will not be the exact image.
I'd like to plot a sine wave on a circle: that is, the circle is in the x,y-plane and the sine wave wraps around it perpendicular to that plane (sticking up the z-axis). I can do this, but when I try to fill the areas between the circle and the sine wave with a polygon (ie paint on the surface of the imaginary cylinder on which my sine wave lives), I can't get it quite right - matplotlib seems to XOR the regions that overlap in a view of the plot instead of giving me a view in which the ones in front occlude those behind.
Here's the relevant bit of my code:
fig = plt.figure()
ax = fig.gca(projection='3d')
ax._axis3don = False
theta = np.linspace(0., 2 * np.pi, 1000)
r = 1.
x = r * np.sin(theta)
y = r * np.cos(theta)
sinez = N * np.sin(theta * m)
ax.plot(x, y, sinez, color='r')
xv = np.append(x, x[::-1])
yv = np.append(y, y[::-1])
zv = np.append(sinez, np.zeros(n))
verts = [zip(xv,yv,zrev),]
poly = Poly3DCollection(verts, facecolors = [cc('r'), cc('b')],
edgecolor='None')
poly.set_alpha(0.7)
ax.add_collection3d(poly)
Here's what it looks like:
matplotlib's main reason for existence is 2D plotting, the 3D stuff is just some clever transforms and can be buggy/hacky. One of the inherent limitations is that matplotlib draws in layers, so it has no notion of 'in front' or 'behind', it only knows the order in which in draws the curves to the canvas (which is confusingly called z-order).
If you want to get this to look right with out re-writing the 3D code, split the sine wave up into pieces and make sure you set the z-order right by hand (see How to draw intersecting planes? for a simpler version of this), but you won't be able to rotate the image.
If you need real 3D, I would suggest looking into mayavi from enthought which is OpenGL based.
In the docs, the devs claim that Poly3DCollection
does a bit of magic with the _facecolors and _edgecolors properties.
which I believe is the XOR effect that you can see here, and looking at the code it's the function do_3d_projection that seems to be doing the magic.
As I see it, you could either subclass Poly3DCollection and rewrite do_3d_projection to get what you want, or maybe think of another way to plot this (perhaps treating the sinusoid and circle as separate objects somehow).