使用Mplfinance。我希望有人能澄清“y_on_right”参数的正确用法。我相信我正确使用了 mpf.make_addplot() 但它不会将 y 轴移动到图表的另一侧。使用提供的文档。TIA。
mpf.make_addplot(
df['sentiment'],
type='line',
ax=ax3,
y_on_right=True,
ylabel='Sentiment',
color='#6f2d84'
)]
编辑:添加代码的工作示例。
def makeCharts_CountsOHLC(self, props):
fig = props['fig']
df = props['df']
symbol = props['symbol']
start = props['min_date']
end = props['max_date']
# Configure the axes
ax1 = fig.add_subplot(5,1,(1,2))
ax2 = fig.add_subplot(5,1,3, sharex=ax1)
ax3 = fig.add_subplot(5,1,4, sharex=ax1)
ax4 = fig.add_subplot(5,1,5, sharex=ax1)
# Create add plots
aps = [
mpf.make_addplot(
df['count_s'],
ax=ax2,
ylabel='Tweets',
),
mpf.make_addplot(
df['sentiment'],
type='bar',
ax=ax3,
ylabel='Sentiment',
)]
ax1.tick_params(labelbottom=False)
ax2.tick_params(labelbottom=False)
ax3.tick_params(labelbottom=False)
# Functions to add a day to date and format dates
date_p1 = lambda x: dt.strftime(pd.to_datetime(x) + td(days=1), '%d %b %y')
fmt_date = lambda x: dt.strftime(pd.to_datetime(x), '%d %b %y')
title = f'{symbol} Price and Volume Traded from {fmt_date(start)} until {date_p1(end)}'
# Plot the chart
mpf.plot(
df,
type='candle',
ax = ax1,
volume = ax4,
addplot = aps,
xrotation = 0,
datetime_format = '%b %d',
axtitle = title,
ylabel = 'Price ($USD)',
tight_layout = True
)
# Format the prive of the y axis
mkfunc = lambda x, pos: '%1.1fM' % (x * 1e-6) if x >= 1e6 else '%1.1fK' % (x * 1e-3) if x >= 1e3 else '%1.1f' % x
mkformatter = matplotlib.ticker.FuncFormatter(mkfunc)
ax1.yaxis.set_major_formatter(mkformatter)
# Adjustments to plot and save
plt.subplots_adjust(hspace=0)
plt.savefig(fname=props['save_path'], dpi=self.cconf.get('RESOLUTION'))
plt.close('all')
fig.clear()