python - 使用 pandasplot() 时出现意外振荡

标签 python matplotlib plot pandas

我从网络上获取了一些数据并将其加载到 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)

我收到的图有意外的振荡 made with pandas

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

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

made with matplotlib

没有这些振荡。

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

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

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

最佳答案

您的日期未正确解析:它们先有天。如果您将 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>

产生

figure w/o oscillations

关于python - 使用 pandasplot() 时出现意外振荡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20306366/

相关文章:

python - Zenity 与 iPython?

Python Tkinter 从标签中删除/删除图像

python - 在条形图中使用 yerr 以及将数据透视表与 Pandas 一起使用时出现 ValueError

python - 使用 pcolor 叠加两个图

python - 如何在 matplotlib 中美观地显示通用数量的轴?

python - 如何在python动画中添加图例标签

Python:如何使用 matplotlib 将图像像素投影到 map 上

python - 使用 Django 模型作为另一个模型的字段?

MATLAB 填充线之间的区域

java - 简单的 Java 绘图,其中 x-y 数据在另一个类中计算