Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib图形上打印裁剪的背景图像_Python_Pandas_Matplotlib_Plot - Fatal编程技术网

Python 在matplotlib图形上打印裁剪的背景图像

Python 在matplotlib图形上打印裁剪的背景图像,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我正在尝试绘制一个简单的线图,并在图中插入背景图像 示例图片(带有cat.jpg和dog.jpd): 目前,我有一个代码来绘制线条(从熊猫数据帧),并将图像放入图中。但是,图像和线条图根本没有“交互” fig, ax = plt.subplots(figsize=(15,10)) cat = np.array(Image.open('cat.jpg')) dog = np.array(Image.open('dog.jpg')) ax.imshow(cat, extent=[0, 10, 0,

我正在尝试绘制一个简单的线图,并在图中插入背景图像

示例图片(带有cat.jpg和dog.jpd):

目前,我有一个代码来绘制线条(从熊猫数据帧),并将图像放入图中。但是,图像和线条图根本没有“交互”

fig, ax = plt.subplots(figsize=(15,10))
cat = np.array(Image.open('cat.jpg'))
dog = np.array(Image.open('dog.jpg'))
ax.imshow(cat, extent=[0, 10, 0, 18], aspect='auto',   cmap='gray',alpha=0.75)
ax.imshow(dog, extent=[10, 20, 0, 18], aspect='auto', cmap='gray',alpha=0.75)

ax.plot(df['Series'],color='#3cb8fb',alpha=0.95,linewidth=3.0)

plt.show()
可以使用创建覆盖原点和直线之间区域的多边形,然后使用每个图像对象的方法仅显示位于多边形内的图像部分

例如:

from matplotlib import pyplot as plt
from scipy.misc import lena

fig, ax = plt.subplots(1, 1)

x = np.linspace(0, 1, 10)
y = np.random.rand(10)
image = ax.imshow(lena(), cmap=plt.cm.gray, extent=[0, 1, 0, 1])
line = ax.plot(x, y, '-r', lw=2)

# invisible clipping polygon
poly = ax.fill_between(x, 0, y, visible=False)
clip = poly.get_paths()[0]

# you will need to do this separately for each of the images in your plot
image.set_clip_path(clip, transform=ax.transData)

plt.show()

您希望的是什么样的交互?我的意思是我希望裁剪猫和狗的图像,以便它们与线图一致(如示例图片中所示)。