GraphicsĀ¶
The module gstlearn.plot contains various plot functions for gstlearn objets: DbGrid, Db, Vario, Model, Polygons... These functions are also accessible as methods of each class. For example for a grid, we could use equivalently gp.grid(mygrid,...) or mygrid.plot(...), or for more specific functions: gp.point(mygrid,...) or mygrid.plot_point(...)
Import packagesĀ¶
import numpy as np
import pandas as pd
import sys
import os
import matplotlib
import matplotlib.pyplot as plt
import gstlearn as gl
import gstlearn.plot as gp
import gstlearn.document as gdoc
from scipy import ndimage, misc
gdoc.setNoScroll()
We define the Space dimension and the defaulted figure dimension for Geographic plots. The command printDefault() provides the list of defaulted values.
gl.defineDefaultSpace(gl.ESpaceType.RN, 2)
gp.setDefault(dims=[6,6])
gp.printDefault()
Non geographical defaults: - Figure dimensions = [6, 6] - Limits along X (not defined) - Limits along Y (not defined) - Aspect = auto Geographical defaults: - Figure dimensions = [8, 8] - Limits along X (not defined) - Limits along Y (not defined) - Aspect = 1
General principleĀ¶
The paragraph describes the deiferrent manners for calling the graphics contained in gstlearn.plot package. This is demonstrated on an example of a 2-D data set, organized as a set of isolated points (called mydb)
ndat = 30
mydb = gl.Db.createFillRandom(ndat=30, nvar=3)
mydb
Data Base Characteristics ========================= Data Base Summary ----------------- File is organized as a set of isolated points Space dimension = 2 Number of Columns = 6 Total number of samples = 30 Variables --------- Column = 0 - Name = rank - Locator = NA Column = 1 - Name = x-1 - Locator = x1 Column = 2 - Name = x-2 - Locator = x2 Column = 3 - Name = z-1 - Locator = z1 Column = 4 - Name = z-2 - Locator = z2 Column = 5 - Name = z-3 - Locator = z3
The graphic representation is performed in general, for each object of gstlearn, in many different ways:
- by calling the general graphic representation function named plot
- by calling the generic function dedicated to the graphic representation of the specific object of the gstlearn library (this option presents different syntaxes)
- by calling the specific function attached to the graphic representation of the specific object of the gstlearn library (this option presetns different syntaxes)
General function plotĀ¶
This triggers the option attached specifically to each type of object.
gp.plot(mydb, nameSize="z-1")
Calling the generic functionĀ¶
You can call the generic function regrouping all the representation methods assigned to the target class (point).
This manner offers the possibility to check the list of options in the help.
help(gp.point)
Help on function point in module gstlearn.plot: point(db, *args, **kwargs) Construct a figure for plotting a point data base ax: matplotlib.Axes db: Db containing the variable to be plotted nameColor: Name of the variable containing the color per sample nameSize: Name of the variable containing the size per sample nameLabel: Name of the variable containing the label per sample nameCoorX: Name of the variable standing for X coordinate nameCoorY: Name of the variable standing for Y coordinate useSel : Boolean to indicate if the selection has to be considered color: Constant color (used if 'nameColor' is not defined) size: Constant size (used if 'nameSize' is not defined) sizmin: Size corresponding to the smallest value (used if 'nameSize' is defined) sizmax: Size corresponding to the largest value (used if 'nameSize' is defined) flagAbsSize: Represent the Absolute value in Size representation flagCst: When True, the size is kept constant (equel to 's') cmap: Optional Color scale flagGradient: Draw Gradient (if Gradients are defined) colorGradient: Color attached to the Gradient representation scaleGradient: Scale of the Gradient representation flagTangent: Draw Tangent (if Tangents are defined) colorTangent: Color attached to the Tangent representation scaleTangent: Scale of the Tangent representation flagLegendColor: Flag for representing the Color Legend (only if name-color is defined) flagLegendSize: Flag for representing the Size legend (only if nameSize is defined) flagLegendLabel: Flag for representing the Label Legend (only if nameLabel is defined) legendNameColor: Title for the Color Legend (set to 'nameColor' if not defined) legendNameSize: Title for the Size legend (set to 'nameSize' if not defined) legendNameLabel: Title for the Label Legend (set to 'nameLabel' if not defined) posX: rank of the first coordinate posY: rank of the second coordinate **kwargs : arguments passed to matplotllib.pyplot.scatter
For demonstration sake, we can use the generic function and ask for a combined display using symbols:
proportional to the variable z-2
with a color with relies on the variable z-1
The command point returns a pointer on the type of object (from matplotlib) which is generated internally. To get rid of this information (when considered as useless), you can simply store it in a dummy variable, as it is done in the next paragraph.
dum = gp.point(mydb, nameSize="z-2", nameColor="z-1")
You can also call the the same function, but in a object-based manner, where this representation is considered as a method of the object to be represented (here a Db).
dum = mydb.point(nameColor="z-1")
- calling the same function, in a object-based manner, but where this representation is considered as a method of the class Axes (from matplotlib). This is the clever way to handle the complex setup or overlays of views within a figure.
In addition, for this figure, we ask the display of a legend which combines the explanation for both representations (by size and by color).
Note that here, the display is initialized as a Geographic environment (the other one being called standard). This requires the system to use the default options registered for Geographic figures (dimensions, scale factor, ...).
fig, ax = gp.initGeographic()
ax.gstpoint(mydb, nameSize="z-2", nameColor="z-1", flagLegendSize=True, flagLegendColor=True)
plt.show()
In order to illustrate the use of this extensive version which allows overlay of several layers of information, we quickly create a polygon constituted as the (dilated) convex hull of the data (this option will be used later in this document).
mypoly = gl.Polygons.createFromDb(mydb, dilate=0.05)
The overlay can be handled as follows
fig, ax = gp.initGeographic()
ax.gstpoint(mydb, nameSize="z-2", nameColor="z-1", flagLegendColor=True, flagLegendSize=True)
ax.polygon(mypoly)
plt.show()
Calling the specific functionĀ¶
You can also be more specific by calling a function named explicitely. This is the case for displaying the literal values attached to each sample: the method is named literal.
Again the call to this function is covered using three different syntaxes.
dum = gp.literal(mydb, name="z-1")
The same function can be considered as a method of the object to be represented (Db)
dum = mydb.literal(name="z-2")
Fincally the same function can be specified as a method of the class Axis
fix, ax = gp.initGeographic()
ax.literal(mydb, name="z-1")
plt.show()
Model representationĀ¶
Creating a dummy Model used for simulating a random field and display it
ctxt = gl.CovContext() # use default space
mymodel = gl.Model(ctxt)
cova = gl.CovAniso(gl.ECov.CUBIC,10,1,1.5,mymodel.getContext())
mymodel.addCov(cova)
This first case gives the demonstration that we can combine gstlearn.plot functions with matplotlib functions. This is what is performed by representing a Model and adding some annotation. As an example, we wish to add the formula of the covariance represented, i.e.:
from IPython.display import display, Latex
display(Latex('$%s$'%cova.getFormula()))
fig, ax = gp.init() # gstlearn.plot entry function
ax.model(mymodel, flagLegend=True) # gstlearn.plot function
ax.decoration(title="Cubic model") # basic gstlean.plot function
ax.text(5,0.5,'$%s$'%cova.getFormula(),size='medium') # standard matplotlib function
plt.show() # standard matplotlib: flushing graphic
Grid representationsĀ¶
We create a rectangular non-rotated 2-D grid, and simulate random Gaussian field (using the Model previously defined). Two simulations are generated in order to emphasize the graphic posibilities in further parts of this note.
nx = [70,25]
dx = [1,2]
x0 = [-40, 20]
mygrid = gl.DbGrid.create(nx,dx,x0)
err = gl.simtub(None,mygrid,mymodel,None,2)
mygrid.display()
Data Base Grid Characteristics ============================== Data Base Summary ----------------- File is organized as a regular grid Space dimension = 2 Number of Columns = 5 Total number of samples = 1750 Grid characteristics: --------------------- Origin : -40.000 20.000 Mesh : 1.000 2.000 Number : 70 25 Variables --------- Column = 0 - Name = rank - Locator = NA Column = 1 - Name = x1 - Locator = x1 Column = 2 - Name = x2 - Locator = x2 Column = 3 - Name = Simu.1 - Locator = z1 Column = 4 - Name = Simu.2 - Locator = z2
Add a dummy selection to test visualization with Selection
dum = gp.symbol(mygrid, nameColor="Simu.1")
dum = mygrid.symbol(nameSize="Simu.1")
fig, ax = gp.initGeographic()
ax.symbol(mygrid, nameColor="Simu.1")
plt.show()