Skip to content

Instantly share code, notes, and snippets.

@ppizarror
Created February 20, 2017 03:43
Show Gist options
  • Save ppizarror/a36d214fd38a029cb80b7363bb133023 to your computer and use it in GitHub Desktop.
Save ppizarror/a36d214fd38a029cb80b7363bb133023 to your computer and use it in GitHub Desktop.
Paste an image into a matlab plot to (x,y) coordinates
# coding=utf-8
"""
This function adds a figure to (x,y) position to a plot.
Autor: Pablo Pizarro @ppizarror.com, 2017.
"""
# Library importation
import matplotlib.pyplot as plt
from matplotlib.offsetbox import (OffsetImage, AnnotationBbox)
from matplotlib.cbook import get_sample_data
def addfigure_xy(x, y, ax, file_dir, zoom=1.0, imgformat='png'):
"""
Add a figure (from file direction) to a [x, y] position to a plot (stored in ax)
:param x: Coordenate x to append file
:param y: Coordenate y to append file
:param ax: Matlab plot object to append file
:param file_dir: Direction to file
:param zoom: Image zoom
:param imgformat: Format of the image
:return:
"""
# Load image
fn = get_sample_data(file_dir, asfileobj=False)
arr_img = plt.imread(fn, format=imgformat)
imagebox = OffsetImage(arr_img, zoom=zoom)
imagebox.image.axes = ax
# Create Annotation box
ab = AnnotationBbox(imagebox, [x, y], frameon=False)
ax.add_artist(ab)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment