python中定义图片位置,在python中的绘图中的自定义行之间放置图像

What I would like is to use specified functions or lines to create the border of the images, and then inside the borders place custom images.

I used fill between as an example, but maybe there is an easier way of achieving this. This is what I have in mind:

import matplotlib.pyplot as plt

import numpy as np

x = np.linspace(0,100,1000)

y = (3 + np.sin(x/5.)) * np.exp(-abs(x-50)/25.)*10

x2 = np.linspace(20,40,1000)

y2 = np.sqrt(100 - (x2 - 30)**2) + 40

y3 = -np.sqrt(100 - (x2 - 30)**2) + 40

plt.figure()

ax1 = plt.subplot(1,1,1, aspect='equal')

plt.fill_between(x=x, y1=y, y2=0, alpha=0.5, color='orange', linewidth=3)

plt.fill_between(x=x2, y1=y2, y2=y3, alpha=0.5, color='green', linewidth=3)

ax1.axis('off')

the output of that code is this image, what I have:

4fbf1653e469dc2b18386794c1d2f82e.png

and I would like something like this, what I want:

6a264ad5e1bc045d17ecdf0723980372.png

Please have in mind that I would like to have a border around the image that I can customize it, and if I have more than one fillbetween, I can choose what to put inside the different curves.

And also if its possible to respect the axis ratio of the image so its shape doesn't get distorted when I place it in the plot.

解决方案

There are a lot of different ways to obtain an image clipped to some shape. I personally like to use of an OffsetImage inside an AnnotationBbox. In the solution below a Patch is used as a clip_path for the image. If the input to the function is a Path, it is converted to a patch first.

63613345fe97e721fd6af5b90026f981.png

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.path import Path

from matplotlib.patches import PathPatch

from matplotlib.offsetbox import OffsetImage, AnnotationBbox

plt.figure()

ax1 = plt.subplot(1,1,1, aspect='equal')

ax1.axis('off')

def img_to_path( fn, path, zoom=0.72, ax = None, **kwargs):

if ax==None: ax=plt.gca()

kwargs.pop("facecolor", None)

im = plt.imread(fn, format='png')

if type(path) == Path:

xmin = path.vertices[:,0].min()

ymin = path.vertices[:,1].min()

patch = PathPatch(path, facecolor='none', zorder=3, **kwargs)

else:

patch = path

xmin = patch.get_verts()[:,0].min()

ymin = patch.get_verts()[:,1].min()

ax.add_patch(patch)

imagebox = OffsetImage(im, zoom=zoom, clip_path=patch, zorder=-10)

boxoffset = np.array(im.shape[:2][::-1])/2.*zoom

ab = AnnotationBbox(imagebox, (xmin,ymin), xycoords='data',

xybox=boxoffset, boxcoords="offset points",

pad=0, frameon=False)

ax.add_artist(ab)

# two images

fn1 = "data/raspberries.png"

fn2 = "data/blueberries.png"

# use a Path

x = np.linspace(0,100,1000)

y = (3 + np.sin(x/5.)) * np.exp(-abs(x-50)/25.)*10

x = np.append(x,[x[-1],x[0], x[0]])

y = np.append(y,[0,0,y[0]])

path = Path(np.c_[x,y], [1]+[2]*(len(x)-2)+[79])

img_to_path(fn1, path, edgecolor="red", lw=4, ax=ax1 )

# use a Patch

circ = plt.Circle((30,40), 10, edgecolor="gold", lw=13, zorder=3 )

img_to_path(fn2, circ, zoom=0.15, ax=ax1 )

ax1.relim()

ax1.autoscale_view()

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值