基于matplotlib绘图库的图表参数设置
环境配置
- 编程语言:python
- 编辑器: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')
(