I am currently facing an issue related to displaying x-axis data on a candlestick chart.
In short words:
The plotted vertical grid lines do not line up with the candlesticks.
Some of these vertical grid lines are offset per
1
candlestick after the first Japanese candlestick.
As can be seen in the graph below:
The dataframe (called df_trading_pair_date_time_index
) used to graph the above was the following:
Open High Low Close Volume End Date
Start Date
2022-09-11 11:45:00 7.696 7.704 7.695 7.704 267.3 2022-09-11 11:47:59.999
2022-09-11 11:48:00 7.703 7.703 7.695 7.697 537.5 2022-09-11 11:50:59.999
2022-09-11 11:51:00 7.696 7.701 7.695 7.695 292.7 2022-09-11 11:53:59.999
2022-09-11 11:54:00 7.695 7.699 7.695 7.699 64.8 2022-09-11 11:56:59.999
2022-09-11 11:57:00 7.701 7.702 7.701 7.702 101.5 2022-09-11 11:59:59.999
2022-09-11 12:00:00 7.702 7.705 7.680 7.685 4487.0 2022-09-11 12:02:59.999
2022-09-11 12:03:00 7.687 7.691 7.681 7.689 2482.5 2022-09-11 12:05:59.999
2022-09-11 12:06:00 7.691 7.691 7.677 7.682 1074.8 2022-09-11 12:08:59.999
2022-09-11 12:09:00 7.686 7.709 7.686 7.709 1389.5 2022-09-11 12:11:59.999
2022-09-11 12:12:00 7.708 7.708 7.690 7.701 1277.5 2022-09-11 12:14:59.999
2022-09-11 12:15:00 7.698 7.698 7.690 7.695 1655.7 2022-09-11 12:17:59.999
2022-09-11 12:18:00 7.694 7.698 7.692 7.696 576.2 2022-09-11 12:20:59.999
2022-09-11 12:21:00 7.696 7.700 7.694 7.696 1711.3 2022-09-11 12:23:59.999
2022-09-11 12:24:00 7.698 7.703 7.697 7.703 1893.1 2022-09-11 12:26:59.999
2022-09-11 12:27:00 7.701 7.720 7.701 7.717 1256.0 2022-09-11 12:29:59.999
2022-09-11 12:30:00 7.718 7.743 7.718 7.739 5602.6 2022-09-11 12:32:59.999
2022-09-11 12:33:00 7.739 7.744 7.730 7.744 905.6 2022-09-11 12:35:59.999
2022-09-11 12:36:00 7.743 7.780 7.740 7.780 6651.3 2022-09-11 12:38:59.999
2022-09-11 12:39:00 7.780 7.782 7.769 7.772 4478.3 2022-09-11 12:41:59.999
2022-09-11 12:42:00 7.773 7.886 7.773 7.875 30280.6 2022-09-11 12:44:59.999
The data types of the columns are as follows:
Start Date datetime64[ns]
Open float64
High float64
Low float64
Close float64
Volume float64
End Date datetime64[ns]
The code I wrote to graph the above was as follows:
import pandas as pd
import mplfinance as mpf
trading_pair= "DOTBUSD"
# Plotting
# Create my own `marketcolors` style:
mc = mpf.make_marketcolors(up='#2fc71e',down='#ed2f1a',inherit=True)
# Create my own `MatPlotFinance` style:
s = mpf.make_mpf_style(base_mpl_style=['bmh', 'dark_background'],marketcolors=mc, y_on_right=True)
# Plot it
trading_plot, axlist = mpf.plot(df_trading_pair_date_time_index,
figratio=(10, 6),
type="candle",
style=s,
tight_layout=True,
datetime_format = '%H:%M',
ylabel = "Precio ($)",
returnfig=True
)
# Add Title
symbol = trading_pair.replace("BUSD","")+"/"+"BUSD"
axlist[0].set_title(f"{symbol} - 3m", fontsize=25, style='italic', fontfamily='fantasy')
So I decided to open this question to learn how to properly adjust the grid lines for plotting candlestick data, after reading this other similar question I tried changing the values of figratio
a (12,6)
and (20,7)
but that didn't solve my problem at all.
I just wish my chart ended up looking something like this, not necessarily covering every candle, but making sure to align some of the x-axis data with their corresponding candles:
Good day,
The easiest way I could think of to adapt the solution to your code is by using the argument
show_nontrading = True
together withmatplotlib.axis.Axis.set_major_locator
andmatplotlib.axis.Axis.set_minor_locator
Full example using your data from the "sample2.csv" file:
This returns the following graph: