The fatiando package has been deprecated. Please check out the new tools in the Fatiando a Terra website: www.fatiando.org

Plot 2D objects, maps and grids with matplotlib (fatiando.vis.mpl)

Wrappers for matplotlib functions to facilitate plotting grids, 2D objects, etc.

This module loads all functions from matplotlib.pyplot, adds new functions and overwrites some others (like contour, pcolor, etc).

Warning

This module will be removed in v0.6. We recommend the use of matplotlib.pyplot module directly. Some of the fatiando specific functions will remain.

Grids

Grids are automatically reshaped and interpolated if desired or necessary.

2D objects

Interactive

Basemap (map projections)

Auxiliary


fatiando.vis.mpl.basemap(area, projection, resolution='c')[source]

Make a basemap to use when plotting with map projections.

Uses the matplotlib basemap toolkit.

Parameters:

  • area
    : list

    [west, east, south, north], i.e., the area of the data that is going to be plotted

  • projection
    : str

    The name of the projection you want to use. Choose from:

    • ‘ortho’: Orthographic
    • ‘geos’: Geostationary
    • ‘robin’: Robinson
    • ‘cass’: Cassini
    • ‘merc’: Mercator
    • ‘poly’: Polyconic
    • ‘lcc’: Lambert Conformal
    • ‘stere’: Stereographic
  • resolution
    : str

    The resolution for the coastlines. Can be ‘c’ for crude, ‘l’ for low, ‘i’ for intermediate, ‘h’ for high

Returns:

  • basemap
    : mpl_toolkits.basemap.Basemap

    The basemap

fatiando.vis.mpl.contour(x, y, v, shape, levels, interp=False, extrapolate=False, color='k', label=None, clabel=True, style='solid', linewidth=1.0, basemap=None)[source]

Make a contour plot of the data.

Parameters:

  • x, y
    : array

    Arrays with the x and y coordinates of the grid points. If the data is on a regular grid, then assume x varies first (ie, inner loop), then y.

  • v
    : array

    The scalar value assigned to the grid points.

  • shape
    : tuple = (ny, nx)

    Shape of the regular grid. If interpolation is not False, then will use shape to grid the data.

  • levels
    : int or list

    Number of contours to use or a list with the contour values.

  • interp
    : True or False

    Wether or not to interpolate before trying to plot. If data is not on regular grid, set to True!

  • extrapolate
    : True or False

    Wether or not to extrapolate the data when interp=True

  • color
    : str

    Color of the contour lines.

  • label
    : str

    String with the label of the contour that would show in a legend.

  • clabel
    : True or False

    Wether or not to print the numerical value of the contour lines

  • style
    : str

    The style of the contour lines. Can be 'dashed', 'solid' or 'mixed' (solid lines for positive contours and dashed for negative)

  • linewidth
    : float

    Width of the contour lines

  • basemap
    : mpl_toolkits.basemap.Basemap

    If not None, will use this basemap for plotting with a map projection (see basemap for creating basemaps)

Returns:

  • levels
    : list

    List with the values of the contour levels

fatiando.vis.mpl.contourf(x, y, v, shape, levels, interp=False, extrapolate=False, vmin=None, vmax=None, cmap=<matplotlib.colors.LinearSegmentedColormap object>, basemap=None)[source]

Make a filled contour plot of the data.

Parameters:

  • x, y
    : array

    Arrays with the x and y coordinates of the grid points. If the data is on a regular grid, then assume x varies first (ie, inner loop), then y.

  • v
    : array

    The scalar value assigned to the grid points.

  • shape
    : tuple = (ny, nx)

    Shape of the regular grid. If interpolation is not False, then will use shape to grid the data.

  • levels
    : int or list

    Number of contours to use or a list with the contour values.

  • interp
    : True or False

    Wether or not to interpolate before trying to plot. If data is not on regular grid, set to True!

  • extrapolate
    : True or False

    Wether or not to extrapolate the data when interp=True

  • vmin, vmax

    Saturation values of the colorbar. If provided, will overwrite what is set by levels.

  • cmap
    : colormap

    Color map to be used. (see pyplot.cm module)

  • basemap
    : mpl_toolkits.basemap.Basemap

    If not None, will use this basemap for plotting with a map projection (see basemap for creating basemaps)

Returns:

  • levels
    : list

    List with the values of the contour levels

fatiando.vis.mpl.draw_coastlines(basemap, linewidth=1, style='solid')[source]

