基于matplotlib绘图库的图表参数设置(python语言)

基于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')
(80, 'boxplot.showmeans')
(81, 'boxplot.vertical')
(82, 'boxplot.whiskerprops.color')
(83, 'boxplot.whiskerprops.linestyle')
(84, 'boxplot.whiskerprops.linewidth')
(85, 'boxplot.whiskers')
(86, 'contour.corner_mask')
(87, 'contour.negative_linestyle')
(88, 'datapath')
(89, 'date.autoformatter.day')
(90, 'date.autoformatter.hour')
(91, 'date.autoformatter.microsecond')
(92, 'date.autoformatter.minute')
(93, 'date.autoformatter.month')
(94, 'date.autoformatter.second')
(95, 'date.autoformatter.year')
(96, 'docstring.hardcopy')
(97, 'errorbar.capsize')
(98, 'examples.directory')
(99, 'figure.autolayout')
(100, 'figure.constrained_layout.h_pad')
(101, 'figure.constrained_layout.hspace')
(102, 'figure.constrained_layout.use')
(103, 'figure.constrained_layout.w_pad')
(104, 'figure.constrained_layout.wspace')
(105, 'figure.dpi')
(106, 'figure.edgecolor')
(107, 'figure.facecolor')
(108, 'figure.figsize')
(109, 'figure.frameon')
(110, 'figure.max_open_warning')
(111, 'figure.subplot.bottom')
(112, 'figure.subplot.hspace')
(113, 'figure.subplot.left')
(114, 'figure.subplot.right')
(115, 'figure.subplot.top')
(116, 'figure.subplot.wspace')
(117, 'figure.titlesize')
(118, 'figure.titleweight')
(119, 'font.cursive')
(120, 'font.family')
(121, 'font.fantasy')
(122, 'font.monospace')
(123, 'font.sans-serif')
(124, 'font.serif')
(125, 'font.size')
(126, 'font.stretch')
(127, 'font.style')
(128, 'font.variant')
(129, 'font.weight')
(130, 'grid.alpha')
(131, 'grid.color')
(132, 'grid.linestyle')
(133, 'grid.linewidth')
(134, 'hatch.color')
(135, 'hatch.linewidth')
(136, 'hist.bins')
(137, 'image.aspect')
(138, 'image.cmap')
(139, 'image.composite_image')
(140, 'image.interpolation')
(141, 'image.lut')
(142, 'image.origin')
(143, 'image.resample')
(144, 'interactive')
(145, 'keymap.all_axes')
(146, 'keymap.back')
(147, 'keymap.forward')
(148, 'keymap.fullscreen')
(149, 'keymap.grid')
(150, 'keymap.grid_minor')
(151, 'keymap.home')
(152, 'keymap.pan')
(153, 'keymap.quit')
(154, 'keymap.quit_all')
(155, 'keymap.save')
(156, 'keymap.xscale')
(157, 'keymap.yscale')
(158, 'keymap.zoom')
(159, 'legend.borderaxespad')
(160, 'legend.borderpad')
(161, 'legend.columnspacing')
(162, 'legend.edgecolor')
(163, 'legend.facecolor')
(164, 'legend.fancybox')
(165, 'legend.fontsize')
(166, 'legend.framealpha')
(167, 'legend.frameon')
(168, 'legend.handleheight')
(169, 'legend.handlelength')
(170, 'legend.handletextpad')
(171, 'legend.labelspacing')
(172, 'legend.loc')
(173, 'legend.markerscale')
(174, 'legend.numpoints')
(175, 'legend.scatterpoints')
(176, 'legend.shadow')
(177, 'lines.antialiased')
(178, 'lines.color')
(179, 'lines.dash_capstyle')
(180, 'lines.dash_joinstyle')
(181, 'lines.dashdot_pattern')
(182, 'lines.dashed_pattern')
(183, 'lines.dotted_pattern')
(184, 'lines.linestyle')
(185, 'lines.linewidth')
(186, 'lines.marker')
(187, 'lines.markeredgewidth')
(188, 'lines.markersize')
(189, 'lines.scale_dashes')
(190, 'lines.solid_capstyle')
(191, 'lines.solid_joinstyle')
(192, 'markers.fillstyle')
(193, 'mathtext.bf')
(194, 'mathtext.cal')
(195, 'mathtext.default')
(196, 'mathtext.fallback_to_cm')
(197, 'mathtext.fontset')
(198, 'mathtext.it')
(199, 'mathtext.rm')
(200, 'mathtext.sf')
(201, 'mathtext.tt')
(202, 'patch.antialiased')
(203, 'patch.edgecolor')
(204, 'patch.facecolor')
(205, 'patch.force_edgecolor')
(206, 'patch.linewidth')
(207, 'path.effects')
(208, 'path.simplify')
(209, 'path.simplify_threshold')
(210, 'path.sketch')
(211, 'path.snap')
(212, 'pdf.compression')
(213, 'pdf.fonttype')
(214, 'pdf.inheritcolor')
(215, 'pdf.use14corefonts')
(216, 'pgf.debug')
(217, 'pgf.preamble')
(218, 'pgf.rcfonts')
(219, 'pgf.texsystem')
(220, 'polaraxes.grid')
(221, 'ps.distiller.res')
(222, 'ps.fonttype')
(223, 'ps.papersize')
(224, 'ps.useafm')
(225, 'ps.usedistiller')
(226, 'savefig.bbox')
(227, 'savefig.directory')
(228, 'savefig.dpi')
(229, 'savefig.edgecolor')
(230, 'savefig.facecolor')
(231, 'savefig.format')
(232, 'savefig.frameon')
(233, 'savefig.jpeg_quality')
(234, 'savefig.orientation')
(235, 'savefig.pad_inches')
(236, 'savefig.transparent')
(237, 'scatter.marker')
(238, 'svg.fonttype')
(239, 'svg.hashsalt')
(240, 'svg.image_inline')
(241, 'text.antialiased')
(242, 'text.color')
(243, 'text.hinting')
(244, 'text.hinting_factor')
(245, 'text.latex.preamble')
(246, 'text.latex.preview')
(247, 'text.latex.unicode')
(248, 'text.usetex')
(249, 'timezone')
(250, 'tk.window_focus')
(251, 'toolbar')
(252, 'verbose.fileo')
(253, 'verbose.level')
(254, 'webagg.address')
(255, 'webagg.open_in_browser')
(256, 'webagg.port')
(257, 'webagg.port_retries')
(258, 'xtick.alignment')
(259, 'xtick.bottom')
(260, 'xtick.color')
(261, 'xtick.direction')
(262, 'xtick.labelbottom')
(263, 'xtick.labelsize')
(264, 'xtick.labeltop')
(265, 'xtick.major.bottom')
(266, 'xtick.major.pad')
(267, 'xtick.major.size')
(268, 'xtick.major.top')
(269, 'xtick.major.width')
(270, 'xtick.minor.bottom')
(271, 'xtick.minor.pad')
(272, 'xtick.minor.size')
(273, 'xtick.minor.top')
(274, 'xtick.minor.visible')
(275, 'xtick.minor.width')
(276, 'xtick.top')
(277, 'ytick.alignment')
(278, 'ytick.color')
(279, 'ytick.direction')
(280, 'ytick.labelleft')
(281, 'ytick.labelright')
(282, 'ytick.labelsize')
(283, 'ytick.left')
(284, 'ytick.major.left')
(285, 'ytick.major.pad')
(286, 'ytick.major.right')
(287, 'ytick.major.size')
(288, 'ytick.major.width')
(289, 'ytick.minor.left')
(290, 'ytick.minor.pad')
(291, 'ytick.minor.right')
(292, 'ytick.minor.size')
(293, 'ytick.minor.visible')
(294, 'ytick.minor.width')
(295, 'ytick.right')

