如何在一个图形中显示多个图像?

2024-04-26 10:24:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python库matplotlib来绘制函数,我知道如何在一个图中以不同的子块s来绘制多个函数,比如这个图,enter image description here

在处理图像时,我使用imshow()来绘制图像,但是如何在不同的子块中用一个图形将多个图像一起绘制?


Tags: 函数图像图形matplotlib绘制子块imshow
2条回答

documentation提供了一个示例(大约是页面向下的四分之三):

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
fig = plt.figure()
a=fig.add_subplot(1,2,1)
img = mpimg.imread('../_static/stinkbug.png')
lum_img = img[:,:,0]
imgplot = plt.imshow(lum_img)
a.set_title('Before')
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation ='horizontal')
a=fig.add_subplot(1,2,2)
imgplot = plt.imshow(lum_img)
imgplot.set_clim(0.0,0.7)
a.set_title('After')
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation='horizontal')

# ---------------------------------------
# if needed inside the application logic, uncomment to show the images
# plt.show()

基本上,这与通常使用fig.add_subplot创建轴的方式相同。。。

在图形中绘制子块的简单python代码

rows=2
cols=3
fig, axes = plt.subplots(rows,cols,figsize=(30,10))
plt.subplots_adjust(wspace=0.1,hspace=0.2)
features=['INDUS','RM', 'AGE', 'DIS','PTRATIO','MEDV']
plotnum=1
for idx in features:
    plt.subplot(rows,cols,plotnum)
    sns.distplot(data[idx])
    plotnum=plotnum+1
plt.savefig('subplots.png')

浏览下面的链接了解更多详细信息 https://exploredatalab.com/how-to-plot-multiple-subplots-in-python-with-matplotlib/

相关问题 更多 >