简体   繁体   中英

Display a image with imshow to be behind another plot in matplotlib

I have a numpy 2d array that I want to display as a bitmap image behind a regular lined plot.

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

Ultimately I would like something like this:

在此处输入图片说明

Where 0 corresponds to white and any value between 0.01 and 1 will appear according to a colorscale. Right now I see the sinus wave but not the dots.

Actually the code you provided works. You just have to remember that the images when plotted are shown on x axis from 0 to image.width and on y axis from 0 to image.height , so you have to align them:

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()

Which produces the image:

情节背后的形象

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM