【问题标题】:how to add image to plot mplfinance python如何添加图像以绘制 mplfinance python
【发布时间】:2021-10-26 23:57:21
【问题描述】:

尝试添加图像和价格标签并按时添加更多空间,看起来 ylim= 可以解决这个问题,但是当我添加它时,我的整个图表都消失了。

    market_colors = mpf.make_marketcolors(
        base_mpf_style="charles"
    )
    rc = {
        "axes.labelcolor": "none",
        "axes.spines.bottom": True,
        "axes.spines.left": False,
        "axes.spines.right": False,
        "axes.spines.top": False,
        "font.size": 10,
    }
    styles = mpf.make_mpf_style(
        base_mpf_style="nightclouds",
        marketcolors=market_colors,
        gridstyle="",
        rc=rc
    )

    filledShape = {
        "y1": df['Close'].values,
        "facecolor": "#2279e4"
    }

    (mpf.plot(df, type='line',
              title='Test',
              linecolor='white',
              style=styles,
              volume=True,
              figsize=(8, 6),
              figscale=0.5,
              fill_between=filledShape, tight_layout=True,
              scale_padding={'left': 1, 'top': 5, 'right': 1, 'bottom': 2}
              ))

【问题讨论】:

    标签: python mplfinance


    【解决方案1】:

    我知道在 matplotlib 图上显示图像的三种技术:

    1. Axes.imshow()
    2. Figure.figimage()
    3. Putting the image in an AnnotationBbox

    就使用 mplfinance 而言,我会说技术一,调用 Axes.imshow() 可能是最简单的:

    第 1 步:

    对于上述所有三种技术,当你调用mpf.plot() set kwarg returnfig=True

    fig axlist = mpf.plot(df,...,returnfig=True)
    

    这会给你access to the mplfinance Figure and Axes objects

    第 2 步:

    现在在您想要图像/徽标的位置创建一个新的 Axes 对象:

    # Note: [left,bottom,width,height] are in terms of fraction of the Figure.
    # For example [0.05,0.08,0.10,0.06] means:
    #  the lower/left corner of the Axes will be located: 
    #    5% of the way in from the left
    #    8% down from the top,
    #  and the Axes will be
    #    10% of the Figure wide and
    #    6% of the Figure high.
    
    logo_axes = fig.add_axes([left,bottom,width,height])
    

    第 3 步:

    读入图片:

    import Image
    im = Image.open('image_file_name.png')
    

    第四步:

    在新创建的 Axes 上调用 imshow(),并转动轴线:

    logo_axes.imshow(im)
    logo_axes.axis('off')
    

    第 5 步:

    由于returnfig=True导致mplfinance显示该图,请致电mpf.show()

    mpf.show()
    

    【讨论】:

    • 我在看专家的回答,我是不是搞错了?
    • 如此详尽的解释对mpf的用户来说很有价值。 +1
    • @r-beginners ...您的方法很好。如上所述,Figure.figimage() 是我所知道的将图像放置在图形上的三种技术之一。老实说,它和Axes.imshow() 一样简单(如上图所示)。只有AnnotationBbox 技术变得更复杂一些。
    【解决方案2】:

    我不确定此答案是否会对您有所帮助,因为我不确定您要添加哪种图像。我假设您想添加公司徽标或类似的东西,所以我做了一些研究并找到了answer 是否可以向 mpf 添加水印。我将此答案用作指南,并将stackoveflow.com 上使用的图标添加到图表中。但是,不可能将它们添加到轴上,所以我不得不将它们添加到无花果中。我已更改样式以添加图像。

    img = plt.imread('./data/se-icon.png')
    
    market_colors = mpf.make_marketcolors(
            base_mpf_style="charles"
        )
    rc = {
        "axes.labelcolor": "none",
        "axes.spines.bottom": True,
        "axes.spines.left": False,
        "axes.spines.right": False,
        "axes.spines.top": False,
        "font.size": 10,
    }
    styles = mpf.make_mpf_style(
        base_mpf_style="yahoo",# nightclouds
        marketcolors=market_colors,
        gridstyle="",
        rc=rc
    )
    
    filledShape = {
        "y1": df['Close'].values,
        "facecolor": "#2279e4"
    }
    
    fig, axes = mpf.plot(df, type='line',
                         title='Test',
                         linecolor='white',
                         style=styles,
                         volume=True,
                         figsize=(8, 6),
                         figscale=0.5,
                         fill_between=filledShape,
                         tight_layout=True,
                         scale_padding={'left': 1, 'top': 5, 'right': 1, 'bottom': 2},
                         returnfig=True
    )
    
    #axes[0].imshow(img)
    #height = img.shape[1]
    
    fig.figimage(img, 0, fig.bbox.ymax - height*1.5)
    plt.show()
    

    【讨论】:

    • 另外,我不太明白你想用 x 轴空间保留做什么。
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2021-10-10
    相关资源
    最近更新 更多