计算机视觉——【python】基于matplotlib绘图库的图表参数设置

本文介绍了在Python中使用matplotlib库进行图表绘制时的参数设置,特别是针对图像颜色映射的问题。通过调整`plt.rcParams`可以预先设定图像的属性,避免因默认设置导致的显示异常,例如将灰度图显示为彩色。文章提供了代码示例,展示了如何解决这一问题并展示了不同`cmap`设置对图像显示的影响。
摘要由CSDN通过智能技术生成

基于matplotlib绘图库的图表参数设置

环境配置

  1. 编程语言:python
  2. 编辑器:jupyter notebook

在使用python语言进行测试模型或者运行代码时,不免会使用到图表的绘制和图像的显示,这里简单说一下matplotlib库中pyplot函数的一些设置问题。

小提醒:

导入所需要的库

import matplotlib.pyplot as plt

若希望将matplotlib绘制的图标嵌入到notebook中,需要执行以下命令:(一般将其放置于import一系列语句的最后)

%matplotlib inline

在使用plt.show()语句进行显示图像时,需要对colormap(image.cmap)进行调整,如果你使用默认的参数属性,可能会带来一点小麻烦。比如,加载的是灰度图,但是由于默认属性,可能显示得并不是原图。

代码示例:

下面看一下代码:

from skimage import io, color, data
from skimage.transform import resize
import matplotlib.pyplot as plt
%matplotlib inline
shape = [128,128] #设置需要的图形尺寸
image_orig = io.imread("./images/rgb/rgb2.jpg")#注意imag文件路径,这里需根据系统调整
image_resize = resize(image_orig,shape)
image_gray = color.rgb2grey(image_resize)
print(image_gray.shape)

plt.figure(1) #创建图表1
ax1 = plt.subplot(131) #在图表1中创建子图(Axes)1
ax2 = plt.subplot(132)
ax3 = plt.subplot(133)


plt.sca(ax1)#选择图表1中子图1为当前活动对象
plt.imshow(image_orig)


plt.sca(ax2)
plt.imshow(image_resize)


plt.sca(ax3)
plt.imshow(image_gray)

运行结果为:
这里写图片描述

这里可以发现,本应该为灰度图像的三张图片竟然是这个鬼样子···
如果将最后一句代码,改为

plt.imshow(image_gray, cmap='gray')

输出就会正常:
这里写图片描述
那么这个cmap就是我们需要注意的地方(当然你也可以使用skimage.io.imshow()),下面来看一下这个plt的一些属性。

plt.rcParams

在一些代码当中,可以提前设置plt绘制的图象属性,比如:

plt.rcParams['figure.size']=(5.0, 4.0)#绘制图表的大小
plt.rcParams['image.interpolation']='nearest'#图像的插补格式
plt.rcParams['image.cmap']='gray'#颜色映射,如果本身就是三通道rgb图,此参数不需要调整

这些代码在程序运行前执行,则程序运行过程中的图标及图像格式都会遵从这些设定。
那么,关于plt.rcParams究竟有哪些参数可以设置,我们可以来看一看,在notebook中运行:

for i in enumerate(plt.rcParams):
    print(i)

运行结果为:

(0, '_internal.classic_mode')
(1, 'agg.path.chunksize')
(2, 'animation.avconv_args')
(3, 'animation.avconv_path')
(4, 'animation.bitrate')
(5, 'animation.codec')
(6, 'animation.convert_args')
(7, 'animation.convert_path')
(8, 'animation.embed_limit')
(9, 'animation.ffmpeg_args')
(10, 'animation.ffmpeg_path')
(11, 'animation.frame_format')
(12, 'animation.html')
(13, 'animation.html_args')
(14, 'animation.writer')
(15, 'axes.autolimit_mode')
(16, 'axes.axisbelow')
(17, 'axes.edgecolor')
(18, 'axes.facecolor')
(19, 'axes.formatter.limits')
(20, 'axes.formatter.min_exponent')
(21, 'axes.formatter.offset_threshold')
(22, 'axes.formatter.use_locale')
(23, 'axes.formatter.use_mathtext')
(24, 'axes.formatter.useoffset')
(25, 'axes.grid')
(26, 'axes.grid.axis')
(27, 'axes.grid.which')
(28, 'axes.hold')
(29, 'axes.labelcolor')
(30, 'axes.labelpad')
(31, 'axes.labelsize')
(32, 'axes.labelweight')
(33, 'axes.linewidth')
(34, 'axes.prop_cycle')
(35, 'axes.spines.bottom')
(36, 'axes.spines.left')
(37, 'axes.spines.right')
(38, 'axes.spines.top')
(39, 'axes.titlepad')
(40, 'axes.titlesize')
(41, 'axes.titleweight')
(42, 'axes.unicode_minus')
(43, 'axes.xmargin')
(44, 'axes.ymargin')
(45, 'axes3d.grid')
(46, 'backend')
(47, 'backend.qt4')
(48, 'backend.qt5')
(49, 'backend_fallback')
(50, 'boxplot.bootstrap')
(51, 'boxplot.boxprops.color')
(52, 'boxplot.boxprops.linestyle')
(53, 'boxplot.boxprops.linewidth')
(54, 'boxplot.capprops.color')
(55, 'boxplot.capprops.linestyle')
(56, 'boxplot.capprops.linewidth')
(57, 'boxplot.flierprops.color')
(58, 'boxplot.flierprops.linestyle')
(59, 'boxplot.flierprops.linewidth')
(60, 'boxplot.flierprops.marker')
(61, 'boxplot.flierprops.markeredgecolor')
(62, 'boxplot.flierprops.markerfacecolor')
(63, 'boxplot.flierprops.markersize')
(64, 'boxplot.meanline')
(65, 'boxplot.meanprops.color')
(66, 'boxplot.meanprops.linestyle')
(67, 'boxplot.meanprops.linewidth')
(68, 'boxplot.meanprops.marker')
(69, 'boxplot.meanprops.markeredgecolor')
(70, 'boxplot.meanprops.markerfacecolor')
(71, 'boxplot.meanprops.markersize')
(72, 'boxplot.medianprops.color')
(73, 'boxplot.medianprops.linestyle')
(74, 'boxplot.medianprops.linewidth')
(75, 'boxplot.notch')
(76, 'boxplot.patchartist')
(77, 'boxplot.showbox')
(78, 'boxplot.showcaps')
(79, 'boxplot.showfliers')
(
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值