python画图将标题中有变量_如何在matplotlib中打印变量名作为标题
在python中不可能轻松地获取变量的名称(请参见answer)。对于在python中传递给函数的变量,有使用inspect、详细信息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()
就我个人而言,除了一个快速绘图脚本之外,我会定义一个包含所有要绘图变量的字典,并为每个变量命名,然后将其传递到函数中。这是更明确的,并确保您没有问题,如果您使用这个绘图作为一个更大的代码的一部分。。。
python画图将标题中有变量_如何在matplotlib中打印变量名作为标题相关推荐
- word怎么打印网格线_如何在Excel中打印网格线以及行标题和列标题
word怎么打印网格线 Gridlines and the row and column headings can be helpful when viewing data on worksheets ...
- python画图修改背景颜色_如何在 Matplotlib 中更改绘图背景的实现
介绍 Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库. 在本教程中,我们将研究如何在Matplotlib中更改绘图的背景. 导 ...
- python绘图背景透明_如何在 Matplotlib 中更改绘图背景
介绍Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库.在本教程中,我们将研究如何在Matplotlib中更改绘图的背景.导入数据 ...
- linux中设置环境变量_如何在Linux中设置环境变量
linux中设置环境变量 Wondering how to set environment variables in Linux? This is exactly what we'll be doin ...
- activiti 变量_如何在Activiti中使用瞬态变量
activiti 变量 我们昨天发布的Activiti v6 Beta3中已经加入了很多需要的功能-临时变量. 在这篇文章中,我将向您展示一个示例,说明如何使用瞬态变量来覆盖一些以前不可能(或最佳)的 ...
- scala中打印数组和集合_如何在Scala中打印数组?
scala中打印数组和集合 Scala中的数组 (Array in Scala) In Scala, Array is a data structure that is a collection of ...
- python的matplotlib库内的函数_如何在matplotlib中找到函数下面的区域?
我是python和matplotlib库的新手,我试图在绘图中得到函数行下方的区域.我有一个变量a&:b,它在我的绘图中移动一个矩形.我也许可以使用原始数学来解决这个问题,但我想知道是否 ...
- python三维图形旋转_如何在matplotlib中旋转三维曲面
在我的评论之后:import mayavi.mlab as mlab import numpy as np x,y = np.mgrid[-1:1:0.001, -1:1:0.001] z = x** ...
- python当输入负数时结束_如何在lis中输入负数
我需要一些帮助,因为每当我输入一个负数时,列表会将它解释为一个单独的元素,所以一旦它开始排序,它就会把所有的负数符号放在开头.代码的最终目标是在不使用默认排序函数的情况下对两个合并列表进行排序.另外, ...
最新文章
- python第二阶段 第六天 模块
- WPF窗体置于桌面最底层
- Centos DNS服务器搭建
- [LeetCode] Isomorphic Strings - 字符串操作:数组计数字符个数问题
- Qt经验积累:常见的驱动打包处理方法
- 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据
- Shell入门(七)之关系运算
- HTML5学习笔记(一):初步印象
- 鸿蒙系统开发者如何加入,鸿蒙开发实战系列之五:鸿蒙系统原生数据库
- android olhttp 缓存,android okhttp 缓存设置
- Maven系列一pom.xml 配置详解
- 礼县职业中等专业学校计算机,礼县职业中等专业学校
- rust-vmm 学习
- 2018届华为网络技术大赛复赛组网(B)试题
- 电感电容阻抗和公式记录
- 云计算系列知识点——网络(公网、私网、VPN等)基础知识与计费模式
- psp记忆棒测试软件,乱花渐欲迷人眼——PSP用记忆棒选购指南
- 公司产品移动端网页-前端网页设计技术精美网站源码HTML+CSS+JS
- ca机构将会被区块链取代
- hive 开窗函数OVER(PARTITION)详解(一)