mplfinance常用方法

一、主题相关

  1. 查看可用预设主题
    mpf.available_styles()
    默认的主题包括:'binance','blueskies','brasil','charles','checkers','classic','default','ibd','kenan','mike','nightclouds','sas','starsandstripes','yahoo'
  2. 设置主题
    mpf.plot(daily,**kwargs,style='classic')
  3. 自定义主题
#设置表示涨和跌的颜色
`mc = mpf.make_marketcolors(up='g',down='r')`
#设置自定义主题
`s = mpf.make_mpf_style(marketcolors=mc)`
#使用自定义主题
`mpf.plot(daily,**kwargs,style='classic')`
  1. 其他细节
    除了设置涨跌颜色外,还可以设置蜡烛图的的边框、烛芯、成交量等颜色,如果想指定某个属性的颜色和涨跌颜色一致,可以将该值直接设置成i,in,inh。如果其他属性颜色属性都希望和涨跌颜色保持一致,直接设置属性inherit=True即可
mc = mpf.make_marketcolors(up='g',down='r',
edge='lime',
wick={'up':'blue','down':'orange'},
volume='in',
ohlc='i')
s = mpf.make_mpf_style(marketcolors=mc)
  1. 可以结合预设主题和自定义颜色使用
# 用法1:
# Create my own `marketcolors` to use with the `nightclouds` style:
mc = mpf.make_marketcolors(up='#00ff00',down='#ff00ff',inherit=True)
# Create a new style based on `nightclouds` but with my own `marketcolors`:
s = mpf.make_mpf_style(base_mpf_style='nightclouds',marketcolors=mc)
# 用法2:
mc = mpf.make_marketcolors(base_mpf_style='nightclouds',
edge='#505050',wick='#505050',volume='silver')
# Create a style based on `seaborn` using those market colors:
s = mpf.make_mpf_style(base_mpl_style='seaborn',marketcolors=mc)

二、 坐标轴、标题、大小等设置

  1. 设置大小
    mpf.plot(daily,figratio=(18,10),figscale=0.5)
  2. 坐标轴显示非交易日
    mpf.plot(daily,type='candle',figscale=0.9,show_nontrading=True)
  3. 坐标轴文本、标题
mpf.plot(daily,type='candle',volume=True,
title='\nS&P 500, Nov 2019',
ylabel='OHLC Candles',
ylabel_lower='Shares\nTraded',
xlabel='DATE')
  1. 设置横坐标日期格式和旋转角度
mpf.plot(daily,figscale=0.65,datetime_format=' %A, %d-%m-%Y',xrotation=90)
  1. 设置线条颜色
    mpf.plot(daily, type='line', linecolor='#00ff00')
  2. 调整版面布局
    mpf.plot(daily,volume=True,figscale=0.75,mav=(3,6),tight_layout=True)
  3. 颜色填充
# 只给纵坐标着色
mpf.plot(daily,figscale=0.7,fill_between=dict(y1=daily['Low'].values,y2=daily['High'].values))
# 同时给横坐标和纵坐标
dates_df = pd.DataFrame(daily.index)
buy_date = pd.Timestamp('2019-11-06')
sell_date = pd.Timestamp('2019-11-19')
where_values = pd.notnull(dates_df[ (dates_df>=buy_date) & (dates_df <= sell_date) ])['Date'].values
y1values = daily['Close'].values
y2value = daily['Low'].min()
mpf.plot(daily,figscale=0.7,
fill_between=dict(y1=y1values,y2=y2value,where=where_values,alpha=0.5,color='g')
)
  1. 解决中文乱码
import matplotlib.pyplot as plt
import mplfinance as mpf
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 解决mplfinance绘制输出中文乱码
s = mpf.make_mpf_style(base_mpf_style='yahoo', rc={'font.family': 'SimHei'})

三、多图绘制

  1. 基本用法
