To accomplish Python integration with Minitab Statistical Software, Minitab, LLC provides the mtbpy module. The following descriptions of the classes and methods from the mtbpy module prepare you to write Python code that integrates with Minitab Statistical Software.

For information on how to install Minitab's Python module and how to run Python from Minitab Statistical Software, go to Installing Python to use with Minitab Statistical Software.

For more information on Python, consult the guidance available at www.python.org.

Module: mtbpy

Class: mtb_instance

The following are the methods for the mtb_instance class.

get_column

Retrieves a column from a Minitab worksheet to use in Python.

column_name: string
Specifies the column to retrieve. You can specify either the column number (for example, "C1") or the column name (for example, "My Column").
Return value
Returns the column of data from the active worksheet as a Python list. The list can contain either text or numeric values.
Example
from mtbpy import mtbpy

column1 = mtbpy.mtb_instance().get_column("C1")
column2 = mtbpy.mtb_instance().get_column("My Column")

get_constant

Retrieves a constant from a Minitab worksheet to use in Python.

constant_name: string
Specifies the constant to retrieve. You can specify either the constant number (for example, "K1") or the constant name (for example, "My Constant").
Return value
Returns a constant that can be either a text or numeric value.
Example
from mtbpy import mtbpy

constant1 = mtbpy.mtb_instance().get_constant("K1")
constant2 = mtbpy.mtb_instance().get_constant("My Constant")

get_matrix

Retrieves a matrix from a Minitab worksheet to use in Python.

matrix_name: string
Specifies the matrix to retrieve. You can specify either the matrix number (for example, "M1") or the matrix name (for example, "My Matrix").
Return value
Returns the columns of data from the matrix as a Python list of lists.
Example
from mtbpy import mtbpy

matrix1 = mtbpy.mtb_instance().get_matrix("M1")
matrix2 = mtbpy.mtb_instance().get_matrix("My Matrix")

add_message

Appends a message to the Minitab Output pane.

message: string
Specifies the message to display.
Return value
None
Example
from mtbpy import mtbpy

mtbpy.mtb_instance().add_message("This is a message.")

set_note

Sets a note at the top of the Minitab Output pane.

message: string
Specifies the text to display.
Return value
None
Example
from mtbpy import mtbpy

mtbpy.mtb_instance().set_note("The output contains one note.")

set_note

Sets the title at the top of the Minitab Output pane.

title: string
Specifies the text to display.
Return value
None
Example
from mtbpy import mtbpy

mtbpy.mtb_instance().set_title("The output contains one title.")

add_image

Appends an image to the Minitab Output pane when you have a supported image file.

path: string
Specifies the path to the image.
Return value
None
Example
from mtbpy import mtbpy
import numpy as np
import matplotlib.pyplot as plt

N_points = 1000 
n_bins = 50 
x = np.random.randn(N_points) 
y = .4 * x + np.random.randn(N_points) + 5 
fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) 
axs[0].hist(x, bins=n_bins) 
axs[1].hist(y, bins=n_bins) 
fig.savefig("histogram.png") 
mtbpy.mtb_instance().add_image("histogram.png")

add_image_bytes

Appends an image to the Minitab Output pane when you have a bytes object.

data: bytes
Specifies the bytes of data for an image. For example, you can enter a bytes array as the parameter.
Return value
None
Example
from mtbpy import mtbpy

image_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\t\x00\x00\x00\t\x08\x02\x00\x00\x00o\xf3\x91G\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00\tpHYs\x00\x00\x0e\xc3\x00\x00\x0e\xc3\x01\xc7o\xa8d\x00\x00\x00"IDAT\x18Wc\xd8a\xbb\x8b\x81\x01\'I\xba\x04\x01i\x9c\x12\x04\xa4qJ\x10\x90\xc6)\xb1\xc3v\x17\x00\xfc\x0bE\x08o,\xff\xe2\x00\x00\x00\x00IEND\xaeB`\x82'
mtbpy.mtb_instance().add_image_bytes(image_data)

add_table

Appends a table to the Minitab Output pane.

columns: list of lists
Specifies the columns of data for the table as a list of lists.
headers: list (Optional)
Specifies the column headers for the table. The default headers is an empty list.
title: string (Optional)
Specifies the title for the table. The default title is "".
footnote: string (Optional)
Specifies the footnote under the table. The default footnote is "".
Return value
None
Example
from mtbpy import mtbpy

mytitle = "My table title"
myheaders = ["Header for column 1", "Header for column 2"]
mycolumns = [[1,1,1],[2,2,2]]
myfootnote = "My footnote for the table."
mtbpy.mtb_instance().add_table(columns=mycolumns, headers=myheaders, title=mytitle, footnote=myfootnote)

Example code that converts Minitab date format to Unix date format

By default, Minitab uses a different datetime format than Python. To convert from the Minitab datetime format to the Unix datetime format, use the following code:
from datetime import datetime, timedelta

def minitab_to_unix_datetime(pOrdinal, pEpoch0=datetime(1899, 12, 30)):
    return(pEpoch0 + timedelta(days=pOrdinal))