简体   繁体   中英

Percentage change to plot in histogram

I am trying to plot histogram of percentage change in stock my code looks like:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("M:/Trading/1.JOZO/ALXN.csv")
dataframe = (data['Adj Close'])
zmena1 = (dataframe.pct_change(periods = 1)*100)
data["Zmena"] = zmena1
plt.hist(zmena1, bins = "auto", range = "auto" )
plt.show

but i get an error:

 mn, mx = [mi + 0.0 for mi in range]

TypeError: Can't convert 'float' object to str implicitly

I tried str(zmena1) but can to get it... Do not know how to move through this one...

From the name of the csv file, I can guess that your data can be retrieved from Yahoo finance, so using the Remote Access datareader I'm downloading all 2016 data to play with:

import datetime
data = web.DataReader('ALXN', data_source='yahoo', 
                      start=datetime.datetime(2016, 1, 1))

Now I can calculate the percent change in the [0,100] range

data['Zmena'] = data['Adj Close'].pct_change(periods=1)*100

From there, I would definitely use the built-in DataFrame.hist function:

data['Zmena'].hist()

直方图


Using plt.hist

In case you do want to use plt.hist instead, you need to filter out the NaN (not a number), in particular you will always have one as the first entry:

print(data[['Adj Close','Zmena']].head())

             Adj Close     Zmena
Date                            
2016-01-04  184.679993       NaN
2016-01-05  184.899994  0.119126
2016-01-06  184.070007 -0.448884
2016-01-07  174.369995 -5.269741
2016-01-08  168.130005 -3.578592

So, in order to use plt.hist:

plt.hist(data.Zmena.dropna())

Another problem is that you're specifying bins = "auto", range = "auto" , when really you should just not pass them if you want to default. See the documentation for both parameters at pyplot.hist

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