Draw the coastlines using the given basemap.

Parameters:

  • basemap
    : mpl_toolkits.basemap.Basemap

    The basemap used for plotting (see basemap)

  • linewidth
    : float

    The width of the lines

  • style
    : str

    The style of the lines. Can be: ‘solid’, ‘dashed’, ‘dashdot’ or ‘dotted’

fatiando.vis.mpl.draw_countries(basemap, linewidth=1, style='dashed')[source]

Draw the country borders using the given basemap.

Parameters:

  • basemap
    : mpl_toolkits.basemap.Basemap

    The basemap used for plotting (see basemap)

  • linewidth
    : float

    The width of the lines

  • style
    : str

    The style of the lines. Can be: ‘solid’, ‘dashed’, ‘dashdot’ or ‘dotted’

fatiando.vis.mpl.draw_geolines(area, dlon, dlat, basemap, linewidth=1)[source]

Draw the parallels and meridians on a basemap plot.

Parameters:

  • area
    : list

    [west, east, south, north], i.e., the area where the lines will be plotted

  • dlon, dlat
    : float

    The spacing between the lines in the longitude and latitude directions, respectively (in decimal degrees)

  • basemap
    : mpl_toolkits.basemap.Basemap

    The basemap used for plotting (see basemap)

  • linewidth
    : float

    The width of the lines

fatiando.vis.mpl.draw_layers(area, axes, style='-', marker='o', color='k', width=2)[source]

Draw series of horizontal layers by clicking with the mouse.

The y-axis is assumed to be depth, the x-axis is the physical property of each layer.

INSTRUCTIONS:

  • Click to make a new layer;
  • Press ‘e’ to erase the last layer;
  • Close the figure window to finish;

Parameters:

  • area
    : list = [x1, x2, y1, y2]

    Borders of the area containing the polygon

  • axes
    : matplotlib Axes

    The figure to use for drawing the polygon. To get an Axes instace, just do:

    from matplotlib import pyplot
    axes = pyplot.figure().add_subplot(1,1,1)
    

    You can plot things to axes before calling this function so that they’ll appear on the background.

  • style
    : str

    Line style (as in matplotlib.pyplot.plot)

  • marker
    : str

    Style of the point markers (as in matplotlib.pyplot.plot)

  • color
    : str

    Line color (as in matplotlib.pyplot.plot)

  • width
    : float

    The line width (as in matplotlib.pyplot.plot)

Returns:

  • layers : list = [thickness, values]

    • thickness
      : list

      The thickness of each layer, in order of increasing depth

    • values
      : list

      The physical property value of each layer, in the same order

fatiando.vis.mpl.draw_polygon(area, axes, style='-', marker='o', color='k', width=2, alpha=0.5, xy2ne=False)[source]

Draw a polygon by clicking with the mouse.

INSTRUCTIONS:

  • Left click to pick the edges of the polygon;
  • Draw edges CLOCKWISE;
  • Press ‘e’ to erase the last edge;
  • Right click to close the polygon;
  • Close the figure window to finish;

Parameters:

  • area
    : list = [x1, x2, y1, y2]

    Borders of the area containing the polygon

  • axes
    : matplotlib Axes

    The figure to use for drawing the polygon. To get an Axes instace, just do:

    from matplotlib import pyplot
    axes = pyplot.figure().add_subplot(1,1,1)
    

    You can plot things to axes before calling this function so that they’ll appear on the background.

  • style
    : str

    Line style (as in matplotlib.pyplot.plot)

  • marker
    : str

    Style of the point markers (as in matplotlib.pyplot.plot)

  • color
    : str

    Line color (as in matplotlib.pyplot.plot)

  • width
    : float

    The line width (as in matplotlib.pyplot.plot)

  • alpha
    : float

    Transparency of the fill of the polygon. 0 for transparent, 1 for opaque (fills the polygon once done drawing)

  • xy2ne
    : True or False

    If True, will exchange the x and y axis so that x points north. Use this when drawing on a map viewed from above. If the y-axis of the plot is supposed to be z (depth), then use xy2ne=False.

Returns:

  • edges
    : list of lists

    List of [x, y] pairs with the edges of the polygon

fatiando.vis.mpl.layers(thickness, values, style='-k', z0=0.0, linewidth=1, label=None, **kwargs)[source]

Plot a series of layers and values associated to each layer.

