【问题标题】:Python datframe plottingPython数据框绘图
【发布时间】:2021-07-18 14:27:22
【问题描述】:

我想在 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()

【问题讨论】:

  • 标题中有错字,您可能希望使其比“Python 数据框绘图”更具体。

标签: pandas dataframe candlestick-chart mplfinance


【解决方案1】:

我对 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(垂直线)用垂直线突出显示特定蜡烛很简单) 夸格:

##  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)

结果:

【讨论】:

  • 你好丹尼尔,谢谢你的帮助,你解决了一个大问题。我以前使用过stackoverflow,但这是我的第一次体验,它是一个非常棒的平台。再次感谢。
猜你喜欢
  • 2021-06-09
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2022-11-17
  • 2015-09-29
  • 2018-09-20
相关资源
最近更新 更多