6

没有周末,我无法绘制 matplotlib.finance.candlestick(每 5 个烛台之间的空白)。Matplotlib 网站上的示例也不排除周末,并且在其他地块上排除周末的方法似乎不适用于 CandleSticks。

有没有人遇到过这个?

附言。根据要求,这是示例:

#!/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()
4

2 回答 2

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()])

这很恶心,但似乎完成了工作 - 祝你好运!

于 2012-05-24T03:02:11.853 回答
0

虽然@JMJR 的回答有效,但我发现这更可靠:

def plot(x):
    plt.figure()
    plt.title("VIX curve")
    def idx(val=[0]):
        val[0] = val[0] + 1
        return val[0]
    d = collections.defaultdict(idx)
    # give each date an index
    [d[t] for t in sorted(x.index.get_level_values('baropen_datetime').unique())]
    # use the index
    x['idx'] = [d[t] for t in x.index.get_level_values('baropen_datetime')]
    # plot using index
    x.groupby('code').apply(lambda y: plt.plot(y.idx.values,
                                               y.close.values,
                                               label=y.index.get_level_values('code')[0]))
    plt.legend()
    plt.show()
    plt.close()
于 2020-07-08T12:52:17.580 回答