【问题标题】:unexpected oscillations when using pandas plot()使用 pandas plot() 时出现意外振荡
【发布时间】:2013-11-30 22:28:43
【问题描述】:

我从网上获取了一些数据并将其加载到 pandas 数据帧中

import pandas as pd
%pylab inline

loc = 'https://blockchain.info/charts/hash-rate?showDataPoints=false&timespan=all&show_header=true&daysAverageString=1&scale=1&format=csv&address='
df = pd.read_csv(loc, parse_dates = True, 
                 index_col = 0, skiprows = 1, 
                 names = ['Date', 'Hash Rate (Gh/s)'])

然后我尝试使用 pandas df.plot 命令绘制它

df['Hash Rate (Gh/s)'].plot(logy = True)

我收到的情节有意外的波动

但是如果我用 matplotlib 绘制相同的数据

plt.semilogy(df['Hash Rate (Gh/s)'])

没有这些振荡。

我已尝试使用 pandas 重新索引功能

df_idx = pd.date_range(df.index[0], df.index[-1])
df = df.reindex(df_idx, fill_value=nan) 

但到目前为止还没有找到任何方法来消除情节中的这些虚假振荡。如何消除这些振荡或在 pandas 中重新索引以消除它们?

【问题讨论】:

    标签: python matplotlib plot pandas


    【解决方案1】:

    您的日期没有被正确解析:它们首先有天数。如果你将dayfirst=True 传递给read_csv,它应该可以解决问题。

    In [6]: df = pd.read_csv("ooo.csv", skiprows=1, names=['Date', 'Hash Rate (Gh/s)'], parse_dates=True, index_col=0, dayfirst=True)
    
    In [7]: df.head(10)
    Out[7]: 
                         Hash Rate (Gh/s)
    Date                                 
    2009-01-04 18:15:05          0.000000
    2009-01-05 18:15:05          0.000000
    2009-01-06 18:15:05          0.000000
    2009-01-07 18:15:05          0.000000
    2009-01-08 18:15:05          0.000000
    2009-01-09 18:15:05          0.000696
    2009-01-10 18:15:05          0.001541
    2009-01-11 18:15:05          0.005269
    2009-01-12 18:15:05          0.004424
    2009-01-13 18:15:05          0.005717
    
    [10 rows x 1 columns]
    
    In [8]: !head ooo.csv
    03/01/2009 18:15:05,0.00004971026962962963
    04/01/2009 18:15:05,0.0
    05/01/2009 18:15:05,0.0
    06/01/2009 18:15:05,0.0
    07/01/2009 18:15:05,0.0
    08/01/2009 18:15:05,0.0
    09/01/2009 18:15:05,0.0006959437748148148
    10/01/2009 18:15:05,0.0015410183585185184
    11/01/2009 18:15:05,0.005269288580740741
    12/01/2009 18:15:05,0.004424213997037036
    
    In [9]: df["Hash Rate (Gh/s)"].plot(logy=True)
    Out[9]: <matplotlib.axes._subplots.AxesSubplot at 0xc4ea58c>
    

    生产

    【讨论】:

    • 谢谢。非常简单的解决方案。我希望我从输出中清楚地知道日期时间格式刚刚关闭。
    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2023-02-07
    • 2019-01-24
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多