XLPack for Matplotlib User’s Manual

1. Introduction

XLPack for Matplotlib (hereinafter abbreviated as XLPack MPL) is the program that enable calling Matplotlib from Excel VBA.

It maps the major classes and methods of Matplotlib to VBA classes and methods, enabling you to use Matplotlib by entirely using VBA programs.

This program is currently under development as the visualization component for performing calculations to displaying results in Excel, in combination with the numerical calculation library XLPack.

The unimplemented Matplotlib classes and methods are planned to be be added progressively, but you may also supplement those functions by using the separately provided XLPack for Python.

2. Example program

Let’s look at a simple example to get a better idea of how it works.

The following is an example of plotting a sine curve using pyplot.plot. In Python, it is written as follows.

import matplotlib.pyplot as plt
import numpy as np
import math
# make data
t = np.linspace(0, 2, 201)
s = 1 + np.sin(2 * math.pi * t)
# plot
plt.plot(t, s, '-b')
plt.xlabel("time (s)")
plt.ylabel("voltage (mV)")
plt.title("About as simple as it gets, folks")
plt.grid()
plt.show()

The equivalent program can be written in VBA as follows.

Dim Plt As New PyPlot

Function Pi() As Double
    Pi = 4 * Atn(1)
End Function

Sub Linspace(A As Double, B As Double, N As Long, X() As Double)
    Dim I As Long
    For I = 0 To N - 1
        X(I) = A + I * ((B - A) / (N - 1))
    Next
End Sub

Sub Test_Plt_Plot()
    Const N = 201
    Dim T(N - 1) As Double, S(N - 1) As Double
    Dim I As Long
    Call Linspace(0, 2, N, T())
    For I = 0 To N - 1
        S(I) = 1 + Sin(2 * Pi() * T(I))
    Next
    Call Plt.Plot(N, T(), S(), "-b")
    Call Plt.Xlabel("time (s)")
    Call Plt.Ylabel("voltage (mV)")
    Call Plt.Title("About as simple as it gets, folks")
    Call Plt.Grid
    Call Plt.Show
End Sub

In VBA, methods for Pyplot cannot be called without creating an instance like Python, and then a Pyplot object must be created once somewhere in your VBA program using “Dim Plt As New PyPlot”. After that, the VBA program can be coded in the same way as in Python.

The execution results for both Python and VBA programs are as follows.

The same example can also be written using objects instead of Pyplot. The main program would look like as follows.

Sub Test_Plot()
    Const N = 201
    Dim T(N - 1) As Double, S(N - 1) As Double
    Dim I As Long
    Dim Fig As Figure, Ax As Axs
    Call Linspace(0, 2, N, T())
    For I = 0 To N - 1
        S(I) = 1 + Sin(2 * Pi() * T(I))
    Next
    Set Fig = Plt.Figure()
    Set Ax = Fig.Gca()
    Call Ax.Plot(N, T(), S(), "-b")
    Call Ax.Set_xlabel("time (s)")
    Call Ax.Set_ylabel("voltage (mV)")
    Call Ax.Set_title("About as simple as it gets, folks")
    Call Ax.Grid
    Call Plt.Show
End Sub

3. VBA classes and methods for using Matplotlib

Currently, only a limited number of classes and methods are implemented. See below for details.

Overview
Details of VBA classes and methods
Example programs