discretize.mixins.mpl_mod.InterfaceMPL

class discretize.mixins.mpl_mod.InterfaceMPL[source]

Bases: object

This class is used for simple discretize mesh plotting using matplotlib.

This interface adds three methods to the meshes. plot_grid will plot the grid points of each mesh in 2D and 3D. plot_image for 2D image ploting of models. and plot_slice for plotting a 2D slice through a 3D mesh.

Methods

plotGrid(*args, **kwargs)

plotGrid has been deprecated.

plotImage(*args, **kwargs)

plotImage has been deprecated.

plotSlice(*args, **kwargs)

plotSlice has been deprecated.

plot_3d_slicer(v[, xslice, yslice, zslice, …])

Plot slices of a 3D volume, interactively (scroll wheel).

plot_grid([ax, nodes, faces, centers, …])

Plot the nodal, cell-centered and staggered grids.

plot_image(v[, v_type, grid, view, ax, …])

Plots fields on the given mesh.

plot_slice(v[, v_type, normal, ind, grid, …])

Plots slice of fields on the given 3D mesh.

Attributes

Methods

InterfaceMPL.plotGrid(*args, **kwargs)

plotGrid has been deprecated. See plot_grid for documentation

InterfaceMPL.plotImage(*args, **kwargs)

plotImage has been deprecated. See plot_image for documentation

InterfaceMPL.plotSlice(*args, **kwargs)

plotSlice has been deprecated. See plot_slice for documentation

InterfaceMPL.plot_3d_slicer(v, xslice=None, yslice=None, zslice=None, v_type='CC', view='real', axis='xy', transparent=None, clim=None, xlim=None, ylim=None, zlim=None, aspect='auto', grid=[2, 2, 1], pcolor_opts=None, fig=None, **kwargs)[source]

Plot slices of a 3D volume, interactively (scroll wheel).

If called from a notebook, make sure to set

%matplotlib notebook

See the class discretize.View.Slicer for more information.

It returns nothing. However, if you need the different figure handles you can get it via

fig = plt.gcf()

and subsequently its children via

fig.get_children()

and recursively deeper, e.g.,

fig.get_children()[0].get_children().

One can also provide an existing figure instance, which can be useful for interactive widgets in Notebooks. The provided figure is cleared first.

InterfaceMPL.plot_grid(ax=None, nodes=False, faces=False, centers=False, edges=False, lines=True, show_it=False, **kwargs)[source]

Plot the nodal, cell-centered and staggered grids.

Parameters
axmatplotlib.axes.Axes or None, optional

The axes to draw on. None produces a new Axes.

nodes, faces, centers, edges, linesbool, optional

Whether to plot the corresponding item

show_itbool, optional

whether to call plt.show()

colorColor or str, optional

If lines=True, the color of the lines, defaults to first color.

linewidthfloat, optional

If lines=True, the linewidth for the lines.

Returns
matplotlib.axes.Axes

Axes handle for the plot

Other Parameters
edges_x, edges_y, edges_z, faces_x, faces_y, faces_zbool, optional

When plotting a TreeMesh, these are also options to plot the individual component items.

cell_linebool, optional

When plotting a TreeMesh, you can also plot a line through the cell centers in order.

slice{‘both’, ‘theta’, ‘z’}

When plotting a CylindricalMesh, which dimension to slice over.

Notes

Excess arguments are passed on to plot

Examples

Plotting a 2D TensorMesh grid

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> import numpy as np
>>> h1 = np.linspace(.1, .5, 3)
>>> h2 = np.linspace(.1, .5, 5)
>>> mesh = discretize.TensorMesh([h1, h2])
>>> mesh.plot_grid(nodes=True, faces=True, centers=True, lines=True)
>>> plt.show()

