0

I am creating a figure with 2 subplots using the following code:

fig, (ax1, ax2) = plt.subplots(2, sharex = True, figsize=(20, 6)) 
mpf.candlestick_ohlc(ax1,quotes, width=0.01)
ax1.xaxis_date()
ax1.xaxis.set_major_locator(mpl.dates.DayLocator(interval=1) )
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter('%a, %b %d \'%y'))
ax1.xaxis.set_minor_locator(mpl.dates.HourLocator(byhour=range(0,24,4)))
ax1.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%-H'))
ax1.grid(True)
ax1.grid(b=True, which='minor', color='0.7', linestyle='dotted')
ax2.tick_params(direction='out', pad=15)
majors=ax1.xaxis.get_majorticklocs()
chart_start, chart_end = (ax1.xaxis.get_view_interval()[0],ax1.xaxis.get_view_interval()[1])
for major in majors:
 ax1.axvspan(max (chart_start, major-(0.3333)),min(chart_end, major+(0.3333)),color="0.95", zorder=-1 )
plt.bar(quotes[:,0] , quotes[:, 5], width = 0.01)
plt.show()

Here the resulting figure:

enter image description here

I would like the subplot at the bottom to have the same grid, major/ minor ticks and axvspan as the subplot above. I could rewrite all the ax1... lines changing ax1 for ax2 , but i suspect there might be a way to assign the different elements (ie grid, minor/major ticks and axvspan) to both axes in 1 go?

asked Dec 31, 2016 at 2:54

1 Answer 1

1

ok found it:

fig, (axes) = plt.subplots(2, sharex = True, figsize=(20, 6))
mpf.candlestick_ohlc(axes[0],quotes, width=0.01)
plt.bar(quotes[:,0] , quotes[:, 5], width = 0.01)
for i , axes[i] in enumerate(axes):
 axes[i].xaxis_date(tz="Europe/Berlin")
 axes[i].xaxis.set_major_locator(mpl.dates.DayLocator(interval=1) )
 axes[i].xaxis.set_major_formatter(mpl.dates.DateFormatter('%a, %b %d \'%y'))
 axes[i].xaxis.set_minor_locator(mpl.dates.HourLocator(byhour=range(0,24,4)))
 axes[i].xaxis.set_minor_formatter(mpl.dates.DateFormatter('%-H'))
 axes[i].tick_params(direction='out', pad=15)
 axes[i].grid(True)
 axes[i].grid(b=True, which='minor', color='0.7', linestyle='dotted')
 majors=axes[i].xaxis.get_majorticklocs()
 chart_start, chart_end = (axes[i].xaxis.get_view_interval()[0], axes[i].xaxis.get_view_interval()[1])
 for major in majors:
 axes[i].axvspan(max (chart_start, major-(0.3333)),min(chart_end, major+(0.3333)),color="0.95", zorder=-1 )
plt.show()
answered Dec 31, 2016 at 3:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.