Parameters:

  • thickness
    : list

    The thickness of each layer in order of increasing depth

  • values
    : list

    The value associated with each layer in order of increasing depth

  • style
    : str

    String with the color and line style (as in matplotlib.pyplot.plot)

  • z0
    : float

    The depth of the top of the first layer

  • linewidth
    : float

    Line width

  • label
    : str

    label associated with the square.

Returns:

  • axes
    : matplitlib.axes

    The axes element of the plot

fatiando.vis.mpl.m2km(axis=None)[source]

Convert the x and y tick labels from meters to kilometers.

Parameters:

  • axis
    : matplotlib axis instance

    The plot.

Tip

Use fatiando.vis.gca() to get the current axis. Or the value returned by fatiando.vis.subplot or matplotlib.pyplot.subplot.

fatiando.vis.mpl.paths(pts1, pts2, style='-k', linewidth=1, label=None)[source]

Plot paths between the two sets of points.

Parameters:

  • pts1
    : list of lists

    List of (x, y) pairs with the coordinates of the points

  • pts2
    : list of lists

    List of (x, y) pairs with the coordinates of the points

  • style
    : str

    String with the color and line style (as in matplotlib.pyplot.plot)

  • linewidth
    : float

    The width of the lines representing the paths

  • label
    : str

    If not None, then the string that will show in the legend

fatiando.vis.mpl.pcolor(x, y, v, shape, interp=False, extrapolate=False, cmap=<matplotlib.colors.LinearSegmentedColormap object>, vmin=None, vmax=None, basemap=None)[source]

Make a pseudo-color plot of the data.

Parameters:

  • x, y
    : array

    Arrays with the x and y coordinates of the grid points. If the data is on a regular grid, then assume x varies first (ie, inner loop), then y.

  • v
    : array

    The scalar value assigned to the grid points.

  • shape
    : tuple = (ny, nx)

    Shape of the regular grid. If interpolation is not False, then will use shape to grid the data.

  • interp
    : True or False

    Wether or not to interpolate before trying to plot. If data is not on regular grid, set to True!

  • extrapolate
    : True or False

    Wether or not to extrapolate the data when interp=True

  • cmap
    : colormap

    Color map to be used. (see pyplot.cm module)

  • vmin, vmax

    Saturation values of the colorbar.

  • basemap
    : mpl_toolkits.basemap.Basemap

    If not None, will use this basemap for plotting with a map projection (see basemap for creating basemaps)

Returns:

  • axes
    : matplitlib.axes

    The axes element of the plot

fatiando.vis.mpl.pick_points(area, axes, marker='o', color='k', size=8, xy2ne=False)[source]

Get the coordinates of points by clicking with the mouse.

INSTRUCTIONS:

  • Left click to pick the points;
  • Press ‘e’ to erase the last point picked;
  • Close the figure window to finish;

Parameters:

  • area
    : list = [x1, x2, y1, y2]

    Borders of the area containing the points

  • axes
    : matplotlib Axes

    The figure to use for drawing the polygon. To get an Axes instace, just do:

    from matplotlib import pyplot
    axes = pyplot.figure().add_subplot(1,1,1)
    

    You can plot things to axes before calling this function so that they’ll appear on the background.

  • marker
    : str

    Style of the point markers (as in matplotlib.pyplot.plot)

  • color
    : str

    Line color (as in matplotlib.pyplot.plot)

  • size
    : float

    Marker size (as in matplotlib.pyplot.plot)

  • xy2ne
    : True or False

    If True, will exchange the x and y axis so that x points north. Use this when drawing on a map viewed from above. If the y-axis of the plot is supposed to be z (depth), then use xy2ne=False.

Returns:

  • points
    : list of lists

    List of [x, y] coordinates of the points

fatiando.vis.mpl.points(pts, style='.k', size=10, label=None, xy2ne=False)[source]

Plot a list of points.

Parameters:

  • pts
    : list of lists

    List of [x, y] pairs with the coordinates of the points

  • style
    : str

    String with the color and line style (as in matplotlib.pyplot.plot)

  • size
    : int

    Size of the plotted points

  • label
    : str

    If not None, then the string that will show in the legend

  • xy2ne
    : True or False

    If True, will exchange the x and y axis so that the x coordinates of the polygon are north. Use this when drawing on a map viewed from above. If the y-axis of the plot is supposed to be z (depth), then use xy2ne=False.

Returns:

  • axes
    : matplitlib.axes

    The axes element of the plot

