7

我想根据我使用熊猫从雅虎下载的数据创建一个每日烛台图。我无法弄清楚如何在这种情况下使用烛台 matplotlib 函数。这是代码:

#The following example, downloads stock data from Yahoo and plots it.
from pandas.io.data import get_data_yahoo
import matplotlib.pyplot as plt

from matplotlib.pyplot import subplots, draw
from matplotlib.finance import candlestick

symbol = "GOOG"

data = get_data_yahoo(symbol, start = '2013-9-01', end = '2013-10-23')[['Open','Close','High','Low','Volume']]

ax = subplots()

candlestick(ax,data['Open'],data['High'],data['Low'],data['Close'])

谢谢

安德鲁。

4

5 回答 5

8

使用散景:

import io
from math import pi
import pandas as pd
from bokeh.plotting import figure, show, output_file

df = pd.read_csv(
    io.BytesIO(
        b'''Date,Open,High,Low,Close
2016-06-01,69.6,70.2,69.44,69.76
2016-06-02,70.0,70.15,69.45,69.54
2016-06-03,69.51,70.48,68.62,68.91
2016-06-04,69.51,70.48,68.62,68.91
2016-06-05,69.51,70.48,68.62,68.91
2016-06-06,70.49,71.44,69.84,70.11
2016-06-07,70.11,70.11,68.0,68.35'''
    )
)

df["Date"] = pd.to_datetime(df["Date"])

inc = df.Close > df.Open
dec = df.Open > df.Close
w = 12*60*60*1000

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title
= "Candlestick")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3

p.segment(df.Date, df.High, df.Date, df.Low, color="black")
p.vbar(df.Date[inc], w, df.Open[inc], df.Close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.Date[dec], w, df.Open[dec], df.Close[dec], fill_color="#F2583E", line_color="black")

output_file("candlestick.html", title="candlestick.py example")

show(p)

来自 Pandas DataFrame 的烛台图

上面的代码来自这里: http ://docs.bokeh.org/en/latest/docs/gallery/candlestick.html

于 2017-02-23T22:10:26.913 回答
5

我没有评论@randall-goodwin 答案的声誉,但对于 pandas 0.16.2 行:

# convert the datetime64 column in the dataframe to 'float days'
data.Date = mdates.date2num(data.Date)

一定是:

data.Date = mdates.date2num(data.Date.dt.to_pydatetime())

因为 matplotlib 不支持 numpy datetime64 dtype

于 2015-06-15T11:48:34.797 回答
3

我偶然发现了一个很棒的 pastebin 条目:http ://pastebin.com/ne7Fjdiq ,它做得很好。我也很难正确调用语法。它通常围绕以简单的方式转换数据以使功能正常工作。我的问题是日期时间。我的格式数据中一定有什么东西。一旦我用 range(maxdata) 替换了 Date 系列,它就起作用了。

data = pandas.read_csv('data.csv', parse_dates={'Timestamp': ['Date', 'Time']}, index_col='Timestamp')
ticks = data.ix[:, ['Price', 'Volume']]
bars = ticks.Price.resample('1min', how='ohlc')
barsa = bars.fillna(method='ffill')
fig = plt.figure()
fig.subplots_adjust(bottom=0.1)
ax = fig.add_subplot(111)
plt.title("Candlestick chart")
volume = ticks.Volume.resample('1min', how='sum')
value = ticks.prod(axis=1).resample('1min', how='sum')
vwap = value / volume
Date = range(len(barsa))
#Date = matplotlib.dates.date2num(barsa.index)#
DOCHLV = zip(Date , barsa.open, barsa.close, barsa.high, barsa.low, volume)
matplotlib.finance.candlestick(ax, DOCHLV, width=0.6, colorup='g', colordown='r', alpha=1.0)
plt.show()
于 2014-07-30T18:46:09.097 回答
2

这是解决方案:

from pandas.io.data import get_data_yahoo
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
from matplotlib import ticker as mticker
from matplotlib.finance import candlestick_ohlc
import datetime as dt
symbol = "GOOG"

data = get_data_yahoo(symbol, start = '2014-9-01', end = '2015-10-23')
data.reset_index(inplace=True)
data['Date']=mdates.date2num(data['Date'].astype(dt.date))
fig = plt.figure()
ax1 = plt.subplot2grid((1,1),(0,0))
plt.ylabel('Price')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(6))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))

candlestick_ohlc(ax1,data.values,width=0.2)
于 2016-07-26T04:24:21.997 回答
1

当我也在寻找如何将烛台与从 get_data_yahoo 等 DataReader 服务之一返回的 pandas 数据帧一起使用时发现了这个问题。我最终想通了。Wes McKinney 和 RJRyV 回答了另一个问题,其中一个关键是。这是那个链接:

Pandas 将数据帧转换为元组数组

关键是阅读烛台.py 函数定义以确定它预期如何接收数据。首先需要转换日期,然后需要将整个数据帧转换为元组数组。

这是对我有用的最终代码。也许在某个地方还有其他一些烛台图表可以直接在从股票报价服务之一返回的 pandas 数据帧上工作。那肯定很不错。

# Imports
from pandas.io.data import get_data_yahoo
from datetime import datetime, timedelta
import matplotlib.dates as mdates
from matplotlib.pyplot import subplots, draw
from matplotlib.finance import candlestick
import matplotlib.pyplot as plt

# get the data on a symbol (gets last 1 year)
symbol = "TSLA"
data = get_data_yahoo(symbol, datetime.now() - timedelta(days=365))

# drop the date index from the dateframe
data.reset_index(inplace = True)

# convert the datetime64 column in the dataframe to 'float days'
data.Date = mdates.date2num(data.Date)

# make an array of tuples in the specific order needed
dataAr = [tuple(x) for x in data[['Date', 'Open', 'Close', 'High', 'Low']].to_records(index=False)]

# construct and show the plot
fig = plt.figure()
ax1 = plt.subplot(1,1,1)
candlestick(ax1, dataAr)
plt.show()
于 2014-06-01T04:15:18.437 回答