Store a 2d array as a table inside a virtual file.

Use the virtual file name to pass in the data in your matrix to a GMT module.

Context manager (use in a with block). Yields the virtual file name that you can pass as an argument to a GMT module call. Closes the virtual file upon exit of the with block.

The virtual file will contain the array as a GMT_MATRIX pretending to be a GMT_DATASET.

Not meant for creating ``GMT_GRID``. The grid requires more metadata than just the data matrix. Use pygmt.clib.Session.virtualfile_from_grid instead.

Use this instead of creating the data container and virtual file by hand with pygmt.clib.Session.create_data, pygmt.clib.Session.put_matrix, and pygmt.clib.Session.open_virtual_file

The matrix must be C contiguous in memory. If it is not (e.g., it is a slice of a larger array), the array will be copied to make sure it is.


matrix (2d array) – The matrix that will be included in the GMT data container.


fname (str) – The name of virtual file. Pass this as a file name argument to a GMT module.


>>> from pygmt.helpers import GMTTempFile
>>> import numpy as np
>>> data = np.arange(12).reshape((4, 3))
>>> print(data)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
>>> with Session() as ses:
...     with ses.virtualfile_from_matrix(data) as fin:
...         # Send the output to a file so that we can read it
...         with GMTTempFile() as fout:
...             ses.call_module(
...                 "info", "{} ->{}".format(fin,
...             )
...             print(
<matrix memory>: N = 4 <0/9> <1/10> <2/11>