- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Pandas 数据框中有 float 据。每列代表一个变量(它们有字符串名称),每一行代表一组值(这些行有不重要的整数名称)。
>>> print data
0 kppawr23 kppaspyd
1 3.312387 13.266040
2 2.775202 0.100000
3 100.000000 100.000000
4 100.000000 39.437420
5 17.017150 33.019040
...
我想为每一列绘制直方图。我取得的最佳结果是使用 dataframe 的 hist 方法:
data.hist(bins=20)
但我希望每个直方图的 x 轴都在 log10 范围内。并且 bins 也在 log10 规模上,但这很容易使用 bins=np.logspace(-2,2,20)。
解决方法可能是在绘图之前对数据进行 log10 转换,但我已经尝试过这种方法,
data.apply(math.log10)
和
data.apply(lambda x: math.log10(x))
给我一个浮点错误。
"cannot convert the series to {0}".format(str(converter)))
TypeError: ("cannot convert the series to <type 'float'>", u'occurred at index kppawr23')
最佳答案
你可以使用
ax.set_xscale('log')
data.hist()
返回一个轴数组。你需要打电话ax.set_xscale('log')
为每个轴,ax
使每个对数缩放。
例如,
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(2015)
N = 100
arr = np.random.random((N,2)) * np.logspace(-2,2,N)[:, np.newaxis]
data = pd.DataFrame(arr, columns=['kppawr23', 'kppaspyd'])
bins = np.logspace(-2,2,20)
axs = data.hist(bins=bins)
for ax in axs.ravel():
ax.set_xscale('log')
plt.gcf().tight_layout()
plt.show()
产量
顺便说一句,要获取 DataFrame 中每个值的日志,data
,您可以使用
logdata = np.log10(data)
因为 NumPy ufunc(例如 np.log10
)可以应用于 pandas DataFrames,因为它们操作 elementwise on all the values in the DataFrame .
data.apply(math.log10)
不起作用,因为 apply
试图将整列(一系列)值传递给 math.log10
。 math.log10
只需要一个标量值。
data.apply(lambda x: math.log10(x))
失败的原因与 data.apply(math.log10)
相同。此外,如果 data.apply(func)
和 data.apply(lambda x: func(x))
都是可行的选项,那么第一个应该是首选,因为 lambda 函数只会让通话变慢一点。
您可以再次使用 data.apply(np.log10)
,因为 NumPy ufunc np.log10
可以应用于 Series,但没有理由麻烦在 np.log10(data)
工作时执行此操作。
您还可以使用 data.applymap(math.log10)
因为 applymap
调用math.log10
对 data
中的每个值一次一个。但这会慢得多而不是调用等效的 NumPy 函数 np.log10
数据框。尽管如此,还是值得了解一下 applymap
以防您需要调用一些不是 ufunc 的自定义函数。
关于python - 想要将 Pandas Dataframe 绘制为具有 log10 比例 x 轴的多个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930340/
不同的 LogCat 方法是: Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e();
在android群里,经常会有人问我,android log是怎么用的,今天我就把从网上以及sdk里东拼西凑过来,让大家先一睹为快,希望对大家入门android log有一定的帮助. android
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 社区 12
我正在尝试使用 sonarlint 检查代码质量.上面的问题概要,我不明白为什么它要说要大写。但是 this discussion与上述建议相反。哪一个应该被认为是正确的? 最佳答案 这没有正确答案,
随着 n 变大,log*(log n) 和 log(log* n) 这两个函数会更快吗? 这里,log* 函数是迭代对数,定义如下: 我怀疑它们是相同的,只是写法不同,但它们之间有什么区别吗? 最佳答
作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + l
我正在使用 Tomee。日志文件夹包含这样的文件 localhost_access_log.2016-12-02.txt localhost.2016-12-02.log catalina.2016-
Android Log.v、Log.d、Log.i、Log.e 等的 ios 等效项是什么?同样在 android 上,我使用 Android 设备监视器和 logcat 来访问我的手机日志,我需要在
我认为下面的代码是 O(log log n) 因为它里面有 i*i 但我对 log n 感到困惑> 和 log (log n)。 for (i=2; i*i<=number; i++) { if
我正在修改 kvm 模块,并在内核代码中添加了 printk 语句。运行虚拟机后,printk 为我提供了错误地址和有关 guest 操作系统的其他信息。 我需要从这个信息中生成统计信息。当我使用 d
我有一个部署为 Windows Azure Web 角色的 WCF 服务。 我正在使用 Enterprise Library 进行异常处理,并且在我的本地 Development Fabric 中,似
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
在 Go 的生产中使用 log.SetFlags(log.LstdFlags | log.Lshortfile) 是好的做法(至少是一般做法)吗?我想知道在生产中这样做是否存在性能或安全问题。因为它不
我想知道什么更快: double value = Math.log(a) - Math.log(b); 或 double value = Math.log(a/b); 我计算值的方式是否会对性能产生影
我有数百个子例程使用 log.Println() 写入日志文件 我正在使用 log.Println 写入 error.log 文件。 func main() { e, err := os.Open
我将 Nuxt 与 SSR 一起使用,并希望有类似于 apaches 的 access.log 和 error.log 的东西 我特别感兴趣的是每次调用的响应时间。 我在 nuxt 文档中找不到任何内
我知道以前有人问过这个问题,但我相信这是一个不同的问题。 Nginx 在 www-data 下运行: $ ps -eo "%U %G %a" | grep nginx root root
我在我的日志文件中发现了一个非常奇怪的条目 Jan 29 01:35:30 vs-proj-handy sshd[5316]: Received disconnect from 130.207.203
对于我正在开发的应用程序,我希望在开发过程中和发布时简化故障排除。我希望能够检索到对 Log 的调用,以了解在 USB 调试中没有连接手机的情况下运行应用程序时的调用,以便可以检索并发送给我。例如,当
我试图捕获 panic 并记录错误: func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRep
我是一名优秀的程序员,十分优秀!