(Source code, png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_00_00.png

Plotting a 3D TensorMesh grid

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> import numpy as np
>>> h1 = np.linspace(.1, .5, 3)
>>> h2 = np.linspace(.1, .5, 5)
>>> h3 = np.linspace(.1, .5, 3)
>>> mesh = discretize.TensorMesh([h1, h2, h3])
>>> mesh.plot_grid(nodes=True, faces=True, centers=True, lines=True)
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_01_00.png

Plotting a 2D CurvilinearMesh

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> X, Y = discretize.utils.exampleLrmGrid([10, 10], 'rotate')
>>> M = discretize.CurvilinearMesh([X, Y])
>>> M.plot_grid()
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_02_00.png

Plotting a 3D CurvilinearMesh

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> X, Y, Z = discretize.utils.exampleLrmGrid([5, 5, 5], 'rotate')
>>> M = discretize.CurvilinearMesh([X, Y, Z])
>>> M.plot_grid()
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_03_00.png

Plotting a 2D TreeMesh

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> M = discretize.TreeMesh([32, 32])
>>> M.insert_cells([[0.25, 0.25]], [4])
>>> M.plot_grid()
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_04_00.png

Plotting a 3D TreeMesh

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> M = discretize.TreeMesh([32, 32, 32])
>>> M.insert_cells([[0.3, 0.75, 0.22]], [4])
>>> M.plot_grid()
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-1_05_00.png
InterfaceMPL.plot_image(v, v_type='CC', grid=False, view='real', ax=None, clim=None, show_it=False, pcolor_opts=None, stream_opts=None, grid_opts=None, range_x=None, range_y=None, sample_grid=None, stream_thickness=None, stream_threshold=None, **kwargs)[source]

Plots fields on the given mesh.

Parameters
vnumpy.ndarray

values to plot

v_type{‘CC’,’CCV’, ‘N’, ‘F’, ‘Fx’, ‘Fy’, ‘Fz’, ‘E’, ‘Ex’, ‘Ey’, ‘Ez’}

Where the values of v are defined.

view{‘real’, ‘imag’, ‘abs’, ‘vec’}

How to view the array.

axmatplotlib.axes.Axes, optional

The axes to draw on. None produces a new Axes.

climtuple of float, optional

length 2 tuple of (vmin, vmax) for the color limits

range_x, range_ytuple of float, optional

length 2 tuple of (min, max) for the bounds of the plot axes.

pcolor_optsdict, optional

Arguments passed on to pcolormesh

gridbool, optional

Whether to plot the edges of the mesh cells.

grid_optsdict, optional

If grid is true, arguments passed on to plot for grid

sample_gridtuple of numpy.ndarray, optional

If view == ‘vec’, mesh cell widths (hx, hy) to interpolate onto for vector plotting

stream_optsdict, optional

If view == ‘vec’, arguments passed on to streamplot

stream_thicknessfloat, optional

If view == ‘vec’, linewidth for streamplot

stream_thresholdfloat, optional

If view == ‘vec’, only plots vectors with magnitude above this threshold

show_itbool, optional

Whether to call plt.show()

numberingbool, optional

For 3D TensorMesh only, show the numbering of the slices

annotation_colorColor or str, optional

For 3D TensorMesh only, color of the annotation

Examples

2D TensorMesh plotting

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> import numpy as np
>>> M = discretize.TensorMesh([20, 20])
>>> v = np.sin(M.gridCC[:, 0]*2*np.pi)*np.sin(M.gridCC[:, 1]*2*np.pi)
>>> M.plot_image(v)
>>> plt.show()

(Source code, png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-2_00_00.png

3D TensorMesh plotting

>>> import discretize
>>> import numpy as np
>>> M = discretize.TensorMesh([20, 20, 20])
>>> v = np.sin(M.gridCC[:, 0]*2*np.pi)*np.sin(M.gridCC[:, 1]*2*np.pi)*np.sin(M.gridCC[:, 2]*2*np.pi)
>>> M.plot_image(v, annotation_color='k')
>>> plt.show()

(png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-2_01_00.png
InterfaceMPL.plot_slice(v, v_type='CC', normal='Z', ind=None, grid=False, view='real', ax=None, clim=None, show_it=False, pcolor_opts=None, stream_opts=None, grid_opts=None, range_x=None, range_y=None, sample_grid=None, stream_threshold=None, stream_thickness=None, **kwargs)[source]

Plots slice of fields on the given 3D mesh.

Parameters
vnumpy.ndarray

values to plot

v_type{‘CC’,’CCV’, ‘N’, ‘F’, ‘Fx’, ‘Fy’, ‘Fz’, ‘E’, ‘Ex’, ‘Ey’, ‘Ez’}, or tuple of these options

Where the values of v are defined.

normal{‘Z’, ‘X’, ‘Y’}

Normal direction of slicing plane.

indNone, optional

index along dimension of slice. Defaults to the center index.

view{‘real’, ‘imag’, ‘abs’, ‘vec’}

How to view the array.

axmatplotlib.axes.Axes, optional

The axes to draw on. None produces a new Axes. Must be None if v_type is a tuple.

climtuple of float, optional

length 2 tuple of (vmin, vmax) for the color limits

range_x, range_ytuple of float, optional

length 2 tuple of (min, max) for the bounds of the plot axes.

pcolor_optsdict, optional

Arguments passed on to pcolormesh

gridbool, optional

Whether to plot the edges of the mesh cells.

grid_optsdict, optional

If grid is true, arguments passed on to plot for the edges

sample_gridtuple of numpy.ndarray, optional

If view == ‘vec’, mesh cell widths (hx, hy) to interpolate onto for vector plotting

stream_optsdict, optional

If view == ‘vec’, arguments passed on to streamplot

stream_thicknessfloat, optional

If view == ‘vec’, linewidth for streamplot

stream_thresholdfloat, optional

If view == ‘vec’, only plots vectors with magnitude above this threshold

show_itbool, optional

Whether to call plt.show()

Examples

Plot a slice of a 3D TensorMesh solution to a Laplace’s equaiton.

First build the mesh:

>>> from matplotlib import pyplot as plt
>>> import discretize
>>> from pymatsolver import Solver
>>> import numpy as np
>>> hx = [(5, 2, -1.3), (2, 4), (5, 2, 1.3)]
>>> hy = [(2, 2, -1.3), (2, 6), (2, 2, 1.3)]
>>> hz = [(2, 2, -1.3), (2, 6), (2, 2, 1.3)]
>>> M = discretize.TensorMesh([hx, hy, hz])

then build the necessary parts of the PDE:

>>> q = np.zeros(M.vnC)
>>> q[[4, 4], [4, 4], [2, 6]]=[-1, 1]
>>> q = discretize.utils.mkvc(q)
>>> A = M.face_divergence * M.cell_gradient
>>> b = Solver(A) * (q)

and finaly, plot the vector values of the result, which are defined on faces

>>> M.plot_slice(M.cell_gradient*b, 'F', view='vec', grid=True, pcolor_opts={'alpha':0.8})
>>> plt.show()

(Source code, png, pdf)

../../_images/discretize-mixins-mpl_mod-InterfaceMPL-3.png