fig = mpf.figure(style='yahoo',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
mpf.plot(df,ax=ax1,volume=ax2)
  1. 给不同子图指定不同主题
fig = mpf.figure(figsize=(12,9))
ax1 = fig.add_subplot(2,2,1,style='blueskies')
ax2 = fig.add_subplot(2,2,2,style='yahoo')
s = mpf.make_mpf_style(base_mpl_style='fast',base_mpf_style='nightclouds')
ax3 = fig.add_subplot(2,2,3,style=s)
ax4 = fig.add_subplot(2,2,4,style='starsandstripes')
mpf.plot(df,ax=ax1,axtitle='blueskies',xrotation=15)
mpf.plot(df,type='candle',ax=ax2,axtitle='yahoo',xrotation=15)
mpf.plot(df,ax=ax3,type='candle',axtitle='nightclouds')
mpf.plot(df,type='candle',ax=ax4,axtitle='starsandstripes')
  1. 不同子图同步坐标轴
fig = mpf.figure(figsize=(9,6),style='yahoo')
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
av1 = fig.add_subplot(3,3,7,sharex=ax1)
av2 = fig.add_subplot(3,3,8,sharex=ax1)
av3 = fig.add_subplot(3,3,9,sharex=ax3)
mpf.plot(aapl,type='candle',ax=ax1,volume=av1,mav=(10,20),axtitle='AAPL')
mpf.plot(goog,type='candle',ax=ax2,volume=av2,mav=(10,20),axtitle='GOOG')
mpf.plot(spy ,type='candle',ax=ax3,volume=av3,mav=(10,20),axtitle='SPY')
  1. 组合图形
# 方式1:通过使用addplot参数
ap = mpf.make_addplot(spy,type='ohlc')
mpf.plot(aapl,type='candle',style='yahoo',addplot=ap)
# 方式二:使用双轴形式 ax.twinx()
s = mpf.make_mpf_style(base_mpl_style='seaborn',rc={'axes.grid':False})
fig = mpf.figure(style=s,figsize=(7.5,5.75))
ax1 = fig.subplot()
ax2 = ax1.twinx()
mpf.plot(spy ,ax=ax1,type='ohlc',style='default')
mpf.plot(aapl,ax=ax2,type='candle',style='yahoo')
  1. 结合使用addplot和双轴形式
# 用法1:
fig = mpf.figure(style='blueskies',figsize=(7,8))
ax1 = fig.add_subplot(2,1,1)
ax2 = ax1.twinx()
ax4 = fig.add_subplot(2,1,2)
ap = mpf.make_addplot(df[['UpperB','LowerB']],ax=ax2,ylabel='Bollinger Bands')
mpf.plot(df,ax=ax1,volume=ax4,addplot=ap,xrotation=10,type='candle')
# 用法2:
fig = mpf.figure(style='charles',figsize=(7,8))
ax1 = fig.add_subplot(3,1,1)
ax2 = fig.add_subplot(3,1,2,sharex=ax1)
ax3 = fig.add_subplot(3,1,3)
ap = [ mpf.make_addplot(df,type='ohlc',ax=ax2,ylabel='OHLC Price'),
mpf.make_addplot(df[['UpperB','LowerB']],ax=ax1)
]
mpf.plot(df,ax=ax1,volume=ax3,addplot=ap,xrotation=10,type='candle')

四、按不同频率作图

# 将数据聚合成周、月、季度
aggregation = {'Open' :'first',
'High' :'max',
'Low' :'min',
'Close' :'last',
'Volume':'sum'}
dfw = df.resample('1W').agg(aggregation)
dfm = df.resample('1M').agg(aggregation)
dfq = df.resample('1Q').agg(aggregation)
kwargs=dict(volume=True,type='candle',tight_layout=True)
# 可以绘制出周、月和季度的波动图
mpf.plot(df,**kwargs,title='\nINTC Daily ')
mpf.plot(dfw,**kwargs,title='\nINTC Weekly ')
mpf.plot(dfm,**kwargs,title='\nINTC Monthly ')
mpf.plot(dfq,**kwargs,title='\nINTC Quarterly ')

posted on   朝朝暮Mu  阅读(1225)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示