则这些都是可以设置的属性。那么问题又来了,我怎么知道需不需要提前设置呢?那么需要查看plt.rcParams的各项设置默认属性,输入以下代码:

print(plt.rcParams)

运行代码:

_internal.classic_mode: False
agg.path.chunksize: 0
animation.avconv_args: []
animation.avconv_path: avconv
animation.bitrate: -1
animation.codec: h264
animation.convert_args: []
animation.convert_path: convert
animation.embed_limit: 20.0
animation.ffmpeg_args: []
animation.ffmpeg_path: ffmpeg
animation.frame_format: png
animation.html: none
animation.html_args: []
animation.writer: ffmpeg
axes.autolimit_mode: data
axes.axisbelow: line
axes.edgecolor: k
axes.facecolor: w
axes.formatter.limits: [-7, 7]
axes.formatter.min_exponent: 0
axes.formatter.offset_threshold: 4
axes.formatter.use_locale: False
axes.formatter.use_mathtext: False
axes.formatter.useoffset: True
axes.grid: False
axes.grid.axis: both
axes.grid.which: major
axes.hold: None
axes.labelcolor: k
axes.labelpad: 4.0
axes.labelsize: medium
axes.labelweight: normal
axes.linewidth: 0.8
axes.prop_cycle: cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])
axes.spines.bottom: True
axes.spines.left: True
axes.spines.right: True
axes.spines.top: True
axes.titlepad: 6.0
axes.titlesize: large
axes.titleweight: normal
axes.unicode_minus: True
axes.xmargin: 0.05
axes.ymargin: 0.05
axes3d.grid: True
backend: module://ipykernel.pylab.backend_inline
backend.qt4: None
backend.qt5: None
backend_fallback: True
boxplot.bootstrap: None
boxplot.boxprops.color: k
boxplot.boxprops.linestyle: -
boxplot.boxprops.linewidth: 1.0
boxplot.capprops.color: k
boxplot.capprops.linestyle: -
boxplot.capprops.linewidth: 1.0
boxplot.flierprops.color: k
boxplot.flierprops.linestyle: none
boxplot.flierprops.linewidth: 1.0
boxplot.flierprops.marker: o
boxplot.flierprops.markeredgecolor: k
boxplot.flierprops.markerfacecolor: none
boxplot.flierprops.markersize: 6.0
boxplot.meanline: False
boxplot.meanprops.color: C2
boxplot.meanprops.linestyle: --
boxplot.meanprops.linewidth: 1.0
boxplot.meanprops.marker: ^
boxplot.meanprops.markeredgecolor: C2
boxplot.meanprops.markerfacecolor: C2
boxplot.meanprops.markersize: 6.0
boxplot.medianprops.color: C1
boxplot.medianprops.linestyle: -
boxplot.medianprops.linewidth: 1.0
boxplot.notch: False
boxplot.patchartist: False
boxplot.showbox: True
boxplot.showcaps: True
boxplot.showfliers: True
boxplot.showmeans: False
boxplot.vertical: True
boxplot.whiskerprops.color: k
boxplot.whiskerprops.linestyle: -
boxplot.whiskerprops.linewidth: 1.0
boxplot.whiskers: 1.5
contour.corner_mask: True
contour.negative_linestyle: dashed
datapath: c:\programdata\anaconda3\envs\tensorflow\lib\site-packages\matplotlib\mpl-data
date.autoformatter.day: %Y-%m-%d
date.autoformatter.hour: %m-%d %H
date.autoformatter.microsecond: %M:%S.%f
date.autoformatter.minute: %d %H:%M
date.autoformatter.month: %Y-%m
date.autoformatter.second: %H:%M:%S
date.autoformatter.year: %Y
docstring.hardcopy: False
errorbar.capsize: 0.0
examples.directory: 
figure.autolayout: False
figure.constrained_layout.h_pad: 0.04167
figure.constrained_layout.hspace: 0.02
figure.constrained_layout.use: False
figure.constrained_layout.w_pad: 0.04167
figure.constrained_layout.wspace: 0.02
figure.dpi: 72.0
figure.edgecolor: (1, 1, 1, 0)
figure.facecolor: (1, 1, 1, 0)
figure.figsize: [6.0, 4.0]
figure.frameon: True
figure.max_open_warning: 20
figure.subplot.bottom: 0.125
figure.subplot.hspace: 0.2
figure.subplot.left: 0.125
figure.subplot.right: 0.9
figure.subplot.top: 0.88
figure.subplot.wspace: 0.2
figure.titlesize: large
figure.titleweight: normal
font.cursive: ['Apple Chancery', 'Textile', 'Zapf Chancery', 'Sand', 'Script MT', 'Felipa', 'cursive']
font.family: ['sans-serif']
font.fantasy: ['Comic Sans MS', 'Chicago', 'Charcoal', 'ImpactWestern', 'Humor Sans', 'xkcd', 'fantasy']
font.monospace: ['DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Computer Modern Typewriter', 'Andale Mono', 'Nimbus Mono L', 'Courier New', 'Courier', 'Fixed', 'Terminal', 'monospace']
font.sans-serif: ['DejaVu Sans', 'Bitstream Vera Sans', 'Computer Modern Sans Serif', 'Lucida Grande', 'Verdana', 'Geneva', 'Lucid', 'Arial', 'Helvetica', 'Avant Garde', 'sans-serif']
font.serif: ['DejaVu Serif', 'Bitstream Vera Serif', 'Computer Modern Roman', 'New Century Schoolbook', 'Century Schoolbook L', 'Utopia', 'ITC Bookman', 'Bookman', 'Nimbus Roman No9 L', 'Times New Roman', 'Times', 'Palatino', 'Charter', 'serif']
font.size: 10.0
font.stretch: normal
font.style: normal
font.variant: normal
font.weight: normal
grid.alpha: 1.0
grid.color: #b0b0b0
grid.linestyle: -
grid.linewidth: 0.8
hatch.color: k
hatch.linewidth: 1.0
hist.bins: 10
image.aspect: equal
image.cmap: viridis
image.composite_image: True
image.interpolation: nearest
image.lut: 256
image.origin: upper
image.resample: True
interactive: True
keymap.all_axes: ['a']
keymap.back: ['left', 'c', 'backspace']
keymap.forward: ['right', 'v']
keymap.fullscreen: ['f', 'ctrl+f']
keymap.grid: ['g']
keymap.grid_minor: ['G']
keymap.home: ['h', 'r', 'home']
keymap.pan: ['p']
keymap.quit: ['ctrl+w', 'cmd+w', 'q']
keymap.quit_all: ['W', 'cmd+W', 'Q']
keymap.save: ['s', 'ctrl+s']
keymap.xscale: ['k', 'L']
keymap.yscale: ['l']
keymap.zoom: ['o']
legend.borderaxespad: 0.5
legend.borderpad: 0.4
legend.columnspacing: 2.0
legend.edgecolor: 0.8
legend.facecolor: inherit
legend.fancybox: True
legend.fontsize: medium
legend.framealpha: 0.8
legend.frameon: True
legend.handleheight: 0.7
legend.handlelength: 2.0
legend.handletextpad: 0.8
legend.labelspacing: 0.5
legend.loc: best
legend.markerscale: 1.0
legend.numpoints: 1
legend.scatterpoints: 1
legend.shadow: False
lines.antialiased: True
lines.color: C0
lines.dash_capstyle: butt
lines.dash_joinstyle: round
lines.dashdot_pattern: [6.4, 1.6, 1.0, 1.6]
lines.dashed_pattern: [3.7, 1.6]
lines.dotted_pattern: [1.0, 1.65]
lines.linestyle: -
lines.linewidth: 1.5
lines.marker: None
lines.markeredgewidth: 1.0
lines.markersize: 6.0
lines.scale_dashes: True
lines.solid_capstyle: projecting
lines.solid_joinstyle: round
markers.fillstyle: full
mathtext.bf: sans:bold
mathtext.cal: cursive
mathtext.default: it
mathtext.fallback_to_cm: True
mathtext.fontset: dejavusans
mathtext.it: sans:italic
mathtext.rm: sans
mathtext.sf: sans
mathtext.tt: monospace
patch.antialiased: True
patch.edgecolor: k
patch.facecolor: C0
patch.force_edgecolor: False
patch.linewidth: 1.0
path.effects: []
path.simplify: True
path.simplify_threshold: 0.1111111111111111
path.sketch: None
path.snap: True
pdf.compression: 6
pdf.fonttype: 3
pdf.inheritcolor: False
pdf.use14corefonts: False
pgf.debug: False
pgf.preamble: []
pgf.rcfonts: True
pgf.texsystem: xelatex
polaraxes.grid: True
ps.distiller.res: 6000
ps.fonttype: 3
ps.papersize: letter
ps.useafm: False
ps.usedistiller: False
savefig.bbox: None
savefig.directory: ~
savefig.dpi: figure
savefig.edgecolor: w
savefig.facecolor: w
savefig.format: png
savefig.frameon: True
savefig.jpeg_quality: 95
savefig.orientation: portrait
savefig.pad_inches: 0.1
savefig.transparent: False
scatter.marker: o
svg.fonttype: path
svg.hashsalt: None
svg.image_inline: True
text.antialiased: True
text.color: k
text.hinting: auto
text.hinting_factor: 8
text.latex.preamble: []
text.latex.preview: False
text.latex.unicode: False
text.usetex: False
timezone: UTC
tk.window_focus: False
toolbar: toolbar2
verbose.fileo: sys.stdout
verbose.level: silent
webagg.address: 127.0.0.1
webagg.open_in_browser: True
webagg.port: 8988
webagg.port_retries: 50
xtick.alignment: center
xtick.bottom: True
xtick.color: k
xtick.direction: out
xtick.labelbottom: True
xtick.labelsize: medium
xtick.labeltop: False
xtick.major.bottom: True
xtick.major.pad: 3.5
xtick.major.size: 3.5
xtick.major.top: True
xtick.major.width: 0.8
xtick.minor.bottom: True
xtick.minor.pad: 3.4
xtick.minor.size: 2.0
xtick.minor.top: True
xtick.minor.visible: False
xtick.minor.width: 0.6
xtick.top: False
ytick.alignment: center_baseline
ytick.color: k
ytick.direction: out
ytick.labelleft: True
ytick.labelright: False
ytick.labelsize: medium
ytick.left: True
ytick.major.left: True
ytick.major.pad: 3.5
ytick.major.right: True
ytick.major.size: 3.5
ytick.major.width: 0.8
ytick.minor.left: True
ytick.minor.pad: 3.4
ytick.minor.right: True
ytick.minor.size: 2.0
ytick.minor.visible: False
ytick.minor.width: 0.6
ytick.right: False

这里可以看到,image.cmap: viridis,viridis为“翠绿色”,故会产生第三张图错误的输出。

猜你喜欢

转载自blog.csdn.net/u011106767/article/details/80065536
今日推荐