我正在尝试绘制一个简单的线条图并将背景图像插入到图中.
示例pic(使用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()
ali_m.. 6
您可以使用plt.fill_between
创建覆盖原点和线之间区域的多边形,然后使用.set_clip_path
每个图像对象的方法仅显示位于多边形内的图像部分.
例如:
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()
您可以使用plt.fill_between
创建覆盖原点和线之间区域的多边形,然后使用.set_clip_path
每个图像对象的方法仅显示位于多边形内的图像部分.
例如:
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()