Python 数据框绘图

Python datframe plotting

我想在 pandas 中绘制包含烛台的数据框,增加和减少的蜡烛都必须具有相同的颜色,除了我的一些选择。

我真的很难找到解决办法。

我尝试了 mplfinance、plotly、finplot,我可以更改所有蜡烛的颜色,但不仅可以更改某些蜡烛的颜色。

如果无法单独修改颜色,我至少希望沿着图表绘制与烛台相对应的垂直线。

我可以识别受数据帧索引及其时间影响的蜡烛。

提前致谢,帮助很有用。

一些代码行:

import finplot as fplt

...
    # I receive the data from yahoo finance and save it in the pdOHLCVs dataframe

colorBlue = "#0509fc"

fplt.candle_bear_color = colorBlue
fplt.candle_bull_color = colorBlue
fplt.candle_bull_body_color = colorBlue
fplt.candlestick_ochl (pdOHLCVs [['open', 'close', 'high', 'low']])

    # here I want to be able to change the color for example at candle 12
    # here I want to be able to change the color for example at candle 29
    # here I want to be able to change the color for example at candle 56

fplt.show()

我对 plotly 和 finplot 不是很熟悉,但是(完全披露)我是 mplfinance 的维护者。尚不支持在 mplfinance 中为特定蜡烛着色,但我们已收到此请求并计划在未来支持它。

与此同时,这可以使用 mplfinance 完成一些额外的工作。基本思路是这样的:

  1. 将您的数据框一分为二,以获得您想要的两种不同颜色,每个数据框仅包含您想要的颜色的蜡烛,以及所有其他蜡烛的 NaN 值。
  2. 创建两种自定义“mplfinance 样式”,每种蜡烛只有一种颜色,但每种样式的颜色都不同。
  3. 在同一个轴上绘制两个数据帧,每个数据帧都有其指定的颜色样式。

这是一些示例代码。此示例的数据可以在 here.

中找到
import pandas as pd
import mplfinance as mpf

# Read in S&P500 November 2019 OHLCV:
df1 = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
df1.drop('Volume',axis=1,inplace=True) # Drop Volume (not needed for this demo)

# Create a DataFrame with the same shape, but all NaN values:
nc = [float('nan')]*len(df1)  # nc = Nan Column
df2 = pd.DataFrame(dict(Open=nc,High=nc,Low=nc,Close=nc))
df2.index = df1.index

# Copy specific values from one DataFrame to the Other
df2.loc['11/8/2019']  = df1.loc['11/8/2019'].copy()
df2.loc['11/15/2019'] = df1.loc['11/15/2019'].copy()
df2.loc['11/21/2019'] = df1.loc['11/21/2019'].copy()
df2.loc['11/27/2019'] = df1.loc['11/27/2019'].copy()

# Set the same values to NaN back in the original DataFrame:
df1.loc[ '11/8/2019'] = [float('nan')]*4
df1.loc['11/15/2019'] = [float('nan')]*4
df1.loc['11/21/2019'] = [float('nan')]*4
df1.loc['11/27/2019'] = [float('nan')]*4

# Now make 2 custom styles where the candles are all the same color
# (But a different color for each style)

# style1 has all candles 'blue'
m = mpf.make_marketcolors(base_mpf_style='default',up='b',down='b')
style1 = mpf.make_mpf_style(base_mpf_style='default',marketcolors=m)

# style2 has all candles 'lime'
m = mpf.make_marketcolors(base_mpf_style='default',up='lime',down='lime')
style2 = mpf.make_mpf_style(base_mpf_style='default',marketcolors=m)

# Now plot each DataFrame on the same Axes but with different styles:
# Use returnfig=True to get the Axes and pass to the next call:
fig, ax = mpf.plot(df1,type='candle',style=style1,returnfig=True)
mpf.plot(df2,type='candle',style=style2,ax=ax[0])
mpf.show()

以上代码的结果:


顺便说一句,由于您提到了 垂直线 ,使用 mplfinance 的 突出显示带有垂直线的特定蜡烛很简单vlines(竖线)kwarg:

##  Easier just to add vertical lines:
# Read in S&P500 November 2019 OHLCV:
df1 = pd.read_csv('data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)

v = dict(vlines=['11/7/2019','11/15/2019','11/21/2019','11/27/2019'],
         alpha=0.3,colors=['lime'],linewidths=[7])

mpf.plot(df1,type='candle',vlines=v)

结果: