I am trying to create a code to produce a failure mode plot for honeycomb beam structures as seen in the image below:
(from M. Sadighi et al. 2009)
There are distinct formulas to calculate the load at which the failure will occur, and given the material/beam parameters, the lowest value is the most likely failure to occur.
I have a triple nested for loop running ranges of values for t, L, and rho. Mesh grid from numpy and the contour plot from matplotlib seemed logical, but throw an error for the z input requiring a 2D array.
I thought that maybe each failure type could be encoded into a value (i.e. 1 for core crush, 2 for indentation, etc.) and you could scan the x and y values to store where the change in failure type happen, but I still don't know how to get that into a plot.
The closest thing I have found so far can be seen here The moons dataset and decision surface graphics in a Jupyter environment where the plot is split into different colored regions.
How can this be plotted?
P.S. I know that you are suppose to attach code, however in this case, a ton of variables are need to do the calculations and would be difficult to pass around.
I am trying to rephrase the question. Let's say we have functions of two coordinates: f1(x, y), f2(x, y)... They correspond for instance at the critical stress for each failure mode. Variables x, y are two of the parameters.
For a given (x, y) the failure mode is obtain by using argmin( f1(x, y), f2(x, y), ... ) i.e. the failure mode for which the critical stress is minimal
Here is the simple solution I could think of to obtain a map of the failure modes:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
# Mesh the parameters space
x = np.linspace(0, 2, 35)
y = np.linspace(0, 1, 24)
x_grid, y_grid = np.meshgrid(x, y)
# Compute the functions for each point on the mesh
f1 = x_grid + y_grid
f2 = 0.5 + x_grid**2
f3 = 1 + y_grid**2
# Identify which function is minimal for each point
failure_mode = np.argmin([f1, f2, f3], axis=0)
# Graph
discrete_colormap = ListedColormap(['gold', 'darkorange', 'mediumseagreen'])
plt.pcolormesh(x, y, failure_mode, cmap=discrete_colormap);
cbar = plt.colorbar();
cbar.set_label('failure mode');
cbar.set_ticks(np.arange(np.max(failure_mode)+1));
plt.xlabel('x'); plt.ylabel('y');
which gives:
See for example this answer for discrete colormap.
Here is a solution to plot the contour of each zone:
The contour of the zone i is defined as the points (x, y) such that f_i(x, y) is equal to the minimum of all the remaining functions i.e. min( f_j(x, y) for i != j ). We could use several contour plots with surfaces equal to f_i(x, y) - min( f_j(x, y) for i!=j ). The level of the zone boundary is zero.
import numpy as np
import matplotlib.pyplot as plt
# Mesh the parameters space
x = np.linspace(0, 2, 35)
y = np.linspace(0, 1, 24)
x_grid, y_grid = np.meshgrid(x, y)
# List of functions evaluated for each point of the mesh
f_grid = [x_grid + y_grid,
0.5 + x_grid**2,
1 + y_grid**2]
# Identify which function is minimal for each point
failure_mode = np.argmin(f_grid, axis=0)
# this part is for the background
critical_stress = np.min(f_grid, axis=0)
plt.pcolormesh(x, y, critical_stress, shading='Gouraud')
cbar = plt.colorbar();
cbar.set_label('critical stress');
# Plot the contour of each zone
for i in range(len(f_grid)):
other_functions = [f_j for j, f_j in enumerate(f_grid) if i != j]
level_surface = f_grid[i] - np.min(other_functions, axis=0)
plt.contour(x, y, level_surface,
levels=[0, ],
linewidths=2, colors='black');
# label
barycentre_x = np.mean(x_grid[failure_mode==i])
barycentre_y = np.mean(y_grid[failure_mode==i])
plt.text(barycentre_x, barycentre_y, 'mode %i' % i)
plt.xlabel('x'); plt.ylabel('y');
the graph is:
I have a contour map and I want to make a deformation of all the contour lines, where the contour of level 0.5 will be deformed around the blue point situated in his line and then pass on the blue point on the contour of level 1, and so on.
Original map :
Deformed map :
I think there are two steps, the first one is the delete some parts of the map and the second is to redraw the contour map.
I think i have to iterate through the contour map like this:
CS = plt.contour(X, Y, Z)
for level in CS.collections:
for kp, path in list(enumerate(level.get_paths())):
But I have no idea how to use kp and path
Any tips for doing this would be appreciated!
Here is an example on how you could change the contour plot to achieve the intended deformation.
It generates some data x,y,z which should later be modified. Then it specifies a deformation function, which when multiplied to z deforms the data in the desired way. This deformation function takes the x and y data as input as well as the angle of the line along which to perform the deformation and a width (spread) of the deformation. Finally a parameter i is used for stearing the degree of deformation (i.e. i=0 means no deformation). Of course you can use any other function to deform your contour.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation
#### generate some x,y,z data ####
r = np.linspace(0,6, num=100)
phi = np.linspace(0, 2*np.pi, num=200)
R, Phi = np.meshgrid(r,phi)
x = R*np.cos(Phi)
y = R*np.sin(Phi)
z = R
##################################
fig, ax=plt.subplots()
ax.set_aspect("equal")
def update(i):
ax.clear()
f = lambda x,y, offs, width, i: 1-i*np.exp(-(np.arctan2(x,y)-offs)**2/width)
z_deformed = z*f(x,y, np.pi/4, 1./8., i=i)
ax.contour(x,y,z_deformed, 10, linewidths=4)
ax.contourf(x,y,z_deformed, 10, alpha=0.3)
ax.set_xlim([-4,4])
ax.set_ylim([-4,4])
update(0) #plot the original data
anipath = 0.5*np.sin(np.linspace(0, np.pi, num=20))**2
ani = matplotlib.animation.FuncAnimation(fig, update, frames=anipath, interval = 100)
plt.show()
Of course you can use other shapes of deformation. E.g. to get a triangular shape use
f = lambda x, A, a, b: A*(1.-np.abs((x-b)/a))*(np.abs((x-b)) < a )
z_deformed = z - f(np.arctan2(x,y), i, 1./8., np.pi/4 )
Suppose you have a 2D curve, given by e.g.:
from matplotlib import pylab
t = numpy.linspace(-1, 1, 21)
z = -t**2
pylab.plot(t, z)
which produces
I would like to perform a revolution to achieve a 3d plot (see http://reference.wolfram.com/mathematica/ref/RevolutionPlot3D.html). Plotting a 3d surface is not the problem, but it does not produce the result I'm expecting:
How can I perform a rotation of this blue curve in the 3d plot ?
Your plot on your figure seems to use cartesian grid. There is some examples on the matplotlib website of 3D cylindrical functions like Z = f(R) (here: http://matplotlib.org/examples/mplot3d/surface3d_radial_demo.html).
Is that what you looking for ?
Below is what I get with your function Z = -R**2 :
And to add cut off to your function, use the following example:
(matplotlib 1.2.0 required)
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = -(abs(X) + abs(Y))
## 1) Initial surface
# Flatten mesh arrays, necessary for plot_trisurf function
X = X.flatten()
Y = Y.flatten()
Z = Z.flatten()
# Plot initial 3D surface with triangles (more flexible than quad)
#surfi = ax.plot_trisurf(X, Y, Z, cmap=cm.jet, linewidth=0.2)
## 2) Cut off
# Get desired values indexes
cut_idx = np.where(Z > -5)
# Apply the "cut off"
Xc = X[cut_idx]
Yc = Y[cut_idx]
Zc = Z[cut_idx]
# Plot the new surface (it would be impossible with quad grid)
surfc = ax.plot_trisurf(Xc, Yc, Zc, cmap=cm.jet, linewidth=0.2)
# You can force limit if you want to compare both graphs...
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
ax.set_zlim(-10,0)
plt.show()
Result for surfi:
and surfc:
I'd like to plot implicit equation F(x,y,z) = 0 in 3D. Is it possible in Matplotlib?
You can trick matplotlib into plotting implicit equations in 3D. Just make a one-level contour plot of the equation for each z value within the desired limits. You can repeat the process along the y and z axes as well for a more solid-looking shape.
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
def plot_implicit(fn, bbox=(-2.5,2.5)):
''' create a plot of an implicit function
fn ...implicit function (plot where fn==0)
bbox ..the x,y,and z limits of plotted interval'''
xmin, xmax, ymin, ymax, zmin, zmax = bbox*3
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
A = np.linspace(xmin, xmax, 100) # resolution of the contour
B = np.linspace(xmin, xmax, 15) # number of slices
A1,A2 = np.meshgrid(A,A) # grid on which the contour is plotted
for z in B: # plot contours in the XY plane
X,Y = A1,A2
Z = fn(X,Y,z)
cset = ax.contour(X, Y, Z+z, [z], zdir='z')
# [z] defines the only level to plot for this contour for this value of z
for y in B: # plot contours in the XZ plane
X,Z = A1,A2
Y = fn(X,y,Z)
cset = ax.contour(X, Y+y, Z, [y], zdir='y')
for x in B: # plot contours in the YZ plane
Y,Z = A1,A2
X = fn(x,Y,Z)
cset = ax.contour(X+x, Y, Z, [x], zdir='x')
# must set plot limits because the contour will likely extend
# way beyond the displayed level. Otherwise matplotlib extends the plot limits
# to encompass all values in the contour.
ax.set_zlim3d(zmin,zmax)
ax.set_xlim3d(xmin,xmax)
ax.set_ylim3d(ymin,ymax)
plt.show()
Here's the plot of the Goursat Tangle:
def goursat_tangle(x,y,z):
a,b,c = 0.0,-5.0,11.8
return x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
plot_implicit(goursat_tangle)
You can make it easier to visualize by adding depth cues with creative colormapping:
Here's how the OP's plot looks:
def hyp_part1(x,y,z):
return -(x**2) - (y**2) + (z**2) - 1
plot_implicit(hyp_part1, bbox=(-100.,100.))
Bonus: You can use python to functionally combine these implicit functions:
def sphere(x,y,z):
return x**2 + y**2 + z**2 - 2.0**2
def translate(fn,x,y,z):
return lambda a,b,c: fn(x-a,y-b,z-c)
def union(*fns):
return lambda x,y,z: np.min(
[fn(x,y,z) for fn in fns], 0)
def intersect(*fns):
return lambda x,y,z: np.max(
[fn(x,y,z) for fn in fns], 0)
def subtract(fn1, fn2):
return intersect(fn1, lambda *args:-fn2(*args))
plot_implicit(union(sphere,translate(sphere, 1.,1.,1.)), (-2.,3.))
Update: I finally have found an easy way to render 3D implicit surface with matplotlib and scikit-image, see my other answer. I left this one for whom is interested in plotting parametric 3D surfaces.
Motivation
Late answer, I just needed to do the same and I found another way to do it at some extent. So I am sharing this another perspective.
This post does not answer: (1) How to plot any implicit function F(x,y,z)=0? But does answer: (2) How to plot parametric surfaces (not all implicit functions, but some of them) using mesh with matplotlib?
#Paul's method has the advantage to be non parametric, therefore we can plot almost anything we want using contour method on each axe, it fully addresses (1). But matplotlib cannot easily build a mesh from this method, so we cannot directly get a surface from it, instead we get plane curves in all directions. This is what motivated my answer, I wanted to address (2).
Rendering mesh
If we are able to parametrize (this may be hard or impossible), with at most 2 parameters, the surface we want to plot then we can plot it with matplotlib.plot_trisurf method.
That is, from an implicit equation F(x,y,z)=0, if we are able to get a parametric system S={x=f(u,v), y=g(u,v), z=h(u,v)} then we can plot it easily with matplotlib without having to resort to contour.
Then, rendering such a 3D surface boils down to:
# Render:
ax = plt.axes(projection='3d')
ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap='jet', antialiased=True)
Where (x, y, z) are vectors (not meshgrid, see ravel) functionally computed from parameters (u, v) and triangles parameter is a Triangulation derived from (u,v) parameters to shoulder the mesh construction.
Imports
Required imports are:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.tri import Triangulation
Some surfaces
Lets parametrize some surfaces...
Sphere
# Parameters:
theta = np.linspace(0, 2*np.pi, 20)
phi = np.linspace(0, np.pi, 20)
theta, phi = np.meshgrid(theta, phi)
rho = 1
# Parametrization:
x = np.ravel(rho*np.cos(theta)*np.sin(phi))
y = np.ravel(rho*np.sin(theta)*np.sin(phi))
z = np.ravel(rho*np.cos(phi))
# Triangulation:
tri = Triangulation(np.ravel(theta), np.ravel(phi))
Cone
theta = np.linspace(0, 2*np.pi, 20)
rho = np.linspace(-2, 2, 20)
theta, rho = np.meshgrid(theta, rho)
x = np.ravel(rho*np.cos(theta))
y = np.ravel(rho*np.sin(theta))
z = np.ravel(rho)
tri = Triangulation(np.ravel(theta), np.ravel(rho))
Torus
a, c = 1, 4
u = np.linspace(0, 2*np.pi, 20)
v = u.copy()
u, v = np.meshgrid(u, v)
x = np.ravel((c + a*np.cos(v))*np.cos(u))
y = np.ravel((c + a*np.cos(v))*np.sin(u))
z = np.ravel(a*np.sin(v))
tri = Triangulation(np.ravel(u), np.ravel(v))
Möbius Strip
u = np.linspace(0, 2*np.pi, 20)
v = np.linspace(-1, 1, 20)
u, v = np.meshgrid(u, v)
x = np.ravel((2 + (v/2)*np.cos(u/2))*np.cos(u))
y = np.ravel((2 + (v/2)*np.cos(u/2))*np.sin(u))
z = np.ravel(v/2*np.sin(u/2))
tri = Triangulation(np.ravel(u), np.ravel(v))
Limitation
Most of the time, Triangulation is required in order to coordinate mesh construction of plot_trisurf method, and this object only accepts two parameters, so we are limited to 2D parametric surfaces. It is unlikely we could represent the Goursat Tangle with this method.
Matplotlib expects a series of points; it will do the plotting if you can figure out how to render your equation.
Referring to Is it possible to plot implicit equations using Matplotlib? Mike Graham's answer suggests using scipy.optimize to numerically explore the implicit function.
There is an interesting gallery at http://xrt.wikidot.com/gallery:implicit showing a variety of raytraced implicit functions - if your equation matches one of these, it might give you a better idea what you are looking at.
Failing that, if you care to share the actual equation, maybe someone can suggest an easier approach.
As far as I know, it is not possible. You have to solve this equation numerically by yourself. Using scipy.optimize is a good idea. The simplest case is that you know the range of the surface that you want to plot, and just make a regular grid in x and y, and try to solve equation F(xi,yi,z)=0 for z, giving a starting point of z. Following is a very dirty code that might help you
from scipy import *
from scipy import optimize
xrange = (0,1)
yrange = (0,1)
density = 100
startz = 1
def F(x,y,z):
return x**2+y**2+z**2-10
x = linspace(xrange[0],xrange[1],density)
y = linspace(yrange[0],yrange[1],density)
points = []
for xi in x:
for yi in y:
g = lambda z:F(xi,yi,z)
res = optimize.fsolve(g, startz, full_output=1)
if res[2] == 1:
zi = res[0]
points.append([xi,yi,zi])
points = array(points)
Actually there is an easy way to plot implicit 3D surface with the scikit-image package. The key is the marching_cubes method.
import numpy as np
from skimage import measure
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
Then we compute the function over a 3D meshgrid, in this example we use the goursat_tangle method #Paul defined in its answer:
xl = np.linspace(-3, 3, 50)
X, Y, Z = np.meshgrid(xl, xl, xl)
F = goursat_tangle(X, Y, Z)
The magic is happening here with marching_cubes:
verts, faces, normals, values = measure.marching_cubes(F, 0, spacing=[np.diff(xl)[0]]*3)
verts -= 3
We just need to correct vertices coordinates as they are expressed in Voxel coordinates (hence scaling using spacing switch and the subsequent origin shift).
Finally it is just about rendering the iso-surface using tri_surface:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2], cmap='jet', lw=0)
Which returns:
Have you looked at mplot3d on matplotlib?
Finally, I did it (I updated my matplotlib to 1.0.1).
Here is code:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def hyp_part1(x,y,z):
return -(x**2) - (y**2) + (z**2) - 1
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_range = np.arange(-100,100,10)
y_range = np.arange(-100,100,10)
X,Y = np.meshgrid(x_range,y_range)
A = np.linspace(-100, 100, 15)
A1,A2 = np.meshgrid(A,A)
for z in A:
X,Y = A1, A2
Z = hyp_part1(X,Y,z)
ax.contour(X, Y, Z+z, [z], zdir='z')
for y in A:
X,Z= A1, A2
Y = hyp_part1(X,y,Z)
ax.contour(X, Y+y, Z, [y], zdir='y')
for x in A:
Y,Z = A1, A2
X = hyp_part1(x,Y,Z)
ax.contour(X+x, Y, Z, [x], zdir='x')
ax.set_zlim3d(-100,100)
ax.set_xlim3d(-100,100)
ax.set_ylim3d(-100,100)
Here is result:
Thank You, Paul!
MathGL (GPL plotting library) can plot it easily. Just create a data mesh with function values f[i,j,k] and use Surf3() function to make isosurface at value f[i,j,k]=0. See this sample.
I am trying to make a 3-dimensional surface plot for the expression: z = y^2/x, for x in the interval [-2,2] and y in the interval [-1.4,1.4]. I also want the z-values to range from -4 to 4.
The problem is that when I'm viewing the finished surfaceplot, the z-axis values do not stop at [-4,4].
So my question is how I can "remove" the z-axis value that range outside the intervall [-4,4] from the finished plot?
My code is:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection="3d")
x = np.arange(-2.0,2.0,0.1,float) # x in interval [-2,2]
y = np.arange(-1.4,1.4,0.1,float) # y in interval [-1.4,1.4]
x,y = np.meshgrid(x,y)
z = (y**2/x) # z = y^2/x
ax.plot_surface(x, y, z,rstride=1, cstride=1, linewidth=0.25)
ax.set_zlim3d(-4, 4) # viewrange for z-axis should be [-4,4]
ax.set_ylim3d(-2, 2) # viewrange for y-axis should be [-2,2]
ax.set_xlim3d(-2, 2) # viewrange for x-axis should be [-2,2]
plt.show()
I am having the same issue and still have not found anything better than clipping my data. Unfortunately in my case I am tied to matplotlib 1.2.1. But in case you can upgrade to version 1.3.0 you could have a solution: it seems there is a bunch of new API related to axes ranges. In particular, you may be interested by the "set_zlim".
Edit 1: Manage to migrate my environnement to use matplotlib 1.3.0; set_zlim worked like a charm :)
The follwing code worked for me (By the way I am running this on OSX, I am not sure this has an impact?):
# ----------------------------------------------------------------------------
# Make a 3d plot according to data passed as arguments
def Plot3DMap( self, LabelX, XRange, LabelY, YRange, LabelZ, data3d ) :
fig = plt.figure()
ax = fig.add_subplot( 111, projection="3d" )
xs, ys = np.meshgrid( XRange, YRange )
surf = ax.plot_surface( xs, ys, data3d )
ax.set_xlabel( LabelX )
ax.set_ylabel( LabelY )
ax.set_zlabel( LabelZ )
ax.set_zlim(0, 100)
plt.show()
clipping your data will accomplish this, but it's not very pretty.
z[z>4]= np.nan
z[z<-4]= np.nan
Rather than using ax.plot_surface I found ax.plot_trisurf to work well, since you don't need to give it a rectangular grid of values like ax.plot_surface. If you're using numpy arrays, you can then use the following trick to only select points within your z-bounds.
from matplotlib import cm
x, y, z = x.flatten(), y.flatten(), z.flatten()
usable_points = (-4 < z) & (z < 4)
x, y, z = x[usable_points], y[usable_points], z[usable_points]
ax.plot_trisurf(x, y, z, cmap=cm.jet)