我有一个numpy二维数组,我希望在常规的线条图后面显示一个位图图像。
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之间的任何值。现在我看到了正弦波而不是点。
答案 0 :(得分:4)
实际上您提供的代码有效。您只需要记住,绘制时的图像显示在从0
到image.width
的x轴和从0
到image.height
的y轴上,因此您必须对齐它们:
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()
产生图像: