NoNam4
NoNam4

Reputation: 597

how to add image to plot mplfinance python

Trying to add image and price label and add more space on time and it seems like ylim= takes care of that but when i add it my whole graph disappears.

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

Upvotes: 0

Views: 781

Answers (2)

r-beginners
r-beginners

Reputation: 35155

I'm not sure if this answer will help you or not since I'm not sure what kind of images you want to add. I assume you want to add a corporate logo or something like that, so I did some research and found an answer to whether you can add a watermark to an mpf. I used this answer as a guide and added the icons used on stackoveflow.com to the graph. However, it was not possible to add them to the axes, so I had to add them to the fig. I have changed the style to add the image.

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

enter image description here

Upvotes: 0

Daniel Goldfarb
Daniel Goldfarb

Reputation: 7714

There are three techniques that I know of to display an image on a matplotlib plot:

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

In terms of working with mplfinance, I would say that technique one, calling Axes.imshow() is probably simplest:

Step 1:

For all three of the above techniques, when you call mpf.plot() set kwarg returnfig=True:

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

This will give you access to the mplfinance Figure and Axes objects.

Step 2:

Now create a new Axes object where you want the image/logo:

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

Step 3:

Read in the image:

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

Step 4:

Call imshow() on the newly created Axes, and turn of the axis lines:

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

Step 5:

Since returnfig=True causes mplfinance to not show the Figure, call mpf.show()

mpf.show()

Upvotes: 1

Related Questions