如何使用Python';是matplotlib烛台吗?

如何使用Python';是matplotlib烛台吗?,python,matplotlib,candlestick-chart,Python,Matplotlib,Candlestick Chart,我无法在没有周末的情况下绘制matplotlib.finance.candlestick(每5个烛台之间有空格)。这个标准也不排除周末,也不适用于烛台 以前有人见过这个吗 注:根据要求,以下是示例: #!/usr/bin/env python from pylab import * from matplotlib.dates import DateFormatter, WeekdayLocator, HourLocator, \ DayLocator, MONDAY from matplot

我无法在没有周末的情况下绘制matplotlib.finance.candlestick(每5个烛台之间有空格)。这个标准也不排除周末,也不适用于烛台

以前有人见过这个吗

注:根据要求,以下是示例:

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

在您的“报价”行之后:

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

这将在没有工作日间隔的情况下绘制数据,现在您必须将xtick更改回日期,最好是周一。假设您的第一个报价是星期一:

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

这是相当恶心,但似乎得到了工作完成-祝你好运

虽然@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()

你能给出一个最简单的例子吗?@Zhenya,这里有一个例子:可能是
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()