【问题标题】:How to print variable name as a title in matplotlib如何在 matplotlib 中将变量名称打印为标题
【发布时间】:2015-09-29 10:58:32
【问题描述】:

我的目标是创建一个简单的函数,用已绘制变量的名称为图形命名。

到目前为止我有:

def comparigraphside(rawvariable, filtervariable, cut):
    variable = rawvariable[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=True)
    plt.title("%s customers with filter less than or equal to %s" % (len(variable[filtervariable <= cut]), cut))
    plt.subplot(122)
    plt.hist(variable[filtervariable > cut], bins=20, range=(0,upperbound), normed=True)
    plt.title("%s customers with filter greater than %s" % (len(variable[filtervariable > cut]), cut));

它在哪里:

plt.title("%s customers with filter less/greater...") 

我很想说:

plt.title("%s customers with %s less/greater...")

目前我能想到的唯一解决方案是制作一个变量字典,我想避免这种情况。非常感谢任何和所有的帮助。

【问题讨论】:

标签: python matplotlib


【解决方案1】:

在 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=True)
    title = "%s customers with %s less than or equal to %s" % (len(variable[filtervariable <= cut]), rawvariablename, cut)
    plt.title(title)
    plt.subplot(122)
    plt.hist(variable[filtervariable > cut], bins=20, range=(0,upperbound), normed=True)
    plt.title("%s customers with %s greater than %s" % (len(variable[filtervariable > cut]), rawvariablename, cut));


#A solution using inspect
normdist = np.random.randn(1000)
randdist = np.random.rand(1000)

comparigraphside(normdist, normdist, 0.7)
plt.show()

comparigraphside(randdist, normdist, 0.7)
plt.show()

但是,另一种可能的solution,在您的情况下可能更简洁,是在您的函数中使用**kwargs,然后在命令行上定义的变量名称将被打印出来,例如,

import matplotlib.pyplot as plt
import numpy as np

normdist = np.random.randn(1000)
randdist = np.random.rand(1000)

#Another solution using kwargs
def print_fns(**kwargs):
    for name, value in kwargs.items():
        plt.hist(value)
        plt.title(name)

print_fns(normal_distribution=normdist)
plt.show()

print_fns(random_distribution=randdist)
plt.show()

就个人而言,除了快速绘图脚本之外,我会定义一个包含您要绘制的所有变量的字典,并为每个变量命名,然后将其传递给函数。如果将此绘图用作较大代码的一部分,这将更加明确并确保您没有问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多