Skip to content

Instantly share code, notes, and snippets.

@sabopy
Created November 5, 2018 15:27
Show Gist options
  • Save sabopy/5e1808ab957fd78077d94cddebac291a to your computer and use it in GitHub Desktop.
Save sabopy/5e1808ab957fd78077d94cddebac291a to your computer and use it in GitHub Desktop.
マーカーを画像にして散布図
#picture scatter plot
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
def imscatter(x, y, image, ax=None, zoom=1):
if ax is None:
ax = plt.gca()
try:
image = plt.imread(image)
except TypeError:
# Likely already an array...
pass
im = OffsetImage(image, zoom=zoom)
x, y = np.atleast_1d(x, y)
artists = []
for x0, y0 in zip(x, y):
ab = AnnotationBbox(im, (x0, y0), xycoords='data', frameon=False)
artists.append(ax.add_artist(ab))
ax.update_datalim(np.column_stack([x, y]))
ax.autoscale()
return artists
x,y =np.random.rand(2, 20)
image_path = 'cactus5.png'
fig, ax = plt.subplots()
imscatter(x, y, image_path, ax=ax, zoom=.25)
ax.plot(x, y, 'ko',alpha=0)
plt.savefig('cactus_plot.png',dpi=200, transparent=False)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment