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

9
  • Possible duplicate of How do I plot only weekdays using Python's matplotlib candlestick? Commented Jan 30, 2017 at 17:58
  • I looked at that question, but solution shown there don't work. Commented Jan 30, 2017 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 How to Ask. Commented Jan 30, 2017 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. Commented Jan 30, 2017 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. Commented Jan 30, 2017 at 18:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.