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

标签 python matplotlib candlestick-chart

如果没有周末(每 5 个烛台之间的空格),我无法绘制 matplotlib.finance.candlestick。 example from Matplotlib's website也不排除周末和the way to exclude weekends on other plots似乎不适用于 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()

最佳答案

在你的“引号”行之后:

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

这很糟糕,但似乎可以完成工作 - 祝你好运!

关于python - 如何使用 Python 的 matplotlib 烛台仅绘制工作日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10529492/

相关文章:

python - 将方程式转换为 Python

python - 动态更新 QChart

python - 将刻度数据转换为 OHLCV 烛台数据

Python 2.7.8 在终端(ubuntu 14.04)中打印语句 “Syntax Error: invalid syntax” 但在 vim 上运行良好,为什么?

python - subprocess.Popen( ["open", "target.mkv"],shell=True 打开指定文件失败

python - matplotlib/python 中错误栏的边框

python - 来自 Pandas DataFrame 的烛台图中的重叠日期

python - 了解 __init_subclass__

python - int'对象没有属性 '__getitem__'

java - 实时更新的 3D 图表