【问题标题】:Histogram not plotting直方图未绘制
【发布时间】:2015-06-30 01:09:56
【问题描述】:

下午好,

我是这里的新手。我正在尝试绘制价差收益的直方图,但是,matplotlib 拒绝绘制直方图而没有相应的错误。你能解释一下,我在代码中的错误在哪里。谢谢。

import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib.pyplot as plt

goog = web.DataReader('GOOG', data_source='google',
start='3/14/2009', end='4/14/2014')
goog.tail()
goog['Ret'] = ((goog['Close'] - goog['Close'].shift(1)) /
                goog['Close'].shift(1))*100


goog[['Close','Ret']].tail()

WY = web.DataReader('WY', data_source='google',
start='3/14/2009', end='4/14/2014')
WY.tail()
WY['Ret'] = ((WY['Close'] - WY['Close'].shift(1)) /WY['Close'].shift(1))*100
WY[['Close','Ret']].tail()

a=goog['Ret']
a = a[~np.isnan(a)]
b=WY['Ret']
b = b[~np.isnan(b)]

%matplotlib inline 


my_array = [i/m for i,m in zip(a, b)]


plt.hist(my_array, bins=25)
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')

【问题讨论】:

  • 我怀疑这是因为您需要告诉 matplotlib 向您展示您的身材。尝试在底部添加plt.show()

标签: python matplotlib plot histogram


【解决方案1】:

那是因为您的数组中有 inf 值。

如果您防止 0 除法,您可以解决此问题:

my_array = [i/m for i,m in zip(a, b) if m!=0]

或与:

my_array = my_array[~np.isinf(my_array)]

【讨论】:

    【解决方案2】:

    好吧,我不确定您是否要绘制,但看到您正在压缩两个包含两列的列表,那么您可以使用 hist() 绘制它 您的代码中还有另一个错误我更改了第 10 行,goog['Ret']=((goog['Close'].shift(1))) 和第 40 行由

    plt.hist(a, bins=25)
    plt.hist(b, bins=25)
    

    第 40 行的问题在于参数my_array

    ab 是字典,当您执行 my_array = [i/m for i,m in zip(a, b)] 时,您正在为直方图制作一个没有标签的列表。

    那么最后代码是:

    import numpy as np
    import pandas as pd
    import pandas.io.data as web
    import matplotlib.pyplot as plt
    
    goog = web.DataReader('GOOG', data_source='google',
    start='3/14/2009', end='4/14/2014')
    goog.tail()
    #print(goog['Ret'])
    goog['Ret']=((goog['Close'].shift(1)))
    #goog['Close'].shift(1))*100
    
    #print(((goog['Close'].shift(1)),goog['Close'].shift(1))*100)
    #print(((goog['Close'].shift(1))))#,goog['Close'].shift(1)))
    
    
    goog[['Close','Ret']].tail()
    
    WY = web.DataReader('WY', data_source='google',
    start='3/14/2009', end='4/14/2014')
    WY.tail()
    WY['Ret'] = ((WY['Close'] - WY['Close'].shift(1)) /WY['Close'].shift(1))*100
    WY[['Close','Ret']].tail()
    
    a=goog['Ret']
    a = a[~np.isnan(a)]
    b=WY['Ret']
    b = b[~np.isnan(b)]
    
    #matplotlib inline 
    
    
    my_array = [i/m for i,m in zip(a, b) if m!=0] # This works too
    
    #print (a)
    #print (b)
    
    
    plt.hist(my_array, bins=25)
    #plt.hist(a, bins=25)
    #plt.hist(b, bins=25)
    plt.grid(True)
    plt.legend(loc=0)
    plt.xlabel('value')
    plt.ylabel('frequency')
    plt.title('Histogram')
    plt.show()
    

    我真的希望这可以帮助你。

    祝你好运,如果这有助于你不要忘记接受答案和/或投票。

    【讨论】:

    • 恐怕没有意义。不管这个事实你重复使用我的答案。
    • 我想帮忙。我认为社区的目标是共同解决问题。我更正了原始代码,然后我认为您也使用了我的更正,因为原始代码没有运行。但我认为重要的是解决问题。
    • 对不起,我没有建设性,但你的回答并没有回答这个问题。您的代码可能有效,但这是因为您删除了您不理解的部分代码,并且因为您应用了我的答案,就好像它是您的一样(有问题)。除此之外,你的解释说明你不明白你在做什么。
    猜你喜欢
    • 2012-04-13
    • 2017-10-23
    • 2012-09-13
    • 1970-01-01
    • 2018-05-23
    • 2015-07-12
    • 2015-08-26
    • 2020-04-27
    相关资源
    最近更新 更多