How to make color the wick portion of a candlestick stick using matplotlib?

后端 未结 2 1070
清歌不尽
清歌不尽 2021-01-28 07:21

I\'m looking to make the wick portion of a candlestick stick black using matplotlib? I couldn\'t find any mention of it in the documentation, but I\'ve seen pictoral examples sh

相关标签:
2条回答
  • 2021-01-28 08:16

    As Paul says, A MCVE would assist people in helping you greatly.

    However - just having a quick glance at the source code for the candlestick plotting in matplotlib shows that it uses the colorup/colordown parameter for both the candle and the wick.

    So in order to get them to be different colours you will most likely need to reimplement the method and/or monkey patch the base implementation.

    So in your plotting module, use something along the lines of:

    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
    

    Then elsewhere in this module you can use the mpl_finance.candlestick_ohlc(...) plotting functions.

    0 讨论(0)
  • Rewriting the complete candlestick_ohlc seems overly complicated. You can just iterate over the lines returned by the function and set their color to black. You may also set the zorder to have the wicks appear below the boxes.

    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)
    

    If this is to be used often in a script, you can of course put it in a function.

    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)
    
    0 讨论(0)
提交回复
热议问题