将数字传递到数学环境图标签中

问题描述 投票:0回答:2
def function(param):
    x = np.linspace(0,param,10)
    plt.plot(x,x,label = "some label that includes the parameter like" + r"$A_{param}$"
    plt.legend()
    plt.show()

当我有这样的功能时,如何使这个标签工作?

python math plot
2个回答
0
投票

您可以将参数传递到标签的数学环境,如下所示:

def function(param):
    x = np.linspace(0, param, 10)
    plt.plot(x, x, label="some label that includes the parameter like " + r"$A_{%d}$"%param)
    plt.legend()
    plt.show()

例如,将您的函数调用为function(100)将产生以下结果:enter image description here

顺便说一句,function是一个内置名称。最好不要将python内置名称用作变量或函数名称。


0
投票

尝试类似的东西

param = 5
'{{{}}}'.format(param)

> '{5}'
© www.soinside.com 2019 - 2024. All rights reserved.