在python中无法轻松获取变量的名称(参见此
answer).对于传递给python中的函数的变量,有使用inspect的hacky解决方案,详细信息
here以及基于此
answer的案例解决方案,
import matplotlib.pyplot as plt
import numpy as np
import inspect
import re
def comparigraphside(rawvariable, filtervariable, cut):
calling_frame_record = inspect.stack()[1]
frame = inspect.getframeinfo(calling_frame_record[0])
m = re.search( "comparigraphside\((.+)\)", frame.code_context[0])
if m:
rawvariablename = m.group(1).split(',')[0]
variable = rawvariable[filtervariable > 0]
filtervariable = filtervariable[filtervariable > 0]
upperbound = np.mean(variable) + 3*np.std(variable)
plt.figure(figsize=(20,5))
plt.subplot(121)
plt.hist(variable[filtervariable <= cut], bins=20, range=(0,upperbound), normed&#