简体   繁体   中英

Histogram not plotting

Good afternoon,

I'm newbie here. I am trying to plot a histogram of spread returns, however, matplotlib rejects to plot the histogram without a corresponding error. Would you be so kind to explain, where is my mistake in the code. Thanks.

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')

That's is because you have inf values in your array.

You can fix this if you prevent 0 division:

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

or with:

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

Well I don't sure that you want to plot, but saw that you were zipping two list with two columns, then you can plot this with hist() There are another error in your code I change the line 10, goog['Ret']=((goog['Close'].shift(1))) and the line 40 by

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

The problem with this line 40 is parameter my_array .

a and b are dictionaries when you do my_array = [i/m for i,m in zip(a, b)] you're making a list without labels to the histogram.

Then finally the code is:

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()

I really hope this can help you.

Good luck, and if this help you don't forget accept the answer and/or vote up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM