Quickly Run a Python Script with a Button in Minitab

Marilyn Wheatley 01 July, 2020

Topics: Machine Learning, Minitab Statistical Software, Python

If you’ve ever wished for an easy way to add a custom analysis to Minitab’s interface, read on! Minitab’s new Python integration feature allows an avenue for easily running custom Python code via a button in the Minitab interface. If you’re not a programmer, fear not! I’m no programmer myself, so if I can do this, you can too.

In this post, I’ll walk through the steps to add a custom menu with a button to execute a Python script that will generate a QQ plot and display the results in Minitab’s output pane.

If you’re a coding wizard, this example is very simple but will give you a feel for how easily you could set up something similar for others in your organization, allowing them take advantage of your custom code. You’ll be a hero!

First Things First: Let's Get Python Up and Running

This feature is available starting with Minitab 19.2020.1, and you’ll need Python 3.6.1 or higher installed on your machine. You can find all the details about connecting Minitab to Python here.

The Python script to generate the QQ plot, along with the necessary commands to send your data from Minitab to Python and return the results to Minitab are shown below:

Python script

You can also download the Python script above from here. I found the script to generate the QQ plot by searching the internet, which is easy to do for anyone that doesn’t want to learn to code. Note that the above code will retrieve the data for the QQ plot for column C1 in your Minitab worksheet.

Are You a Coding Whiz?

More on Commands that Let Minitab 'Talk' to Python

 

Set the Python File Location in Minitab

We’ll start by telling Minitab where to find your Python script, using this menu path: File > Options. In the new window, browse to the Default file location where you saved the script. Minitab will look for your Python script in the folder you specify. 

python-minitab-file-location-screenshot-updated

Make a New Custom Minitab Menu

  1. Choose View > Customize.

  2. On the Commands tab, from the Categories list, select New Menu, then from the Commands list, select New Menu.

  3. While the Customize dialog box is open, drag New Menu to where you want it to appear in the Minitab interface. When the insertion point appears, release the mouse button.
    Insertion point where your new custom Minitab menu will appear

  4. From the new menu's location in the Minitab interface, right-click New Menu and choose Name Button. Enter a name for your custom menu, for example, Python Scripts, and click OK.

    python-minitab-custom-menu

  5. Click Close.

Are you still with me? Nice work! You’re almost there!

 

Create a Minitab Exec Macro to Call Your Python Script

Now, open a new Windows notepad file and type in the following commands:

PYSC "QQ_plot.py"

This PYSC command tells Minitab to call Python, and to run the QQ plot script. PYSC is the Minitab command, and the value between the double-quotes is the name of the script you want to execute.

To save the commands in the Minitab exec, save the text file and title it QQ_Plot.MTB (be sure to save it with .MTB and not .txt). Save the file to the Default file location where the Python script is saved (the same location you set in Minitab using File > Options):

Be sure to save the file as .MTB, not .txt

Excellent job! Next, we’ll create a command to run a Minitab exec file.

 

Create a Command to Run a Minitab Exec File

  1. Choose View > Customize.

  2. In the Customize dialog box, click the Tools tab.

  3. On the Tools tab, click the New (Insert) button New (Insert) Button .

  4. Type a name for the command (I titled my command QQ plot C1 so that I’ll remember that the data for this command should be in C1 in the worksheet), then press the Enter key.

  5. Click the Open button (...).

  6. From the file type drop-down list, select All (*.*).

  7. Browse to and select the exec file titled QQ_plot.MTB that you saved previously:
    python-minitab-select-exec-dialog-update

Finally, Add your Macro to Your Custom Menu and Make Your Minitab/Python QQ Plot!

  1. Choose View > Customize.

  2. In the Customize dialog box, click the Commands tab.

  3. From the Categories list, select a category.

  4. From the Commands list, select the command to add.

  5. While the Customize dialog box is open, drag the selected command to your custom menu in the Minitab interface.

  6. Click Close.

Fantastic! Are you still with me, and are you ready to see the results?

After adding some data to the Minitab worksheet (in column C1 if you’re using the script shared in this post), click the QQ Plot C1 button you created in your custom menu on the right, Python Scripts

python-minitab-custom-menu-qq-plot-c1-1

Minitab will send your data to Python, then Python will execute the code and send the graph back to the Minitab output pane:

Minitab output from Python script

Way to go! Minitab’s new Python integration feature and free open-source code make it easy for people like us, that may want access to some extra features but aren’t particularly interested in learning to code.

And if you haven’t yet tried Minitab 19.2020.1, get your free 30-day trial today!

New call-to-action

New call-to-action
REGISTER NOW

Subscribe