gpt4 book ai didi

python - Matplotlib/Mplfinance 'y_on_right' 的正确用法?

转载 作者:行者123 更新时间:2023-12-02 16:08:09 26 4
gpt4 key购买 nike

使用 Mplfinance .我希望有人能阐明“y_on_right”参数的正确用法。我相信我正确使用了 mpf.make_addplot() 但它不会将 y 轴移动到图表的另一侧。使用 docs假如。 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()

enter image description here

最佳答案

外轴模式不支持y_on_right

如果你想改变特定轴的方向,你可以直接这样做:

ax3.yaxis.set_label_position("right")
ax3.yaxis.tick_right()

关于python - Matplotlib/Mplfinance 'y_on_right' 的正确用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68782116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com