简体   繁体   中英

Matplotlib half hour major locator on candlestick chart

I'm attempting to draw a candlestick chart with historical data for every half hour, therefore using a major locator for every 30 minutes and have a minor locator of 10 min. How can I achieve a half-hour major locator?

I currently have a HourLocator but I would like to add some extensibility by being able to create locators for custom times, example: for every 45 minutes or a major locator for every 10 minutes.

This is why I currently have:

halfHourFormatter = DateFormatter('%d %H:%M:%S')

date1 = date2num(datetime.datetime.strptime("17/03/2016 22:30:00", "%d/%m/%Y %H:%M:%S"))
date2 = date2num(datetime.datetime.strptime("17/03/2016 23:00:00", "%d/%m/%Y %H:%M:%S"))

quotes = [
    (date1,86.439,86.574,86.423,86.545),
    (date2,86.545,86.551,86.019,86.024)
]

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(dates.HourLocator()) # Ideally: 30 Minute locator
ax.xaxis.set_major_formatter(halfHourFormatter)
ax.xaxis.set_minor_locator(<10 MINUTE LOCATOR>)

candlestick_ohlc(ax, quotes, width=0.006,colorup='g', colordown='r')

ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

plt.show()

Additional question: Is there a way to set the width of the candlestick flag based on the time of the data? In this case, a width of 0.006 seems to work for a 30-minute tick and 0.6 for a day-tick.

Thanks!

Have you tried:

dates.MinuteLocator(interval=30)

From the docs to dates.MinuteLocator :

interval is the interval between each iteration. For example, if interval=2 , mark every second occurrence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM