set grid intervals for x and y axis in mplfinance

115 Views Asked by At

enter image description here

In the picture, I want the time grid on x-axis to be in half hour increments and the price on the y-axis to be in increments of 10 for the major grid and 1 for the minor grid.

    import mplfinance as mpf
    import pandas as pd

    style = {'base_mpl_style': 'fast',
         'marketcolors'  : {'candle': {'up': '#00b060', 'down': '#fe3032'}, 
                            'edge'  : {'up': '#00b060', 'down': '#fe3032'}, 
                            'wick'  : {'up': '#606060', 'down': '#606060'},
                            'ohlc'  : {'up': '#00b060', 'down': '#fe3032'},
                            'volume': {'up': '#4dc790', 'down': '#fd6b6c'},
                            'vcdopcod'      : True, 
                            'alpha'         : 0.9}, 

         'mavcolors'     : None,
         'facecolor'     : '#ffffff',  # light gray
         'gridcolor'     : '#d0d0d0',  # medium gray
         'gridstyle'     : '-', 
         'gridaxis'      : 'both', 
         'y_on_right'    : True,
         'rc'            : {'axes.labelcolor': '#101010',
                            'axes.edgecolor' : 'f0f0f0',
                            'axes.grid.axis' : 'y',
                            'ytick.color'    : '#101010',
                            'xtick.color'    : '#101010',
                            'figure.titlesize': 'x-large',
                            'figure.titleweight':'semibold',
                           },
         'base_mpf_style': 'yahoo'}

    file_location = 'C:\\ES5min.csv'
    file_location2 = 'C:\\ES5min_multi.csv'

    df = pd.read_csv(file_location2, index_col=0, parse_dates=True)
    print(df.shape)
    print(df.head())

    image_path = 'C:\\Users\\Administrator\\PycharmProjects\\pythonTest\\matplot_learning\\mplfinance1learn\\mpl_image\\'

    list1 = ['2023-03-16 09:27:00', '2023-03-17 09:27:00', '2023-03-20 09:27:00', '2023-03-21 09:27:00', '2023-03-22 09:27:00', '2023-03-23 09:27:00', '2023-03-24 09:27:00',]
    list2 = ['2023-03-16 16:03:00', '2023-03-17 16:03:00', '2023-03-20 16:03:00', '2023-03-21 16:03:00', '2023-03-22 16:03:00', '2023-03-23 16:03:00', '2023-03-24 16:03:00',]
    list3 = ['2023-03-16 03:00:00', '2023-03-17 03:00:00', '2023-03-20 03:00:00', '2023-03-21 03:00:00', '2023-03-22 03:00:00', '2023-03-23 03:00:00', '2023-03-24 03:00:00',]
    list4 = ['2023-03-16 08:30:00', '2023-03-17 08:30:00', '2023-03-20 08:30:00', '2023-03-21 08:30:00', '2023-03-22 08:30:00', '2023-03-23 08:30:00', '2023-03-24 08:30:00',]
    list5 = ['2023-03-16', '2023-03-17', '2023-03-20', '2023-03-21', '2023-03-22', '2023-03-23', '2023-03-24',]

    for i in range(7):
        df1 = df.iloc[(276 * i):(276 * (i+1)), :]
    
        mpf.plot(df1,
             type='candle',
             #ylabel="price",
             #ylabel_lower="Volume",
             style=style,
             title='ES 5min ' + list5[i],
             #show_nontrading=True,
             mav=(20),
             volume=True,
             vlines=dict(vlines=[list1[i], list2[i], list3[i], list4[i]], colors='gray', linewidths=(0.2), linestyle='-.'),
             #vlines=[list1[i]],
             figscale=3,
             figratio=(5, 3),
             main_panel=0,
             volume_panel=1,
             panel_ratios=(5, 1),
             datetime_format='%H:%M',
             xrotation=90,
             savefig=dict(fname=image_path + str(i) + '.jpg', dpi=300, pad_inches=0.25),
             tight_layout=True,
             )

I tried to find how to set grid intervals in mplfinance but couldn't. I did find how to set it in matplotlib, but I don't know how to integrate it into mplfinance.

https://www.delftstack.com/howto/matplotlib/set-matplotlib-grid-interval/

0

There are 0 best solutions below