一直在使用Mplfinance及其強大的功能。我一直在努力改變一些子地塊的顏色。
我使用外部軸方法成功地使用了文檔中的'mpf.make_addplot方法。我已經閱讀了關于樣式的文檔。但是,如何將樣式應用于其中一個子場景,而不僅僅是整個情節?
我已經添加了我的代碼和它生成的圖表的一個圖像,該函數通過道具傳遞圖表數據。
我已將圖像標記為“A”,其中有一條線我想更改其顏色,這是df['counts']添加圖,標記為“Tweets”。另外,對于標有“B”的部分,我想改變條的顏色,如果可能的話,根據它是正片還是負片,使它變成不同的顏色。
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()
mpf.make_addplot()
有一個kwargcolor=
,你應該幫助它。作為一般規則,代碼中列出了所有可用的KWARG:
mpf.plot()
,和mpf.make_addplot()