带你十分钟快速入门画图神器 Matplotlib

                <p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(136,136,136);font-size:14px;">点击上方“</span><span style="font-size:14px;color:rgb(61,170,214);">程序人生</span><span style="color:rgb(136,136,136);font-size:14px;">”,选择“置顶公众号”</span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(136,136,136);font-size:14px;">第一时间关注程序猿(媛)身边的故事</span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;text-align:justify;background-color:rgb(255,255,255);"><span style="color:rgb(136,136,136);font-size:14px;"><br></span></p><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/1hReHaqafacxkPDHHM5z9ia7eJjDVF921JPNfhBGOqlnBQ7npnwQQhkALVzU4gibjCNicA4mJ3SrjLmzgrOianxaew/640?wx_fmt=jpeg" alt="640?wx_fmt=jpeg"></p><p style="color:rgb(105,115,117);"><span style="font-size:13px;text-align:right;color:rgb(61,170,214);font-family:'Optima-Regular', 'PingFangTC-light';">作者</span><br></p><p><span style="color:rgb(136,136,136);font-family:'Optima-Regular', 'PingFangTC-light';letter-spacing:.5px;font-size:13px;">王伟同学</span></p><p style="color:rgb(105,115,117);"><span style="color:rgb(204,204,204);font-family:'Optima-Regular', 'PingFangTC-light';font-weight:700;letter-spacing:.5px;text-align:justify;font-size:13px;">已获原作者授权,如需转载,请联系原作者。</span></p><blockquote style="border-left-width:5px;border-left-color:rgb(239,112,96);line-height:inherit;font-size:.9em;color:rgb(0,0,0);background-color:rgb(239,235,233);">一篇快速上手 Matplotlib 的好文章~</blockquote><p><br></p><p>在开始正式介绍 Matplotlib 用法之前,先来简单了解下 Matplotlib。</p><p><br></p><p><span style="text-decoration:underline;">Matplotlib 是 Python 的一个绘图库。它包含了大量的工具,你可以使用这些工具创建各种图形,包括简单的散点图,正弦曲线,甚至是三维图形。</span></p><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t0"></a><a name="t0"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">目录</span></h2><p><strong>1. 画一个简单的图形</strong></p><p><strong>2. 在一张图纸里绘制多个图形</strong></p><p><strong>3. 更多设置</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.1 设置 figure</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.2 设置标题</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.3 设置坐标轴</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.4 设置 label 和 legend</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;3.5 添加注释</strong></p><p><strong>4. 使用子图</strong></p><p><strong>5. 常见的图形</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;5.1 散点图</strong></p><p><strong>&nbsp;&nbsp;&nbsp;&nbsp;5.2 柱状图</strong></p><p><strong>6. 中文乱码解决</strong></p><p><br></p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs kotlin"><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;导入相关模块</span><br><span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-keyword">import</span></span>&nbsp;matplotlib.pyplot&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-keyword">as</span></span>&nbsp;plt<br><span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);">import</span>&nbsp;numpy&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-keyword">as</span></span>&nbsp;np<br></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t1"></a><a name="t1"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">画一个简单的图形</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">这里我们通过画出一个正弦曲线图来讲解下基本用法。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">首先通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">np.linspace</code><span class="Apple-converted-space">&nbsp;</span>方式生成 x,它包含了 50 个元素的数组,这 50 个元素均匀的分布在 [0, 2pi] 的区间上。然后通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">np.sin(x)</code><span class="Apple-converted-space">&nbsp;</span>生成 y。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs vbscript">x&nbsp;=&nbsp;np.linspace(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">0</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>&nbsp;*&nbsp;np.pi,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">50</span></span>)<br>y&nbsp;=&nbsp;np.<span class="hljs-built_in">sin</span>(x)<br></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><p style="font-size:inherit;color:inherit;line-height:inherit;">有了 x 和 y 数据之后,我们通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.plot(x, y)</code><span class="Apple-converted-space">&nbsp;</span>来画出图形,并通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.show()</code><span class="Apple-converted-space">&nbsp;</span>来显示。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs css"><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.show</span>()<br></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><p style="text-align:center;"><br></p><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0ac4x9AElvf0whyOz3P4Vc956UUiarWg9tQhHAheE8PFDXAn4mklTcG6rg/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t2"></a><a name="t2"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">在一张图纸里绘制多个图形</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">有时候,可能需要在一个图纸里绘制多个图形,这里我们同时绘制了 (x, y), (x, y * 2)两个图形。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs css"><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">2</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.show</span>()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acfdlNqHjE0OFuAf2eoZStE5IHjsJu58ZTvAD8jPhNfKnFjl2dbc8ibcQ/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">绘制出图形之后,我们可以自己调整更多的样式,比如颜色、点、线。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs java">plt.plot(x,&nbsp;y,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'y*-'</span></span>)<br>plt.plot(x,&nbsp;y&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'m--'</span></span>)<br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acicKsOE0mK3FVmM1ZeMRCGgicLvf3fXj1LY6d5W6PyWJVD2e1vf3sibCAQ/640?wx_fmt=png" alt="640?wx_fmt=png"></p><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">可以看到,设置样式时,就是增加了一个字符串参数,比如<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">'y*-'</code><span class="Apple-converted-space">&nbsp;</span>,其中 y 表示黄色,* 表示 星标的点,- 表示实线。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">这里列举一些常见的颜色表示方式:</p><div class="table-box"><table width="677" style="width:378px;"><thead style="font-size:inherit;color:inherit;line-height:inherit;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">颜色</th><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">表示方式</th></tr></thead><tbody style="font-size:inherit;color:inherit;line-height:inherit;border-width:0px;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">蓝色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">b</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">绿色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">g</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">红色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">r</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">青色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">c</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">品红</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">m</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">黄色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">y</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">黑色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">k</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">白色</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">w</td></tr></tbody></table></div><p style="font-size:inherit;color:inherit;line-height:inherit;">常见的点的表示方式:</p><div class="table-box"><table width="677" style="width:378px;"><thead style="font-size:inherit;color:inherit;line-height:inherit;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">点的类型</th><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">表示方式</th></tr></thead><tbody style="font-size:inherit;color:inherit;line-height:inherit;border-width:0px;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">点</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">.</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">像素</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">,</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">圆</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">o</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">方形</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">s</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">三角形</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">^</td></tr></tbody></table></div><p style="font-size:inherit;color:inherit;line-height:inherit;">常见的线的表示方式:</p><div class="table-box"><table width="677" style="width:378px;"><thead style="font-size:inherit;color:inherit;line-height:inherit;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">线的类型</th><th style="border-top-width:1px;border-color:rgb(204,204,204);background-color:rgb(240,240,240);color:inherit;line-height:inherit;font-size:12px;text-align:left;">表示方式</th></tr></thead><tbody style="font-size:inherit;color:inherit;line-height:inherit;border-width:0px;"><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">直线</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">-</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">虚线</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">--</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">点线</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">:</td></tr><tr style="font-size:inherit;color:inherit;line-height:inherit;border-width:1px 0px 0px;border-top-style:solid;border-top-color:rgb(204,204,204);background-color:#FFFFFF;"><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">点划线</td><td style="border-color:rgb(204,204,204);color:inherit;line-height:inherit;font-size:12px;">-.</td></tr></tbody></table></div><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t3"></a><a name="t3"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">更多设置</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">Matplotlib 支持各种灵活的设置,这里我们列举一些常见的内容。</p><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t4"></a><a name="t4"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">设置 figure</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">你可以认为Matplotlib绘制的图形都在一个默认的 figure 中,当然了,你可以自己创建 figure,好处就是可以控制更多的参数,常见的就是控制图形的大小,这里创建一个 figure,设置大小为<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">(6, 3)</code>。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs">plt.figure(figsize=(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">6</span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">3</span>))<br>plt.plot(x,&nbsp;y)<br>plt.plot(x,&nbsp;y&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">2</span>)<br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acrSnbuwNMSTbUSs3nmicSib0GeKdVnOMFrW40GqOrxw1810Vze8PbUN8g/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t5"></a><a name="t5"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">设置标题</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">来看下如何设置标题。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs css"><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">2</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.title</span>(<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);">"<span class="hljs-selector-tag">sin</span>(<span class="hljs-selector-tag">x</span>)&nbsp;&amp;&nbsp;2<span class="hljs-selector-tag">sin</span>(<span class="hljs-selector-tag">x</span>)"</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.show</span>()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acNZSvvpUgTg5KbGa0A1MNtfT8pmKyyicgOFzlIOAvjMhPmHeLIoRjkicA/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">直接通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.title</code><span class="Apple-converted-space">&nbsp;</span>即可设置图形标题。</p><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t6"></a><a name="t6"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">设置坐标轴</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">来看下如何设置坐标轴的范围以及名称。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs java">plt.plot(x,&nbsp;y)<br>plt.plot(x,&nbsp;y&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>)<br><br>plt.xlim((<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">0</span></span>,&nbsp;np.pi&nbsp;+&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">1</span></span>))<br>plt.ylim((<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">-<span class="hljs-number">3</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">3</span></span>))<br>plt.xlabel(<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'X'</span></span>)<br>plt.ylabel(<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'Y'</span></span>)<br><br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acFnqYRea7npeh7VuHCMhw9CentAHYJraUsBJP7oeMZ5geRAicTPw6fyQ/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">xlim</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">ylim</code><span class="Apple-converted-space">&nbsp;</span>来设限定轴的范围,通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">xlabel</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">ylabel</code><span class="Apple-converted-space">&nbsp;</span>来设置轴的名称。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">此外,我们也可以通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">xticks</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">yticks</code><span class="Apple-converted-space">&nbsp;</span>来设置轴的刻度。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs css"><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.plot</span>(<span class="hljs-selector-tag">x</span>,&nbsp;<span class="hljs-selector-tag">y</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">2</span>)<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.xticks</span>((<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">0</span>,&nbsp;<span class="hljs-selector-tag">np</span><span class="hljs-selector-class">.pi</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">0<span class="hljs-selector-class">.5</span></span>,&nbsp;<span class="hljs-selector-tag">np</span><span class="hljs-selector-class">.pi</span>,&nbsp;<span class="hljs-selector-tag">np</span><span class="hljs-selector-class">.pi</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">1<span class="hljs-selector-class">.5</span></span>,&nbsp;<span class="hljs-selector-tag">np</span><span class="hljs-selector-class">.pi</span>&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);">2</span>))<br><span class="hljs-selector-tag">plt</span><span class="hljs-selector-class">.show</span>()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acHST84iaeNQ2BLfXAE8onNlcH9DeEkpBPQOVZHMvicK6peVvaqc3MYUiaA/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t7"></a><a name="t7"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">设置 label 和 legend</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">设置 label 和 legend 的目的就是为了区分出每个数据对应的图形名称。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs python">plt.plot(x,&nbsp;y,&nbsp;label=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">"sin(x)"</span></span>)<br>plt.plot(x,&nbsp;y&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;label=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">"2sin(x)"</span></span>)<br><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#&nbsp;plt.legend()</span></span><br><span class="hljs-comment">plt.legend(loc=</span><span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-comment">'best'</span></span><span class="hljs-comment">)</span><br><span class="hljs-comment">plt.show()</span></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acBllPtw7PFUJ6H8z14rTZoPCuOfBXz6iaXvK8XtugkVIfd7qcMfycNMw/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t8"></a><a name="t8"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">添加注释</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">有时候我们需要对特定的点进行标注,我们可以使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.annotate</code><span class="Apple-converted-space">&nbsp;</span>函数来实现。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">这里我们要标注的点是<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">(x0, y0) = (π, 0)</code>。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">我们也可以使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.text</code><span class="Apple-converted-space">&nbsp;</span>函数来添加注释。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs go">plt.plot(x,&nbsp;y)<br><br>x0&nbsp;=&nbsp;np.pi<br>y0&nbsp;=&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">0</span></span><br><br><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;画出标注点</span><br>plt.scatter(x0,&nbsp;y0,&nbsp;s=<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">50</span></span>)<br><br>plt.annotate(<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'sin(np.pi)=%s'</span></span>&nbsp;%&nbsp;y0,&nbsp;xy=(np.pi,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">0</span></span>),&nbsp;xycoords=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'data'</span></span>,&nbsp;xytext=(+<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">30</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">-30</span></span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textcoords=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'offset&nbsp;points'</span></span>,&nbsp;fontsize=<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">16</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arrowprops=dict(arrowstyle=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'-&gt;'</span></span>,&nbsp;connectionstyle=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">"arc3,rad=.2"</span></span>))<br><br>plt.text(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">0.5</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">-0.25</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">"sin(np.pi)&nbsp;=&nbsp;0"</span></span>,&nbsp;fontdict={<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'size'</span></span>:&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">16</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'color'</span></span>:&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'r'</span></span>})<br><br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acxW7Uh9XsUUDXFh41IzJGZw45YOSR0jR37H1RkfDtWibss9cccLicOg0g/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">对于<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">annotate</code><span class="Apple-converted-space">&nbsp;</span>函数的参数,做一个简单解释:</p><ul class="list-paddingleft-2"><li><p><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">'sin(np.pi)=%s' % y0</code><span class="Apple-converted-space">&nbsp;</span>代表标注的内容,可以通过字符串 %s 将 y0 的值传入字符串;</p></li><li><p>参数<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">xycoords='data'</code><span class="Apple-converted-space">&nbsp;</span>是说基于数据的值来选位置;</p></li><li><p><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">xytext=(+30, -30)</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">textcoords='offset points'</code><span class="Apple-converted-space">&nbsp;</span>表示对于标注位置的描述 和 xy 偏差值,即标注位置是 xy 位置向右移动 30,向下移动30;</p></li><li><p><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">arrowprops</code><span class="Apple-converted-space">&nbsp;</span>是对图中箭头类型和箭头弧度的设置,需要用 dict 形式传入。</p></li></ul><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t9"></a><a name="t9"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">使用子图</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">有时候我们需要将多张子图展示在一起,可以使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">subplot()</code><span class="Apple-converted-space">&nbsp;</span>实现。即在调用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plot()</code><span class="Apple-converted-space">&nbsp;</span>函数之前需要先调用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">subplot()</code><span class="Apple-converted-space">&nbsp;</span>函数。该函数的第一个参数代表子图的总行数,第二个参数代表子图的总列数,第三个参数代表活跃区域。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs cpp">ax1&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">1</span></span>)&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;(行,列,活跃区)</span><br>plt.plot(x,&nbsp;np.<span class="hljs-built_in">sin</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'r'</span></span>)<br><br>ax2&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;sharey=ax1)&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;与&nbsp;ax1&nbsp;共享y轴</span><br>plt.plot(x,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>&nbsp;*&nbsp;np.<span class="hljs-built_in">sin</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'g'</span></span>)<br><br>ax3&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">3</span></span>)<br>plt.plot(x,&nbsp;np.<span class="hljs-built_in">cos</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'b'</span></span>)<br><br>ax4&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">4</span></span>,&nbsp;sharey=ax3)&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;与&nbsp;ax3&nbsp;共享y轴</span><br>plt.plot(x,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>&nbsp;*&nbsp;np.<span class="hljs-built_in">cos</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'y'</span></span>)<br><br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acdDibxd9A9MwMdEhbc6ajk1FdG6pEHGTqTxtcWV6SfRSncUKbXPHdt4w/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">上面的<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">subplot(2, 2, x)</code><span class="Apple-converted-space">&nbsp;</span>表示将图像窗口分为 2 行 2 列。x 表示当前子图所在的活跃区。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">可以看到,上面的每个子图的大小都是一样的。有时候我们需要不同大小的子图。比如将上面第一张子图完全放置在第一行,其他的子图都放在第二行。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs cpp">ax1&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">1</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">1</span></span>)&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;(行,列,活跃区)</span><br>plt.plot(x,&nbsp;np.<span class="hljs-built_in">sin</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'r'</span></span>)<br><br>ax2&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">3</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">4</span></span>)<br>plt.plot(x,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>&nbsp;*&nbsp;np.<span class="hljs-built_in">sin</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'g'</span></span>)<br><br>ax3&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">3</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">5</span></span>,&nbsp;sharey=ax2)<br>plt.plot(x,&nbsp;np.<span class="hljs-built_in">cos</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'b'</span></span>)<br><br>ax4&nbsp;=&nbsp;plt.subplot(<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">3</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">6</span></span>,&nbsp;sharey=ax2)<br>plt.plot(x,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">2</span></span>&nbsp;*&nbsp;np.<span class="hljs-built_in">cos</span>(x),&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'y'</span></span>)<br><br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acahpa4SsfMJ5TdJCbVTvELDEpwDh76Zq4yia5zSxLx1ic0ian5qiatFeOrw/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">简单解释下,<code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.subplot(2, 1, 1)</code><span class="Apple-converted-space">&nbsp;</span>将图像窗口分为了 2 行 1 列, 当前活跃区为 1。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.subplot(2, 3, 4)</code><span class="Apple-converted-space">&nbsp;</span>将整个图像窗口分为 2 行 3 列, 当前活跃区为 4。</p><p style="font-size:inherit;color:inherit;line-height:inherit;">解释下为什么活跃区为 4,因为上一步中使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.subplot(2, 1, 1)</code><span class="Apple-converted-space">&nbsp;</span>将整个图像窗口分为 2 行 1 列, 第1个小图占用了第1个位置, 也就是整个第1行. 这一步中使用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.subplot(2, 3, 4)</code><span class="Apple-converted-space">&nbsp;</span>将整个图像窗口分为 2 行 3 列, 于是整个图像窗口的第1行就变成了3列, 也就是成了3个位置, 于是第2行的第1个位置是整个图像窗口的第4个位置。</p><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t10"></a><a name="t10"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">常见的图形</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">这里带大家画一些常见的示例图。</p><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t11"></a><a name="t11"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">散点图</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">首先来看下如何绘制散点图。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs python">k&nbsp;=&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">500</span></span><br>x&nbsp;=&nbsp;np.random.rand(k)&nbsp;<br>y&nbsp;=&nbsp;np.random.rand(k)<br>size&nbsp;=&nbsp;np.random.rand(k)&nbsp;*&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">50</span></span>&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#&nbsp;生成每个点的大小</span></span><br><span class="hljs-comment">colour&nbsp;=&nbsp;np.arctan2(x,&nbsp;y)&nbsp;</span><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#&nbsp;生成每个点的颜色大小</span></span><br><span class="hljs-comment">plt.scatter(x,&nbsp;y,&nbsp;s=size,&nbsp;c=colour)</span><br><span class="hljs-comment">plt.colorbar()&nbsp;</span><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#&nbsp;添加颜色栏</span></span><br><br><span class="hljs-comment">plt.show()</span></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acvZs5Mfib4J8RqAO0o6BZPj58DfbTCnOTo10qEaOb3zdJ4oI0YLKy0ug/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">上面我们首先生成了要绘制的数据的点<code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">x</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">y</code>,接下来为每个数据点生成控制大小的数组<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">size</code>,然后未每个数据点生成控制颜色的数组<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">colour</code>。最后通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">colorbar()</code><span class="Apple-converted-space">&nbsp;</span>来增加一个颜色栏。</p><h3 style="font-weight:bold;font-size:1.3em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(236,68,68);"><a name="t12"></a><a name="t12"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(236,68,68);color:rgb(255,255,255);">柱状图</span><span style="vertical-align:bottom;border-bottom:36px solid rgb(239,235,233);border-right:20px solid transparent;"></span></h3><p style="font-size:inherit;color:inherit;line-height:inherit;">柱状图我们经常会用到,我们来看下如何画出柱状图,并在图上标注出数据对应的数值。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs delphi">k&nbsp;=&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">10</span></span><br>x&nbsp;=&nbsp;np.arange(k)<br>y&nbsp;=&nbsp;np.random.rand(k)<br>plt.bar(x,&nbsp;y)&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;画出&nbsp;x&nbsp;和&nbsp;y&nbsp;的柱状图</span><br><br><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);">#&nbsp;增加数值</span><br><span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-keyword">for</span></span>&nbsp;x,&nbsp;y&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-keyword">in</span></span>&nbsp;zip(x,&nbsp;y):<br>&nbsp;&nbsp;&nbsp;&nbsp;plt.text(x,&nbsp;y&nbsp;,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'%.2f'</span></span>&nbsp;%&nbsp;y,&nbsp;ha=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'center'</span></span>,&nbsp;va=<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'bottom'</span></span>)<br><br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acU6ImxiagcH1q1lF82ibvg6h74yicYzPr5XgaGIiabcTgFibMqQdWNtfYoRA/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">生成数据<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">x</code><span class="Apple-converted-space">&nbsp;</span>和<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">y</code><span class="Apple-converted-space">&nbsp;</span>之后,调用<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.bar</code><span class="Apple-converted-space">&nbsp;</span>函数绘制出柱状图,然后通过<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">plt.text</code><span class="Apple-converted-space">&nbsp;</span>标注数值,设置参数<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">ha='center'</code><span class="Apple-converted-space">&nbsp;</span>横向居中对齐,设置<span class="Apple-converted-space">&nbsp;</span><code style="margin-left:2px;font-size:inherit;line-height:inherit;color:rgb(233,105,0);background-color:rgb(248,248,248);">va='bottom'</code>纵向底部(顶部)对齐。</p><h2 style="font-weight:bold;font-size:1.4em;color:inherit;line-height:inherit;border-bottom:2px solid rgb(0,172,193);"><a name="t13"></a><a name="t13"></a><span style="font-size:inherit;line-height:inherit;font-weight:normal;background-color:rgb(0,172,193);color:rgb(255,255,255);">中文乱码解决</span></h2><p style="font-size:inherit;color:inherit;line-height:inherit;">Matplotlib 有个让人恼火的问题是,默认情况下,Matplotlib 中文会乱码。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs java">x&nbsp;=&nbsp;[<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'北京'</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'上海'</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'深圳'</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'广州'</span></span>]<br>y&nbsp;=&nbsp;[<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">60000</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">58000</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">50000</span></span>,&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(174,135,250);"><span class="hljs-number">52000</span></span>]<br>plt.plot(x,&nbsp;y)<br>plt.show()</code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acQyXsM3noV3kt8h3OxeOBDjK1amvT5Tf8840FH9NJCILpAMTiaqpibnTg/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><p style="font-size:inherit;color:inherit;line-height:inherit;">可以看到,上面所有的中文都乱码了,显示成方框了,如何解决呢?</p><p style="font-size:inherit;color:inherit;line-height:inherit;">其实只需要配置下后台字体即可。</p><pre style="font-size:inherit;color:inherit;line-height:inherit;"><code style="margin-left:2px;line-height:18px;font-size:14px;letter-spacing:0px;font-family:Consolas, Inconsolata, Courier, monospace;border-width:1px 1px 1px 6px;border-style:solid;border-color:rgb(204,204,204) rgb(204,204,204) rgb(204,204,204) rgb(33,152,99);color:rgb(169,183,198);background-color:rgb(40,43,46);" class="hljs python">plt.rcParams[<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'font.sans-serif'</span></span>]=[<span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-string">'SimHei'</span></span>]&nbsp;<span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#用来正常显示中文标签</span></span><br><span class="hljs-comment">plt.rcParams[</span><span style="font-size:inherit;line-height:inherit;color:rgb(238,220,112);"><span class="hljs-comment">'axes.unicode_minus'</span></span><span class="hljs-comment">]=</span><span style="font-size:inherit;line-height:inherit;color:rgb(248,35,117);"><span class="hljs-comment">False</span></span><span class="hljs-comment">&nbsp;</span><span style="font-size:inherit;line-height:inherit;color:rgb(128,128,128);"><span class="hljs-comment">#用来正常显示负号</span></span><br><br><span class="hljs-comment">plt.plot(x,&nbsp;y)</span><br><span class="hljs-comment">plt.show()</span></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre><figure style="font-size:inherit;color:inherit;line-height:inherit;"><figcaption style="line-height:inherit;text-align:center;color:rgb(153,153,153);font-size:.7em;"><br></figcaption><p style="text-align:center;"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_png/FlgksGN9DLxWeVOcPwib7ALkB6pF1z0acxOrUXvGeeJdMSsUnQRn3dKGIS8F4Rrwr2kBTdysiaic79nl40icyyOwbg/640?wx_fmt=png" alt="640?wx_fmt=png"></p></figure><br><br><img class="__bg_gif" style="vertical-align:top;" src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_gif/FlgksGN9DLzsibvNBPpQZThAK3QxPgUoHVqvbCJiaLnNzy73Hv3Cat7tr5HyD0dpKgvDLUNSUCibwia1bibZRyicyWcw/640?wx_fmt=gif" alt="640?wx_fmt=gif"><br><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><em style="color:rgb(62,62,62);text-align:justify;font-family:'Optima-Regular', 'PingFangTC-light';font-size:13px;"><span style="text-align:right;color:rgb(178,178,178);">「若你有原创文章想与大家分享,欢迎投稿。」</span></em><br></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(178,178,178);font-family:'CourierNewPS-ItalicMT';font-size:12px;letter-spacing:.544px;">加编辑微信ID,备注#投稿#:</span><span style="font-size:15px;font-family:'Optima-Regular', 'PingFangTC-light';"></span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(178,178,178);font-size:12px;font-family:'CourierNewPS-ItalicMT';">程序 丨 druidlost&nbsp;&nbsp;<br></span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(178,178,178);font-size:12px;font-family:'CourierNewPS-ItalicMT';">小七 丨 duoshangshuang</span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;"><span style="color:rgb(178,178,178);font-size:12px;font-family:'CourierNewPS-ItalicMT';"><br></span></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;background-color:rgb(255,255,255);text-align:center;">上期精彩内容</p><p style="text-align:center;"><a href="http://mp.weixin.qq.com/s?__biz=MzA5MzY4NTQwMA==&amp;mid=2651006268&amp;idx=1&amp;sn=9c09afac9a6e2e4eb7cc6575e049f19b&amp;chksm=8bad96cbbcda1fdd7611ce2d89724cf9bfab7ba9b909cb8eb8c5a0fe4c420e5dd6c78ada5ab1&amp;scene=21#wechat_redirect" rel="nofollow"><span class="js_jump_icon h5_image_link"><img src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/1hReHaqafafXl47uTiadEB3sBbH5mv5FMezcuNPNUFakDZw5zgVKVRRcslLbA7s2wCReZPMbsnhPMkzTzNCuSFA/640?wx_fmt=jpeg" alt="640?wx_fmt=jpeg"></span></a></p><p style="color:rgb(51,51,51);font-family:'-apple-system-font', 'system-ui', 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size:17px;letter-spacing:.544px;text-align:justify;background-color:rgb(255,255,255);"><img class="__bg_gif" style="letter-spacing:.544px;" src="https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_gif/1hReHaqafaerOd463prtf1AbbmqA1xJK50Rct0FxeaKJ2zv0qsYOPayG6g2YKnzLQ5gmI1nt9PUPib6qKOB5g8Q/640?wx_fmt=gif" alt="640?wx_fmt=gif"></p>
            </div>
发布了258 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/kyle1314608/article/details/104499456