Python熊猫直方图对数标度

Tri*_*ews 31 python pandas

我用pandas做了一个相当简单的直方图

results.val1.hist(bins=120)

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

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

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

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

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

Jan  9 15:53:07 BLARG.local python[6917] <Error>: CGContextClosePath: no current point.
Run Code Online (Sandbox Code Playgroud)

我得到一个对数比例直方图,但它只有条形的顶行,但没有垂直条纹或颜色.我做了一些可怕的错误或者这只是熊猫不支持吗?

从保罗H的代码我添加bottom=0.1hist调用修复问题,我想有一些除零事物或其他东西.

Pau*_*l H 50

没有任何数据很难诊断.以下适用于我:

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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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


Jea*_*die 46

我建议log=True在pyplot hist函数中使用该参数:

import matplotlib.pyplot as plt    
plt.hist(df['column_name'], log=True) 
Run Code Online (Sandbox Code Playgroud)

  • 这使得y轴成对数.那x轴怎么样? (18认同)

gre*_*ata 22

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

为了直接从熊猫构建直方图,无论如何都会将一些args传递给matplotlib.hist方法,因此:

results.val1.hist(bins = 120, log = True)
Run Code Online (Sandbox Code Playgroud)

会产生你需要的东西.