自定义 mplfinance 图 python

Customizing mplfinance plot python

正在尝试创建价格图表(类似于 TradingView 图表)。大多数代码我已经弄清楚并且看起来有效但结果不是我想看到的。

当您看到图表周围有很多白色 space 并且价格和成交量标签不在右侧时,是否有办法移除白色 space 并移动左侧的标签?另外有没有把背景改成白色的?

import pandas as pd
import mplfinance as mpf

chartData = {
         'o': [5.589310029183743, 5.610726267831264, 5.6097540855407715, 5.594677448272705, 5.564507322870441, 5.490811827389135], 
         'c': [5.610726267831264, 5.6097540855407715, 5.594677448272705, 5.564507322870441, 5.490811827389135, 5.49099588394165], 
         'h': [5.610831260681152, 5.610726267831264, 5.6097540855407715, 5.594677448272705, 5.564507322870441, 5.49099588394165], 
         'l': [5.57621750831604, 5.608160018920898, 5.582586765289307, 5.5580267906188965, 5.485752105712891, 5.490811827389135], 
         'v': [90.34457968175411, 5.964259386062622, 27.667950868606567, 101.91513729095459, 273.24095344543457, 4.425440788269043], 
         't': [1625720400, 1625721000, 1625721600, 1625722200, 1625722800, 1625723400]}

df: DataFrame = pd.DataFrame(chartData)
df.columns = ['Open', 'Close', 'High', 'Low', 'Volume', 'Date']
data = df.loc[:, ['Open', 'Close', 'High', 'Low', 'Volume', 'Date']]
data['Date'] = pd.to_datetime(data['Date'], unit='s', origin='unix')
data.index = pd.DatetimeIndex(data['Date'])

fg1, _ = mpf.plot(data, type='candle',
              title="PolyVertex Price",
              ylabel='Price',
              ylabel_lower='Volume',
              figsize=(20,10),
              returnfig=True,
              style='yahoo',
              volume=True,)

fg1.savefig('text1.jpg')

As you see a lot of white space around a chart and label Price and Volume is not right side, so is there is away to remove white space and move labels on left side? Beside is there away to change background to white?

  • 去除多余的白色space:使用scale_padding kwarg

  • left/right 上的价格和成交量标签以及背景颜色都是 mplfinance style 的一部分。因此,要更改这些,您需要创建自己的自定义样式:

    customstyle = mpf.make_mpf_style(base_mpf_style='yahoo',
                                     y_on_right=False,
                                     facecolor='w')
    
    fg1, _ = mpf.plot(data, type='candle',
                title="PolyVertex Price",
                ylabel='Price',
                ylabel_lower='Volume',
                figsize=(20,10),
                returnfig=True,
                style=customstyle,    # use `customstyle`
                volume=True,)