jd_h2003
jd_h2003

Reputation: 304

matplot finance not plotting window properly

I am using matplotlib finance (mpfinance) and am currently having weird intermittent plotting problems with it. (There are no tags for it within stack overflow so it's challenging to flag properly: https://github.com/matplotlib/mplfinance)

I had this code working earlier tonight and displaying the 2 correct y labels/axis, and changed relatively nothing of importance within the code other than some syntax cleanup.

In the included picture for AAPL, it seems the secondary y axis is trying to plot, however, it's displaying as a % of the total volume, rather than it's own y axis with percentages.

Comments should explain thought process.

import mplfinance as mpf

# AAPL call volume %
# Selecting specifics mentioning AAPL
AAPL_df = naster_df[master_df['ticker'] == 'AAPL'][[
             'ticker', 'date', 'call_volume', 'put_volume', 
             'call_ratio', 'put_ratio', 'open', 'low',
             'high', 'close','volume']]

# MPF requires DateTimeIndex for plotting
AAPL_df = AAPL_df.set_index('date')

# Dropping NA's, Not sure if needed so commented out
AAPL_df.dropna()

# Plotting call ratio with close price and volume
# MPF package requires volume to be explicitly named
# Dropping share volume as calculation is already made
# Renaming call volume to volume
AAPL_df = AAPL_df.drop(
                 'volume', axis = 1).rename(
                                     columns = {'call_volume':'volume'})

# Adding a call ratio (in %) as the bottom panel secondary y axis
ap = mpf.make_addplot((AAPL_df['call_ratio']), panel = 1, linestyle = 'dotted', ylabel = 'Options % ratio')

# Plotting AAPL share price with Open, High, Low, Close candles
# call_volume = volume
mpf.plot(AAPL_df, 
         type = 'candle', 
         volume = True, 
         addplot = ap,  
         ylabel = 'AAPL share price',
         ylabel_lower = 'Call Volume')

This produces this plot:

enter image description here

This isn't displaying the proper plot. Removing addplot = ap doesn't change this image.

However, the same code with a different ticker dataframe works below (they are in the same exact format)

# Plotting call ratio with close price and volume
ap = mpf.make_addplot((TSLA_df['call_ratio']), panel = 1, color = 'black', linestyle = 'dotted', ylabel = 'Call volume %')

mpf.plot(TSLA_df, 
         type = 'candle', 
         volume = True, 
         addplot = ap, 
         style = 'binance', 
         ylabel = 'TSLA share price',
         ylabel_lower = 'Call Volume')

Which produces:

enter image description here

They are both pulling data mentioning that specific ticker from the dataframe, and there are no NaN's, so I have no clue why it isn't working. I am trying to get the dashed line on y axis the bottom of the lower box. I guess I'm struggling to figure out why the same code is not working for a specific plot, and wondered if this type of issue was with my code of matplotlib finance.

If anyone has any ideas as to why this would be happening, they'd be greatly appreciated.

Sample df:

date    ticker  call_volume call_ratio  open    low high    close   volume
2021-03-08  AAPL    1229656 0.5782918149466192  120.93  116.209999  121.0   116.360001  154376600.0
2021-03-09  AAPL    774465  3.357156230430039   119.029999  118.790001  122.059998  121.089996  129525800.0
2021-03-10  AAPL    447447  3.9110777365810923  121.690002  119.449997  122.16999799999999  119.980003  111943300.0
2021-03-11  AAPL    577996  1.730115779347954   122.540001  121.260002  123.209999  121.959999  103026500.0
2021-03-12  AAPL    884787  0.5651077603988305  120.400002  119.160004  121.16999799999999  121.029999  88105100.0
2021-03-15  AAPL    778816  1.0272002629632673  121.410004  120.41999799999999  124.0   123.989998  92403800.0
2021-03-16  AAPL    1398777 1.8768538516146607  125.699997  124.720001  127.220001  125.57  115227900.0
2021-03-17  AAPL    978950  0.30645078911078194 124.050003  122.339996  125.860001  124.760002  111932600.0
2021-03-18  AAPL    1041143 2.7229688909208436  122.879997  120.32  123.18  120.529999  121229700.0
2021-03-19  AAPL    1123895 2.2817967870664075  119.900002  119.68  121.43  119.989998  185549500.0

Upvotes: 1

Views: 652

Answers (1)

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

You may want to print out your data arguments to make_addplot() and plot() calls, just before the call, to be absolutely certain that the following assumption holds ...

Assuming that your data has not changed as a result of:

I had this code working earlier tonight and displaying the 2 correct y labels/axis, and changed relatively nothing of importance ...

then it is very likely that the problem will be fixed by adding secondary_y=True in your call to mpf.make_addplot().


As per the mplfinance addplot documentation (between In [15] and In [16]) ...

  • mpf.make_addplot() has a keyword argument called secondary_y which can have three possible values: True, False, and 'auto'.
    • The default value is 'auto' which means if you don't specify secondary_y, or if you specify secondary_y='auto', then mpf.plot() will attempt to decide whether a secondary y-axis is needed, by comparing the order of magnitude of the addplot data with the order of magnitude of the data that is already on the plot.
    • If mpf.plot() gets it wrong, you can always override by setting secondary_y=True or secondary_y=False.

P.S. You may also want to add color=black to the first mpf.make_addplot() call (as you have it in the second call). Using your sample data to reproduce, here is what it looks like without, and with, color=black: (note: you did not have style='binance' on the first set of code above, although the image you posted does appear to have it).

enter image description here

enter image description here

Upvotes: 2

Related Questions