user46147
user46147

Reputation: 234

How to pass a function with parameters into ternary plotting function?

I wrote the following code using package python-ternary, of which documentation you can see here.

import ternary
def myfun(p):

    return p[0]**2+p[1]**2+p[2]**2
def myfunParam(p,A,B,C):
    return p[0] ** A + p[1] ** B + p[2] ** C
def myfunParamBrute(p):
    A= 2; B=3; C=4
    return p[0] ** A + p[1] ** B + p[2] ** C

scale = 60
figure, tax = ternary.figure(scale=scale)
tax.heatmapf(myfunParamBrute, boundary=True, style="triangular")
tax.boundary(linewidth=2.0)
tax.set_title("My ternary function")

tax.show()

I would like to plot a function myfunParam while giving it some parameters as arguments.

I have no idea how to do it. Currently I do it using crude function myfunParamBrute, but this approach isn't scaleable if I want to make numerous plots.

How can I pass the arguments to the plotting function?

Upvotes: 1

Views: 59

Answers (1)

david
david

Reputation: 1501

Just nest your function in another one:

import ternary


def my_function(A, B, C):
    def inner(p):
        return p[0] ** A + p[1] ** B + p[2] ** C
    return inner

scale = 60
figure, tax = ternary.figure(scale=scale)

tax.heatmapf(my_function(2, 3, 4), boundary=True, style="triangular")
tax.boundary(linewidth=2.0)
tax.set_title("My ternary function")

tax.show()

Upvotes: 1

Related Questions