3

I'm using matplotlib to draw candlestick charts for stocks. In regular candlestick charts, weekends (in which stock market is closed) are not displayed. But in matplotlib, weekends are also plotted and gaps appear between weekdays. I wrote demo.py shown in matplotlib website for candlestick charts. How can I avoid plotting weekend dates and gaps between weekdays?

Demo.py
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()

enter image description here

Elgin Cahangirov
  • 1,932
  • 4
  • 24
  • 45
  • Possible duplicate of [How do I plot only weekdays using Python's matplotlib candlestick?](http://stackoverflow.com/questions/10529492/how-do-i-plot-only-weekdays-using-pythons-matplotlib-candlestick) – ImportanceOfBeingErnest Jan 30 '17 at 17:58
  • I looked at that question, but solution shown there don't work. – Elgin Cahangirov Jan 30 '17 at 18:01
  • In that case it is very important to cite that question, and tell why exactly it doesn't work. For further details read [ask]. – ImportanceOfBeingErnest Jan 30 '17 at 18:02
  • I didn't know that I violated the rules. I will consider those rules next time. If you know solution for this problem, please let me know. – Elgin Cahangirov Jan 30 '17 at 18:10
  • At the moment the question can simply be answered by posting the link to the previous question. I would therefore suggest you already consider those hints right now. You can edit your question by clicking the 'edit' button. Of course you don't have to do it, it's your choice. However, the higher the quality of the question, the higher the chances to obtain an satisfying answer. Personally I do not know the answer (yet), but other people might. – ImportanceOfBeingErnest Jan 30 '17 at 18:38
  • If you used `pandas` to handle your stock data, you could make use of the `.dt` namespace as shown in this answer and filter your data that way. – lanery Jan 30 '17 at 21:11
  • As I said above, I tried that solution, but it doesn't have any effect on graph. It also plots candlestick chart with gaps. – Elgin Cahangirov Jan 31 '17 at 12:25
  • Sorry @ElginCahangirov, I meant to link to this answer: http://stackoverflow.com/a/35230225/5285918. I think: http://stackoverflow.com/questions/9673988/intraday-candlestick-charts-using-matplotlib may be of even more help. – lanery Feb 02 '17 at 06:22
  • Possible duplicate of [Intraday candlestick charts using MatPlotLib](http://stackoverflow.com/questions/9673988/intraday-candlestick-charts-using-matplotlib) – lanery Feb 02 '17 at 06:52

0 Answers0