I need candlestick chart in one subplot and another subplot which share Y axe with candlestick chart.
I have following code drawing subplots:
df = get_current_data(limit=150)
df_asks, df_bids = get_order_book_data(bookStep="step0",limit=150)
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2,2, sharex='col', sharey='row', width_ratios=[5,1], height_ratios=[5,1])
ax1.plot(df["timestamp"],df["close"])
ax2.plot(df_asks["amount"],df_asks["price"],"r")
ax2.plot(df_bids["amount"],df_bids["price"],"g")
ax3.bar(df["timestamp"],df["volume"], width=0.0005)
plt.subplots_adjust(wspace=0.0,hspace=0.0)
ax1.set_title("BTC USD")
ax2.set_title("Orders Book")
ax1.set_ylabel("Price")
ax3.set_ylabel("Volume")
ax1.grid()
ax2.grid()
plt.show()
Result:
I need instead of BTC line to draw candlesticks.
I know i can use mplfinance library to draw candlestick chart, but seems to be mplfinance allows to stack additional subplots ONLY vertically sharing X (index of DataFrame which is datetime).
I need candlestick chart in one subplot and another subplot which share Y axe with candlestick chart.
I have following code drawing subplots:
df = get_current_data(limit=150)
df_asks, df_bids = get_order_book_data(bookStep="step0",limit=150)
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2,2, sharex='col', sharey='row', width_ratios=[5,1], height_ratios=[5,1])
ax1.plot(df["timestamp"],df["close"])
ax2.plot(df_asks["amount"],df_asks["price"],"r")
ax2.plot(df_bids["amount"],df_bids["price"],"g")
ax3.bar(df["timestamp"],df["volume"], width=0.0005)
plt.subplots_adjust(wspace=0.0,hspace=0.0)
ax1.set_title("BTC USD")
ax2.set_title("Orders Book")
ax1.set_ylabel("Price")
ax3.set_ylabel("Volume")
ax1.grid()
ax2.grid()
plt.show()
Result:
I need instead of BTC line to draw candlesticks.
I know i can use mplfinance library to draw candlestick chart, but seems to be mplfinance allows to stack additional subplots ONLY vertically sharing X (index of DataFrame which is datetime).
- Have you tried the External Axes Method of mplfinance? github/matplotlib/mplfinance/blob/master/markdown/… – RuthC Commented Mar 23 at 23:26
2 Answers
Reset to default 1i tried a couple of approaches, and found that old (depricated) mpl-finance working good. But old API is available in new version of library as original_flavor.
from mplfinance.original_flavor import candlestick_ohlc
df = get_current_data(limit=150)
df_asks, df_bids = get_order_book_data(bookStep="step0",limit=150)
xs = mdates.date2num(df["timestamp"])
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2,2, sharex='col', sharey='row', width_ratios=[5,1], height_ratios=[5,1])
hfmt = mdates.DateFormatter('%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(hfmt)
candlestick_ohlc(ax1, zip(xs, df['open'], df['high'], df['low'], df['close']), colorup='g', colordown='r', width=0.0004)
ax2.plot(df_asks["amount"],df_asks["price"],"r")
ax2.plot(df_bids["amount"],df_bids["price"],"g")
ax3.bar(xs,df["volume"], width=0.0005)
plt.subplots_adjust(wspace=0.0,hspace=0.0)
ax1.set_title("BTC USD")
ax2.set_title("Orders Book")
ax1.set_ylabel("Price")
ax3.set_ylabel("Volume")
ax1.grid()
ax2.grid()
plt.show()
Result:
Use mplfinance.original_flavor
for candlestick charts in custom subplot layouts:
from mplfinance.original_flavor import candlestick_ohlc
import matplotlib.dates as mdates
# Create subplots with specific ratios
fig, ((ax1, ax2),(ax3,ax4)) = plt.subplots(2, 2, sharex='col', sharey='row',
width_ratios=[5,1], height_ratios=[5,1])
# Convert dates to matplotlib format
xs = mdates.date2num(df["timestamp"])
# Format dates
hfmt = mdates.DateFormatter('%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(hfmt)
# Plot candlesticks
candlestick_ohlc(ax1, zip(xs, df['open'], df['high'], df['low'], df['close']),
colorup='g', colordown='r', width=0.0004)
# Add other plots
ax2.plot(df_asks["amount"], df_asks["price"], "r") # Order book asks
ax2.plot(df_bids["amount"], df_bids["price"], "g") # Order book bids
ax3.bar(xs, df["volume"], width=0.0005) # Volume
# Remove spacing between subplots
plt.subplots_adjust(wspace=0.0, hspace=0.0)
This approach successfully creates a candlestick chart that can share axes with other custom subplot layouts.