如何在 matplotlib 图的后面叠加图像?
How do I superimpose an image in the back of a matplotlib plot?
我正在尝试在 matplotlib 图的后面叠加图像。它在 flask 网站中被渲染为 HTML 所以我在插入它之前将它保存为图像。没有背景图片的情节是这样的:
产生上述输出的代码在这里:
fname = 'scatter_averages.png'
url_full = os.path.join(_path_full, fname)
image = plt.imread("app/static/images/quantum_grid.jpg")
if os.path.isfile(url_full):
os.remove(url_full)
plt.clf()
df = self.text_numeric_averages()
if df is not None:
plt.figure(figsize=(6, 8))
fig, ax = plt.subplots()
df.index += 1
x, y = df.iloc[:, 2], df.iloc[:, 1]
ax.plot(x, y, '-o')
plt.xlabel(df.columns[2])
plt.ylabel(df.columns[1])
for i in range(len(df)):
xyi = df.iloc[i, :].values
ax.annotate(str(df.index[i]) + " " + xyi[0][:3], (xyi[2], xyi[1]))
axes = plt.gca()
y_min, y_max = axes.get_ylim()
x_min, x_max = axes.get_xlim()
# ax.imshow(image, extent=[x_min, x_max, y_min, y_max])
plt.savefig(url_full)
上面注释掉的那一行是我试图让图像叠加。取消注释该行时的输出是这样的:
如何保持第一张图片的大小和比例,但在第二张图中使用背景图片作为背景?我不担心图像看起来失真。
ax.imshow(image, extent=[x_min, x_max, y_min, y_max], aspect="auto")
这将解决它。
我正在尝试在 matplotlib 图的后面叠加图像。它在 flask 网站中被渲染为 HTML 所以我在插入它之前将它保存为图像。没有背景图片的情节是这样的:
产生上述输出的代码在这里:
fname = 'scatter_averages.png'
url_full = os.path.join(_path_full, fname)
image = plt.imread("app/static/images/quantum_grid.jpg")
if os.path.isfile(url_full):
os.remove(url_full)
plt.clf()
df = self.text_numeric_averages()
if df is not None:
plt.figure(figsize=(6, 8))
fig, ax = plt.subplots()
df.index += 1
x, y = df.iloc[:, 2], df.iloc[:, 1]
ax.plot(x, y, '-o')
plt.xlabel(df.columns[2])
plt.ylabel(df.columns[1])
for i in range(len(df)):
xyi = df.iloc[i, :].values
ax.annotate(str(df.index[i]) + " " + xyi[0][:3], (xyi[2], xyi[1]))
axes = plt.gca()
y_min, y_max = axes.get_ylim()
x_min, x_max = axes.get_xlim()
# ax.imshow(image, extent=[x_min, x_max, y_min, y_max])
plt.savefig(url_full)
上面注释掉的那一行是我试图让图像叠加。取消注释该行时的输出是这样的:
如何保持第一张图片的大小和比例,但在第二张图中使用背景图片作为背景?我不担心图像看起来失真。
ax.imshow(image, extent=[x_min, x_max, y_min, y_max], aspect="auto")
这将解决它。