如何使用Python的matplotlib烛台仅绘制工作日?

时间:2012-05-10 07:42:12

标签: python matplotlib candlestick-chart

我没有设法在没有周末的情况下绘制matplotlib.finance.candlestick(每5个烛台之间有空格)。 example from Matplotlib's website也不排除周末,the way to exclude weekends on other plots似乎不适用于CandleSticks。

以前有人遇到过这个吗?

PS。根据要求,这是一个例子:

#!/usr/bin/env python
from pylab import *
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
 DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
 plot_day_summary, candlestick2

# (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')  # Eg, Jan 12
dayFormatter = DateFormatter('%d')      # Eg, 12

quotes = quotes_historical_yahoo('INTC', date1, date2)

fig = figure()
fig.subplots_adjust(bottom=0.2)
ax = fig.add_subplot(111)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)

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

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

show()

2 个答案:

答案 0 :(得分:4)

在你的'引号'之后:

weekday_quotes = [tuple([i]+list(quote[1:])) for i,quote in enumerate(quotes)]

然后

candlestick(ax, weekday_quotes, width=0.6)

这将绘制数据,没有工作日之间的差距,现在你必须将xticks更改回日期,最好是星期一。假设你的第一句话是星期一:

import matplotlib.dates as mdates

ax.set_xticks(range(0,len(weekday_quotes),5))
ax.set_xticklabels([mdates.num2date(quotes[index][0]).strftime('%b-%d') for index in ax.get_xticks()])

这非常严重,但似乎完成了工作 - 祝你好运!

答案 1 :(得分:0)

@JMJR的答案有效时,我发现它更可靠:

<div class="c">
<div class="a">
-----
</div>
<div class="b">
-----
</div>
</div>