Is there a way to set the vlines below/under the candlesticks on plot? (mplfinance)

Viewed 37

I am currently using vlines to shadow in afterhours/premarket trading. However, the candlesticks are under the vlines and as such they become harder to read if I increase the alpha kwarg. Is there a way to put the vlines under the candlesticks as opposed to above them? Using the mplfinance lib.

figure

PS: the vlines are also ever so slighly off the top of the plot, is there a way for them to go all the way up to the top edge?

fig, axlist = mpf.plot(df,
                           type='candle',
                           volume=True,
                           ylabel='',
                           ylabel_lower='\n<thousands>',
                           returnfig=True,
                           style=style,
                           figratio=(21, 9),
                           warn_too_much_data=970,
                           addplot=vwap,
                           hlines=dict(hlines=previousCloseLine(), colors='white', linestyle='dashed'),
                           vlines=dict(vlines=df.between_time('16:00', '09:30').index.tolist(), colors='#323538', linewidths=1, alpha=0.5),
                           datetime_format='%H:%M',
                           tight_layout=True
                           )
1 Answers

Regarding your first question (vlines below candlesticks), sounds like you are asking about zorder. If so, mplfinance presently does not give you direct access to zorder. There is an enhancement request for it that unfortunely got pushed onto the back-burner in favor of some other enhancements. There may be a work-around using returnfig=True but I'm not sure yet if that can work (would have to run some experiments to see if zorder can be modified this way).

In the meantime, given what you are trying to do (shade an area of the plot) you might try mplfinance's fill_between feature. Don't know if that will have the same zorder issue or not, but it is worth a try.


Regarding your second question (about vlines not quite reaching the top of the plot), not sure what's causing that, or whether it is inherent to mplfinance and/or to matplotlib, possibly only in response to certain data.

One possible workaround, in the meantime, may be to set the ylim=(ymin,ymax) kwarg to set ymin and ymax manually (instead of letting mplfinance determine them automatically). It may be worth adding an issue to the mplfinance page, including all the data and code necessary to reproduce the issue. It doesn't seem to me that vlines should stop short of the top like that.

Related