Chart parameter setting based on matplotlib drawing library (python language)

Chart parameter setting based on matplotlib drawing library

  1. Programming language: python
  2. Editor: jupyter notebook

When using the python language to test the model or run the code, it is inevitable to use the drawing of the chart and the display of the image. Here we briefly talk about some setting problems of the pyplot function in the matplotlib library.

Reminder

Import the required libraries

import matplotlib.pyplot as plt

If you want to embed the icon drawn by matplotlib into the notebook, you need to execute the following command: (usually placed at the end of the import series of statements)

%matplotlib inline

When using the plt.show() statement to display the image, the colormap (image.cmap) needs to be adjusted. If you use the default parameter properties, it may bring a little trouble. For example, a grayscale image is loaded, but due to the default properties, it may not be displayed as the original image.

Code example:

Take a look at the code below:

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)

The running result is:
write picture description here

It can be found here that the three pictures that should be grayscale images turned out to be this ghost...
If the last sentence of code is changed to

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

The output will be normal:
write picture description here
then this cmap is where we need to pay attention (of course you can also use skimage.io.imshow()), let's take a look at some properties of this plt.

plt.rcParams

In some codes, the image properties drawn by plt can be set in advance, such as:

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

These codes are executed before the program runs, and the icons and image formats during the running of the program will follow these settings.
So, about what parameters can be set in plt.rcParams, we can take a look and run it in the notebook:


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

The running result is:

(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')

Then these are all properties that can be set. Then the question comes again, how do I know if I need to set it in advance? Then you need to view the default properties of plt.rcParams and enter the following code:

print(plt.rcParams)

Run the code:

_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

As you can see here, image.cmap: viridis, viridis is "emerald green", so the wrong output of the third image will be generated.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779779&siteId=291194637