Python 通过matplotlib绘制.ly烛台图

Python 通过matplotlib绘制.ly烛台图,python,matplotlib,graph,plotly,Python,Matplotlib,Graph,Plotly,通过以下步骤,我能够使用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) tup

通过以下步骤,我能够使用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,您可以使用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生成了以下内容,如果放大该图,可以看到该图实际上并没有显示蜡烛杆的主体,只是显示了上部和下部阴影。我导入的图表有误吗?或者plot.ly不支持蜡烛棒图形,即使它们是通过matplotlib生成的?

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

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

我实际上向plot.ly开发者发出了向股票添加蜡烛棒图表类型的请求plot.ly包。非常奇怪,它还没有被作为默认的“类型”包括进来

对于这个用例,我能够通过使用“OHLC”作为参数重新采样时间索引,将
熊猫数据框
拼凑在一起,构建自己的OHLC蜡烛。唯一需要注意的是,您需要所有资产交易的完整历史记录以及
DataFrame
索引的时间戳,才能正确构建此类图表:

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

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

我实际上向plot.ly开发人员发出了一个请求,要求将烛台图表类型添加到stock plot.ly包中。非常奇怪,它还没有被作为默认的“类型”包括进来

对于这个用例,我能够通过使用“OHLC”作为参数重新采样时间索引,将
熊猫数据框
拼凑在一起,构建自己的OHLC蜡烛。唯一需要注意的是,您需要所有资产交易的完整历史记录以及
DataFrame
索引的时间戳,才能正确构建此类图表:

newOHLC_dataframe = old_dataframe['Price'].astype(float).resample('15min', how='ohlc')
其中,
Price
键是所有y值。此
.resample()
将自行构建蜡烛并计算给定时间段的最大/最小/第一个/最后一个。在我上面的例子中,它将为您提供一个新的
数据帧
,由15分钟的蜡烛组成。
.astype(float)
可能是必需的,也可能不是必需的,这取决于您的基础数据