Mplfinance: Using mpf.plot along with regular ax.plot

Created on 25 Aug 2020  路  2Comments  路  Source: matplotlib/mplfinance

I am trying to use mpf.plot along with ax.plot (on the same ax or separate subplot). With show_nontrading=True, I get an error message. I found a workaround that seems to work.

I wonder whether this workaround is legit and I should keep using it.
Perhaps I am bending the use of mplfinance a bit: what I want to achieve is to integrate bar/candlestick charts into existing matplotlib projects that have already their own style or customization.

The whole exercise is on:
https://github.com/stebas101/Visual-Sketchbook/blob/master/mplfinance%20-%20External%20Axes%20exercise.ipynb

Here's a code snippet:

import numpy as np
# My workaround: passing xpoints to ax.plot:
xpoints = np.arange(len(data_sel))

fig = plt.figure(figsize=(12,6))

ax = plt.subplot(1,1,1)
# show_nontrading defaults to False:
mpf.plot(data_sel, ax=ax)
ax.plot(xpoints, data_sel['H_sma'], color='blue', linewidth = 2)
ax.plot(xpoints, data_sel['L_sma'], color='blue', linewidth =2)
ax.plot(xpoints, data_sel['C_ema'], color='red', linestyle = '--', linewidth = 1.5)

To obtain:
image

question

All 2 comments

Stefano,

The workaround is legit. The way matplotlib presently works is that, if it determines or is told that the x-axis is time-based, then it _assumes that time is linear and continuous_, and thus will allow space on the time axis for all times (or dates), even if some times or dates are missing from the data.

The only way, that I was able to find, to allow a matplotlib time-axis to be non-linear or non-continous, that is to display, and to make space for, only those dates or times that are included in the data, is to do as you have done (and as mplfinance does internally). That is, to have the x-axis be actually the row number of the data (so matplotlib thinks of it not as time-based). The date formatting in mplfinance is done by providing matplotlib with a formatter class that maps the row number to the datetime. It would be nice if matplotlib itself had this feature built in, but it does not.

I agree the workaround is a little messy. The addplot feature handles it for you, nicely abstracting it away so your code can be simpler. The reason linewidth does not work with addplot is because the kwarg (to make_addplot()) is width (_not_ linewidth). The reason for the more generic width is because it affects both line widths (if type='line') and bar widths (if type='bar').

Your code looks good and clean to me. And it confirms that mplfinance has accomplished what I had hoped: That for 90% of use cases it can create nice plots keeping the user's code very simple (with no need to ever import or use matplotlib directly). But for the other 10% of use-cases, and/or for people who are comfortable using matplotlib directly, users can use returnfig=True or "external axes mode" to gain access to all of matplotlib's power and customization.

Let me know if you have any further questions.

Daniel,

Thanks for your accurate reply. I agree that addplot should be the way to go most of the time. I am actually focusing on the 10% or less of the situations where this is not the case. Mplfinance is the only resource available (to my knowledge) to plot bar/candlestick charts within matplotlib, we appreciate very much your work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viorell91 picture viorell91  路  3Comments

mattcambs picture mattcambs  路  3Comments

franklee00 picture franklee00  路  4Comments

obahat picture obahat  路  3Comments

ismailbayram picture ismailbayram  路  4Comments