如何使用matplotlib为烛台棒的烛芯部分着色?

时间:2017-04-05 19:37:30

标签: python matplotlib charts

我希望使用matplotlib将烛台的灯芯部分变成黑色?我在文档中找不到任何提及,但我已经看到了可以完成的图片示例。

这是我目前所拥有的:

enter image description here

这里有一个黑色灯芯的例子:

enter image description here

更新

我使用了下面提供的解决方案,但稍微更改了代码以删除烛台主体区域的垂直线:

from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle


def blackwickcandlestick(ax, quotes, width=0.2, colorup='#00FF00', colordown='#FF0000',
                         alpha=1.0, shadowCol='k', ochl=True):

    OFFSET = width / 2.0

    lines = []
    patches = []
    for q in quotes:
        if ochl:
            t, open, close, high, low = q[:5]
        else:
            t, open, high, low, close = q[:5]

        if close >= open:
            color = colorup
            lower = open
            height = close - open
            vline = Line2D(
                xdata=(t, t), ydata=(low, high),
                color=colorup,      # This changed from the default implementation
                linewidth=0.5,
                antialiased=True,
                )
        else:
            color = colordown
            lower = close
            height = open - close
            vline = Line2D(
                xdata=(t, t), ydata=(low, high),
                color=colordown,      # This changed from the default implementation
                linewidth=0.5,
                antialiased=True,
                )


        rect = Rectangle(
            xy=(t - OFFSET, lower),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=color,
        )
        rect.set_alpha(alpha)

        lines.append(vline)
        patches.append(rect)
        ax.add_line(vline)
        ax.add_patch(rect)
    ax.autoscale_view()

    return lines, patches


import matplotlib.finance as mpl_finance

mpl_finance._candlestick = blackwickcandlestick

2 个答案:

答案 0 :(得分:4)

重写完整的candlestick_ohlc似乎过于复杂。您可以迭代函数返回的行并将其颜色设置为黑色。您也可以设置zorder以使灯芯出现在方框下方。

lines, patches = candlestick_ohlc(ax, quotes, width=0.5)
for line, patch in zip(lines, patches):
    patch.set_edgecolor("k")
    patch.set_linewidth(0.72)
    patch.set_antialiased(False)
    line.set_color("k")
    line.set_zorder(0)

enter image description here

如果要在脚本中经常使用它,您当然可以将它放在一个函数中。

def candlestick_ohlc_black(*args,**kwargs):
    lines, patches = candlestick_ohlc(*args,**kwargs)
    for line, patch in zip(lines, patches):
        patch.set_edgecolor("k")
        patch.set_linewidth(0.72)
        patch.set_antialiased(False)
        line.set_color("k")
        line.set_zorder(0)

candlestick_ohlc_black(ax, quotes, width=0.5)

答案 1 :(得分:1)

正如保罗所说,MCVE会帮助人们大力帮助你。

然而 - 只需快速浏览matplotlib中烛台绘图的源代码,就会发现它对蜡烛和灯芯使用了colorup / colordown参数。

因此,为了使它们具有不同的颜色,您很可能需要重新实现该方法和/或猴子修补基础实现。

因此,在您的绘图模块中,请使用以下内容:

from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle


def blackwickcandlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
                         alpha=1.0, ochl=True):

    OFFSET = width / 2.0

    lines = []
    patches = []
    for q in quotes:
        if ochl:
            t, open, close, high, low = q[:5]
        else:
            t, open, high, low, close = q[:5]

        if close >= open:
            color = colorup
            lower = open
            height = close - open
        else:
            color = colordown
            lower = close
            height = open - close

        vline = Line2D(
            xdata=(t, t), ydata=(low, high),
            color='k',      # This is the only line changed from the default implmentation
            linewidth=0.5,
            antialiased=True,
        )

        rect = Rectangle(
            xy=(t - OFFSET, lower),
            width=width,
            height=height,
            facecolor=color,
            edgecolor=color,
        )
        rect.set_alpha(alpha)

        lines.append(vline)
        patches.append(rect)
        ax.add_line(vline)
        ax.add_patch(rect)
    ax.autoscale_view()

    return lines, patches


import matplotlib.finance as mpl_finance

mpl_finance._candlestick = blackwickcandlestick

然后在本单元的其他地方,您可以使用mpl_finance.candlestick_ohlc(...)绘图功能。