[英]Python Pandas Histogram Log Scale
我用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()
如果我用pandas命令替換plt
命令,那么我有:
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111)
results.val1.hist(bins=120)
ax.set_yscale('log')
plt.show()
導致同一錯誤的許多副本:
Jan 9 15:53:07 BLARG.local python[6917] <Error>: CGContextClosePath: no current point.
我得到一個對數比例直方圖,但它只有條形的頂行,但沒有垂直條紋或顏色。 我做了一些可怕的錯誤或者這只是熊貓不支持嗎?
從Paul H的代碼中我添加了bottom=0.1
到hist
調用修復了這個問題,我想有一些除零事物或其他東西。
沒有任何數據很難診斷。 以下適用於我:
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')
這里的關鍵是你將ax
傳遞給直方圖函數並指定bottom
因為對數刻度上沒有零值。
我建議在pyplot hist函數中使用log=True
參數:
import matplotlib.pyplot as plt
plt.hist(df['column_name'], log=True)
Jean PA的解決方案是這個問題中最簡單,最正確的解決方案。 寫這個作為答案,因為我沒有代表評論。
為了直接從熊貓構建直方圖,無論如何都會將一些args傳遞給matplotlib.hist方法,因此:
results.val1.hist(bins = 120, log = True)
會產生你需要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.