如何用 Matplotlib 显示图片
本教程将讨论如何使用 matplotlib.pyplot.imshow()
方法来使用 Matplotlib 显示图片。
matplotlib.pyplot.imshow()
方法
matplotlib.pyplot.imshow()
在 Matplotlib 中显示数字。
matplotlib.pyplot.imshow()
语法
matplotlib.pyplot.imshow(X,
cmap=None,
norm=None,
aspect=None,
interpolation=None,
alpha=None,
vmin=None,
vmax=None,
origin=None,
extent=None, *,
filternorm=True,
filterrad=4.0,
resample=None,
url=None,
data=None,
**kwargs)
在这里,X
代表显示的图片或 PIL
图片的数组式结构。
示例:用 Matplotlib Python 使用 imshow()
显示图片
import matplotlib.pyplot as plt
import matplotlib.image as img
image = img.imread('lena.jpg')
plt.imshow(image)
plt.show()
输出:
它使用 matplotlib.image
模块中的 imread()
方法从当前工作目录中读取图片 lena.jpg
,最后使用 imshow()
方法显示图片。如果不使用 IPython Notebooks
,必须在 imshow()
之后调用 show()
方法才能查看图片,show()
方法会启动图片的单独窗口。
示例:用 Matplotlib Python 使用 imshow()
显示 PIL
图片
import matplotlib.pyplot as plt
from PIL import Image
image = Image.open('lena.jpg')
plt.imshow(image)
plt.show()
输出:
它显示 PIL
图片。我们使用 PIL
的 Image
模块中的 open()
方法读取它。我们也可以用 PIL
直接显示图片,方法简单得多。
from PIL import Image
img = Image.open('lena.jpg')
img.show()
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串