[英]Display a image with imshow to be behind another plot in matplotlib
我有一个numpy 2d数组,我想以位图图像的形式显示在规则的线条图后面。
import matplotlib as mpl
from numpy import arange
figure = mpl.figure.Figure(dpi=70)
image = my_numpy_array #This is a regular float32 2D array created somewhere else
#I'm using a single sinus phase but the plot could be any (x, y) based function
x = arange(0, 360, 0.01)
y = 100*sin(pi*self.x/180)
subplot = figure.add_subplot(111)
self.subplot.imshow(image) #Doesn't work; how do I display the bitmap in the same subplot
self.subplot.plot(x,y)
figure.show() #I see the graphic and the sinus just fine but not my bitmap
最终,我想要这样的东西:
其中0对应于白色,并且根据色标会出现0.01到1之间的任何值。 现在,我看到的是窦波,但看不到点。
实际上,您提供的代码有效。 你只需要记住的是,图像打印时显示在x轴上从0
到image.width
和Y轴从0
到image.height
,所以你必须将它们对齐:
import numpy as np
import matplotlib.pyplot as plt
# Create a random image 200x360
image = np.random.randn(200, 360)
# Create a sin function from x = 0 - 360, y = -100 - 100
x = np.arange(0, 360, 0.01)
y = 100 * np.sin(np.pi * x / 180)
figure = plt.figure(dpi=70)
subplot = figure.add_subplot(111)
subplot.set_xlim(0, 360)
subplot.set_ylim(-100, 100)
subplot.imshow(image, extent=[min(x), max(x), min(y), max(y)])
subplot.plot(x, y)
figure.show()
产生图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.