plot.ly烛台图来自matplotlib

时间:2015-05-26 19:04:01

标签: python matplotlib graph plotly

使用以下example我可以使用matplotlib创建蜡烛棒图。

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator,\
     DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc


# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2004, 2, 1)
date2 = (2004, 4, 12)


mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12

quotes = quotes_historical_yahoo_ohlc('INTC', date1, date2)
if len(quotes) == 0:
    raise SystemExit

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)

#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, quotes, width=0.6)

ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()

观看plot.ly' APIs你可以使用plot.ly在线发布matplotlib数据。我在上面的代码中添加了以下内容:

import matplotlib.mlab as mlab
import plotly.plotly as py

py.sign_in('xxx', 'xxxx')
plot_url = py.plot_mpl(fig)

plot.ly产生了以下graph,如果你放大到图形,你可以看到图形实际上并没有显示蜡烛的主体只是上下阴影。我是否错误地导入了图表?或者即使它们是通过matplotlib生成的,它们也不支持蜡烛图吗?

2 个答案:

答案 0 :(得分:2)

自2015年5月27日发现Plot.ly目前不支持蜡烛棒。

答案 1 :(得分:2)

我实际上向plot.ly开发人员请求将烛台图表类型添加到stock plot.ly包中。非常惊讶它仍然没有被列为默认"类型"

对于这个用例,我能够通过使用' ohlc'将pandas DataFrame与重新采样的时间索引混合在一起来构建我自己的OHLC蜡烛。作为参数。唯一需要注意的是,您需要有DataFrame索引时间戳的所有资产交易的完整历史记录才能正确构建此类图表:

newOHLC_dataframe = old_dataframe['Price'].astype(float).resample('15min', how='ohlc')

其中Price键是您的所有y值。这个.resample()将构建蜡烛并计算出给定时间段内的最大/最小/第一/最后一个。在上面的示例中,它将为您提供一个由15分钟蜡烛组成的新DataFrame.astype(float)可能需要也可能不需要,具体取决于您的基础数据。