Python Pandas 直方图对数刻度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21033720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:51:31  来源:igfitidea点击:

Python Pandas Histogram Log Scale

pythonpandas

提问by TristanMatthews

I'm making a fairly simple histogram in with pandas using

我正在使用熊猫制作一个相当简单的直方图

results.val1.hist(bins=120)

results.val1.hist(bins=120)

which works fine, but I really want to have a log scale on the y axis, which I normally (probably incorrectly) do like this:

这工作正常,但我真的想在 y 轴上有一个对数刻度,我通常(可能不正确)这样做:

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
plt.plot(np.random.rand(100))
ax.set_yscale('log')
plt.show()

If I replace the pltcommand with the pandas command, so I have:

如果我用pltpandas 命令替换命令,那么我有:

fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
results.val1.hist(bins=120)
ax.set_yscale('log')
plt.show()

results in many copies of the same error:

导致相同错误的许多副本:

Jan  9 15:53:07 BLARG.local python[6917] <Error>: CGContextClosePath: no current point.

I do get a log scale histogram, but it only has the top lines of the bars, but no vertical bars or colors. Am doing something horribly wrong or is this just not supported by pandas?

我确实得到了一个对数刻度直方图,但它只有条形的顶线,但没有垂直条形或颜色。是做错了什么,还是熊猫不支持?

From Paul H's code I added bottom=0.1to histcall fixes the problem, I guess there is some kind of divide by zero thing, or something.

从 Paul H 的代码中,我添加bottom=0.1hist调用修复了问题,我猜有某种除以零的东西,或者其他东西。

采纳答案by Paul H

Hard to diagnose without any data. The following works for me:

没有任何数据很难诊断。以下对我有用:

import numpy as np
import matplotlib.pyplot as plt
import pandas
series = pandas.Series(np.random.normal(size=2000))
fig, ax = plt.subplots()
series.hist(ax=ax, bins=100, bottom=0.1)
ax.set_yscale('log')

enter image description here

在此处输入图片说明

The key here is that you pass axto the histogram function and you specify the bottomsince there is no zero value on a log scale.

这里的关键是您传递ax给直方图函数并指定 ,bottom因为在对数刻度上没有零值。

回答by Jean Pouget-Abadie

I'd recommend using the log=Trueparameter in the pyplot hist function:

我建议使用log=Truepyplot hist 函数中的参数:

import matplotlib.pyplot as plt    
plt.hist(df['column_name'], log=True) 

回答by greg_data

Jean PA's solution is the simplest, most correct one for this question. Writing this as an answer since I don't have the rep to comment.

Jean PA 的解决方案是这个问题最简单、最正确的解决方案。写这个作为答案,因为我没有代表发表评论。

For constructing a histogram straight from pandas, some of the args are passed on to the matplotlib.hist method anyway, so:

为了直接从 Pandas 构建直方图,一些参数无论如何都会传递给 matplotlib.hist 方法,所以:

results.val1.hist(bins = 120, log = True)

Would produce what you need.

会生产你需要的东西。