fatiando.vis.mpl.polygon(polygon, style='-k', linewidth=1, fill=None, alpha=1.0, label=None, xy2ne=False, linealpha=1.0)[source]

Plot a polygon.

Parameters:

  • polygon
    : fatiando.mesher.Polygon

    The polygon

  • style
    : str

    Color and line style string (as in matplotlib.pyplot.plot)

  • linewidth
    : float

    Line width

  • fill
    : str

    A color string used to fill the polygon. If None, the polygon is not filled

  • alpha
    : float

    Transparency of the fill (1 >= alpha >= 0). 0 is transparent and 1 is opaque

  • linealpha
    : float

    Transparency of the line (1 >= alpha >= 0). 0 is transparent and 1 is opaque

  • label
    : str

    String with the label identifying the polygon in the legend

  • xy2ne
    : True or False

    If True, will exchange the x and y axis so that the x coordinates of the polygon are north. Use this when drawing on a map viewed from above. If the y-axis of the plot is supposed to be z (depth), then use xy2ne=False.

Returns:

  • lines
    : matplotlib Line object

    Line corresponding to the polygon plotted

fatiando.vis.mpl.seismic_image(section, dt=0.004, ranges=None, cmap=<matplotlib.colors.LinearSegmentedColormap object>, aspect=None, vmin=None, vmax=None)[source]

Plot a seismic section (numpy 2D array matrix) as an image.

Parameters:

  • section
    : 2D array

    matrix of traces (first dimension time, second dimension traces)

  • dt
    : float

    sample rate in seconds (default 4 ms)

  • ranges
    : (x1, x2)

    min and max horizontal values (default trace number)

  • cmap
    : colormap

    color map to be used. (see pyplot.cm module)

  • aspect
    : float

    matplotlib imshow aspect parameter, ratio between axes

  • vmin, vmax
    : float

    min and max values for imshow

fatiando.vis.mpl.seismic_wiggle(section, dt=0.004, ranges=None, scale=1.0, color='k', normalize=False)[source]

Plot a seismic section (numpy 2D array matrix) as wiggles.

Parameters:

  • section
    : 2D array

    matrix of traces (first dimension time, second dimension traces)

  • dt
    : float

    sample rate in seconds (default 4 ms)

  • ranges
    : (x1, x2)

    min and max horizontal values (default trace number)

  • scale
    : float

    scale factor multiplied by the section values before plotting

  • color
    : tuple of strings

    Color for filling the wiggle, positive and negative lobes.

  • normalize :

    True to normalizes all trace in the section using global max/min data will be in the range (-0.5, 0.5) zero centered

Warning

Slow for more than 200 traces, in this case decimate your data or use seismic_image.

fatiando.vis.mpl.set_area(area)[source]

Set the area of a Matplolib plot using xlim and ylim.

Parameters:

  • area
    : list = [x1, x2, y1, y2]

    Coordinates of the top right and bottom left corners of the area

fatiando.vis.mpl.square(area, style='-k', linewidth=1, fill=None, alpha=1.0, label=None, xy2ne=False)[source]

Plot a square.

Parameters:

  • area
    : list = [x1, x2, y1, y2]

    Borders of the square

  • style
    : str

    String with the color and line style (as in matplotlib.pyplot.plot)

  • linewidth
    : float

    Line width

  • fill
    : str

    A color string used to fill the square. If None, the square is not filled

  • alpha
    : float

    Transparency of the fill (1 >= alpha >= 0). 0 is transparent and 1 is opaque

  • label
    : str

    label associated with the square.

  • xy2ne
    : True or False

    If True, will exchange the x and y axis so that the x coordinates of the polygon are north. Use this when drawing on a map viewed from above. If the y-axis of the plot is supposed to be z (depth), then use xy2ne=False.

Returns:

  • axes
    : matplitlib.axes

    The axes element of the plot

fatiando.vis.mpl.squaremesh(mesh, prop, cmap=<matplotlib.colors.LinearSegmentedColormap object>, vmin=None, vmax=None)[source]

Make a pseudo-color plot of a mesh of squares

Parameters:

  • mesh
    : fatiando.mesher.SquareMesh or compatible

    The mesh (a compatible mesh must implement the methods get_xs and get_ys)

  • prop
    : str

    The physical property of the squares to use as the color scale.

  • cmap
    : colormap

    Color map to be used. (see pyplot.cm module)

  • vmin, vmax
    : float

    Saturation values of the colorbar.

Returns:

  • axes
    : matplitlib.axes

    The axes element of the plot