Plot 4th dimension with Python - python
I would to know if there is the possibility to plot in four dimensions using python. In particular I would to have a tridimensional mesh X, Y, Z and f(X,Y,Z) = 1 or f(X,Y,Z) = 0.
So I need to a symbol (for example "o" or "x") for some specific point (X,Y,Z).
I don't need to a color scale.
Note that I have 100 matrices (512*512) composed by 1 or 0: so my mesh should be 512*512*100.
I hope I have been clear! Thanks.
EDIT:
This is my code:
X = np.arange(W.shape[2])
Y = np.arange(W.shape[1])
Z = np.arange(W.shape[0])
X, Y, Z = np.meshgrid(X, Y, Z)
fig = plt.figure()
ax = fig.gca(projection='3d')
for z in range(W.shape[0]):
indexes = np.where(W[z])
ax.scatter(X[indexes], Y[indexes], ???, marker='.')
ax.set_xlabel('X = columns')
ax.set_ylabel('Y = rows')
ax.set_zlabel('Z')
plt.show()
W is my tridimensional matrix, so: W[0], W[1], etc are 512x512 matrices.
My question is: what have I to write insted of ??? in my code. I know I shouldn't ask this, but I can't understand the idea.
You could create inspect the value of f(x,y,z) for layers of z to see if they are non-zero or not, and then scatterplot the function based on this.
e.g. for nz layers of (n,n) matrices, each a slice of a sphere:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
n, nz = 48, 24
x, y = np.linspace(-n//2,n//2-1,n), np.linspace(-n//2,n//2-1,n)
X, Y = np.meshgrid(x, y)
def f(x,y,z):
return (X**2 + Y**2 + (z-nz//2)**2) < (n*0.2)**2
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for z in range(nz):
layer = f(X, Y, z)
indexes = np.where(layer)
ax.scatter(X[indexes], Y[indexes], layer[indexes]*(z-nz//2), marker='.')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
For random non-zero elements of f(x,y,z):
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
n, nz = 12, 10
x, y, z = np.linspace(0,n-1,n), np.linspace(0,n-1,n), np.linspace(0,nz-1,nz)
X, Y, Z = np.meshgrid(x, y, z)
f = np.random.randint(2, size=(n,n,nz))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for z in range(nz):
indexes = np.where(f[...,z])
ax.scatter(X[indexes], Y[indexes], f[indexes]+z, marker='.')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
But with your large arrays, you may run into problems (a) with memory and the speed of the plotting and (b) being able to resolve detail in the "central" block of the plot.
Related
Contourf on the faces of a Matplotlib cube
I am trying to 'paint' the faces of a cube with a contourf function using Python Matplotlib. Is this possible? This is similar idea to what was done here but obviously I cannot use patches. Similarly, I don't think I can use add_collection3d like this as it only supports PolyCollection, LineColleciton and PatchCollection. I have been trying to use contourf on a fig.gca(projection='3d'). Toy example below. from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np plt.close('all') fig = plt.figure() ax = fig.gca(projection='3d') ############################################ # plotting the 'top' layer works okay... # ############################################ X = np.linspace(-5, 5, 43) Y = np.linspace(-5, 5, 28) X, Y = np.meshgrid(X, Y) varone=np.random.rand(75,28,43) Z=varone[0,:,:] cset = ax.contourf(X, Y, Z, zdir='z', offset=1, levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') #see [1] plt.show() ################################################# # but now trying to plot a vertical slice.... # ################################################# plt.close('all') fig = plt.figure() ax = fig.gca(projection='3d') Z=varone[::-1,:,-1] X = np.linspace(-5, 5, 28) Y = np.linspace(-5, 5, 75) X, Y = np.meshgrid(X, Y) #this 'projection' doesn't result in what I want, I really just want to rotate it cset = ax.contourf(X, Y, Z, offset=5,zdir='x', levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') #here's what it should look like.... ax=fig.add_subplot(1, 2,1) cs1=ax.contourf(X,Y,Z,levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') #see [2] plt.show() 1 From the example, the top surface comes easily: 2 But I'm not sure how to do the sides. Left side of this plot is what the section should look like (but rotated)... Open to other python approaches. The data I'm actually plotting are geophysical netcdf files.
You have to assign the data to the right axis. The zig-zag results from the fact that now you are at x = const and have your oscillation in the z-direction (from the random data, which is generated between 0 and 1). If you you assign the matrixes differently in your example, you end up with the desired result: from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np plt.close('all') fig = plt.figure() ax = fig.gca(projection='3d') X = np.linspace(-5, 5, 43) Y = np.linspace(-5, 5, 28) X, Y = np.meshgrid(X, Y) varone=np.random.rand(75,28,43) * 5.0 - 10.0 Z=varone[0,:,:] cset = [[],[],[]] # this is the example that worked for you: cset[0] = ax.contourf(X, Y, Z, zdir='z', offset=5, levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') # now, for the x-constant face, assign the contour to the x-plot-variable: cset[1] = ax.contourf(Z, Y, X, zdir='x', offset=5, levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') # likewise, for the y-constant face, assign the contour to the y-plot-variable: cset[2] = ax.contourf(X, Z, Y, zdir='y', offset=-5, levels=np.linspace(np.min(Z),np.max(Z),30),cmap='jet') # setting 3D-axis-limits: ax.set_xlim3d(-5,5) ax.set_ylim3d(-5,5) ax.set_zlim3d(-5,5) plt.show() The result looks like this:
The answer given below is not fully satisfying. Indeed, planes in x, y and z direction reproduce the same field. Hereafter, a function that allows to represent the correct field in each of the planes. import numpy as np import matplotlib.pyplot as plt def plot_cube_faces(arr, ax): """ External faces representation of a 3D array with matplotlib Parameters ---------- arr: numpy.ndarray() 3D array to handle ax: Axes3D object Axis to work with """ x0 = np.arange(arr.shape[0]) y0 = np.arange(arr.shape[1]) z0 = np.arange(arr.shape[2]) x, y, z = np.meshgrid(x0, y0, z0) xmax, ymax, zmax = max(x0), max(y0), max(z0) vmin, vmax = np.min(arr), np.max(arr) ax.contourf(x[:, :, 0], y[:, :, 0], arr[:, :, -1].T, zdir='z', offset=zmax, vmin=vmin, vmax=vmax) ax.contourf(x[0, :, :].T, arr[:, 0, :].T, z[0, :, :].T, zdir='y', offset=0, vmin=vmin, vmax=vmax) ax.contourf(arr[-1, :, :].T, y[:, 0, :].T, z[:, 0, :].T, zdir='x', offset=xmax, vmin=vmin, vmax=vmax) x0 = np.arange(30) y0 = np.arange(20) z0 = np.arange(10) x, y, z = np.meshgrid(x0, y0, z0) arr = (x + y + z) // 10 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_cube_faces(arr, ax) plt.show()
How do you create a 3D surface plot with missing values matplotlib?
I am trying to create a 3D surface energy diagram where an x,y position on a grid contains an associated z level. The issue is that the grid is not uniform (ie, there is not a z component for every x,y position). Is there a way to refrain from plotting those values by calling them NaN in the corresponding position in the array? Here is what I have tried so far: import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D import pylab from matplotlib import cm #Z levels energ = np.array([0,3.5,1,-0.3,-1.5,-2,-3.4,-4.8]) #function for getting x,y associated z values? def fun(x,y,array): return array[x] #arrays for grid x = np.arange(0,7,0.5) y = np.arange(0,7,0.5) #create grid X, Y = np.meshgrid(x,y) zs = np.array([fun(x,y,energ) for x in zip(np.ravel(X))]) Z = zs.reshape(X.shape) plt3d = plt.figure().gca(projection='3d') #gradients now with respect to x and y, but ideally with respect to z only Gx, Gz = np.gradient(X * Y) G = (Gx ** 2 + Gz ** 2) ** .5 # gradient magnitude N = G / G.max() # normalize 0..1 plt3d.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=cm.jet(N), edgecolor='k', linewidth=0, antialiased=False, shade=False) plt.show() I cannot post image here of this plot but if you run the code you will see it But I would like to not plot certain x,y pairs, so the figure should triangle downward to the minimum. Can this be accomplished by using nan values? Also would like spacing between each level, to be connected by lines. n = np.NAN #energ represents the z levels, so the overall figure should look like a triangle. energ = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,2.6,n,2.97,n,2.6,n,2.97,n,2.6,n,3.58,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,1.09,n,1.23,n,1.09,n,1.23,n,1.7,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,-0.65,n,-0.28,n,-0.65,n,0.33,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,-2.16,n,-2.02,n,-1.55,n,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,-3.9,n,-2.92,n,n,n,n,n,],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,n,-4.8,n,n,n,n,n,n,]]) plt3d = plt.figure().gca(projection='3d') Gx, Gz = np.gradient(X * energ) # gradients with respect to x and z G = (Gx ** 2 + Gz ** 2) ** .5 # gradient magnitude N = G / G.max() # normalize 0..1 x = np.arange(0,13,1) y = np.arange(0,13,1) X, Y = np.meshgrid(x,y) #but the shapes don't seem to match up plt3d.plot_surface(X, Y, energ, rstride=1, cstride=1, facecolors=cm.jet(N), edgecolor='k', linewidth=0, antialiased=False, shade=False ) Using masked arrays generates the following error: local Python[7155] : void CGPathCloseSubpath(CGMutablePathRef): no current point. n = np.NAN energ = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,2.6,n,2.97,n,2.6,n,2.97,n,2.6,n,3.58,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,1.09,n,1.23,n,1.09,n,1.23,n,1.7,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,-0.65,n,-0.28,n,-0.65,n,0.33,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,-2.16,n,-2.02,n,-1.55,n,n,n,n],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,-3.9,n,-2.92,n,n,n,n,n,],[n,n,n,n,n,n,n,n,n,n,n,n,n],[n,n,n,n,n,n,-4.8,n,n,n,n,n,n,]]) x = np.arange(0,13,1) y = np.arange(0,13,1) X, Y = np.meshgrid(x,y) #create masked arrays mX = ma.masked_array(X, mask=[[0,0,0,0,0,0,0,0,0,0,0,0,0],[1,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1,1,1,1,1,1,1],[1,1,1,1,1,1,0,1,1,1,1,1,1]]) plt3d = plt.figure().gca(projection='3d') plt3d.plot_surface(mX, mY, m_energ, rstride=1, cstride=1, edgecolor='k', linewidth=0, antialiased=False, shade=False) plt.show()
I was playing around with the code from this forum post, and I was able to make the graph have missing values. You can try the code yourself! I got it to work using float("nan") for the missing values. import plotly.graph_objects as go import numpy as np x = np.arange(0.1,1.1,0.1) y = np.linspace(-np.pi,np.pi,10) #print(x) #print(y) X,Y = np.meshgrid(x,y) #print(X) #print(Y) result = [] for i,j in zip(X,Y): result.append(np.log(i)+np.sin(j)) result[0][0] = float("nan") upper_bound = np.array(result)+1 lower_bound = np.array(result)-1 fig = go.Figure(data=[ go.Surface(z=result), go.Surface(z=upper_bound, showscale=False, opacity=0.3,colorscale='purp'), go.Surface(z=lower_bound, showscale=False, opacity=0.3,colorscale='purp')]) fig.show()
Plotting function of 3 dimensions over given domain with matplotlib
I am trying to visualize a function of 3 parameters over a cube in R^3 to get an idea of the smoothness of the function. An example of this problem is shown in the sample code below %pylab from mpl_toolkits.mplot3d import Axes3D import itertools x = np.linspace(0,10,50) y = np.linspace(0,15,50) z = np.linspace(0,8,50) points = [] for element in itertools.product(x, y, z): points.append(element) def f(vals): return np.cos(vals[0]) + np.sin(vals[1]) + vals[2]**0.5 fxyz = map(f, points) xi, yi, zi = zip(*points) fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111, projection='3d') ax.scatter(xi, yi, zi, c=fxyz, alpha=0.5) plt.show() The problem with this approach is that the inside of the cube cannot be visualized. Is there a better way to graph a function over some dense subset of R^3?
As #HYRY and #nicoguaro suggested in the comments above, Mayavi is much better suited for this type of work. There is a good set of examples here that I used for reference. Here is what I came up with import numpy as np from mayavi import mlab x = np.linspace(0,10,50) y = np.linspace(0,15,50) z = np.linspace(0,8,50) X, Y, Z = np.meshgrid(x, y, z) s = np.cos(X) + np.sin(Y) + Z**0.5 b1 = np.percentile(s, 20) b2 = np.percentile(s, 80) mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=b1, vmax=b2) mlab.axes() mlab.show() After which I rotated the figure to desired angles with the GUI and saved desired views
Surface plot with different number of points in x, y and z axis
I'm trying to plot a surface plot with 549 points. The x axis has 51 points and y-axis has 9 points. and the z-axis has 549 points. For example: fig = plt.figure() X = list(xrange(0,51)) Y = list(xrange(0,9)) Z = list(xrange(0,459)) print len(X) print len(Y) print len(Z) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X,Y,Z, cmap=plt.cm.jet, cstride=1, rstride=1) plt.savefig('graph-1' + '.jpg', bbox_inches='tight', pad_inches=0.2,dpi=100) plt.clf() And I try to plot it I get the following error: ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 1. How do we plot when we have different axis lengths? The 3-tuple looks like this: for a in range(0,len(X)): for b in range(0, len(Y)): for c in range(0, len(Z)): print (a,b,c)
Thanks to #Andrey import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import random def fun(x, y): return test[x][y] global test fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = list(xrange(0,9)) y = list(xrange(0,51)) test = [[a for a in range(0, len(y)] for b in range(0, len(x))] X, Y = np.meshgrid(x, y) zs = np.array([fun(x,y) for x,y in zip(np.ravel(X), np.ravel(Y))]) Z = zs.reshape(X.shape) ax.plot_surface(X, Y, Z) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
Edit: Ok, looking at this again, you want to plot 459 point, on a grid. Using this loop: for a in range(0,len(X)): for b in range(0, len(Y)): for c in range(0, len(Z)): print (a,b,c) will give you many more than 459 points, it will give you 51*9*459 points. try this: import itertools X2,Y2=zip(*list(itertools.product(X,Y))) This will create all possible combinations of x,y then you should be able to plot (X2,Y2,Z). len(X2) and len(Y2) are both 459
Contour graph in python
How would I make a countour grid in python using matplotlib.pyplot, where the grid is one colour where the z variable is below zero and another when z is equal to or larger than zero? I'm not very familiar with matplotlib so if anyone can give me a simple way of doing this, that would be great. So far I have: x= np.arange(0,361) y= np.arange(0,91) X,Y = np.meshgrid(x,y) area = funcarea(L,D,H,W,X,Y) #L,D,H and W are all constants defined elsewhere. plt.figure() plt.contourf(X,Y,area) plt.show()
You can do this using the levels keyword in contourf. import numpy as np import matplotlib.pyplot as plt fig, axs = plt.subplots(1,2) x = np.linspace(0, 1, 100) X, Y = np.meshgrid(x, x) Z = np.sin(X)*np.sin(Y) levels = np.linspace(-1, 1, 40) zdata = np.sin(8*X)*np.sin(8*Y) cs = axs[0].contourf(X, Y, zdata, levels=levels) fig.colorbar(cs, ax=axs[0], format="%.2f") cs = axs[1].contourf(X, Y, zdata, levels=[-1,0,1]) fig.colorbar(cs, ax=axs[1]) plt.show() You can change the colors by choosing and different colormap; using vmin, vmax; etc.