概述
<div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article">matplotlib基础1:绘图基本属性设置 -- xticks(loc,labels)格式化转义标记</h1>
</div>
<div class="article-info-box">
<div class="article-bar-top">
<!--文章类型-->
<!--原创-->
<img class="article-type-img" src="https://csdnimg.cn/release/phoenix/template/new_img/original.png" alt="">
<!--翻译-->
<!--转载-->
<div class="bar-content">
<a class="follow-nickName" href="https://me.csdn.net/weixin_40040404" target="_blank" rel="noopener">煲饭酱</a>
<span class="time">2018-07-24 16:32:17</span>
<img class="article-read-img article-heard-img" src="https://csdnimg.cn/release/phoenix/template/new_img/articleReadEyes.png" alt="">
<span class="read-count">43567</span>
<a id="blog_detail_zk_collection" class="un-collection" data-report-click="{"mod":"popu_823","ab":"new"}">
<img class="article-collect-img article-heard-img un-collect-status" style="display:inline-block" src="https://csdnimg.cn/release/phoenix/template/new_img/tobarCollect.png" alt="">
<img class="article-collect-img article-heard-img collect-status" style="display:none" src="https://csdnimg.cn/release/phoenix/template/new_img/tobarCollectionActive.png" alt="">
<span class="name">收藏</span>
<span class="get-collection">
39 </span>
</a>
</div>
</div>
<div class="blog-tags-box">
<div class="tags-box artic-tag-box">
<span class="label">分类专栏:</span>
<a class="tag-link" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_40040404/category_7761993.html">
matplotlib </a>
</div>
</div>
<div class="up-time" style="left: 89.875px; display: none;"><span>最后发布:2018-07-24 16:32:17</span><span>首发:2018-07-24 16:32:17</span></div>
<div class="slide-content-box">
<div class="all-tags-box">
</div>
<div class="article-copyright">
<div class="creativecommons">
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"></a>
</div>
<div class="creativecommons">
版权声明:本文为博主原创文章,遵循<a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="noopener"> CC 4.0 BY-SA </a>版权协议,转载请附上原文出处链接和本声明。 </div>
<div class="article-source-link">
本文链接:<a href="https://blog.csdn.net/weixin_40040404/article/details/81185564">https://blog.csdn.net/weixin_40040404/article/details/81185564</a>
</div>
</div>
</div>
<div class="operating">
<a class="href-article-edit slide-toggle">版权</a>
</div>
</div>
</div>
</div>
<!--python安装手册结束-->
<article class="baidu_pl">
<div id="article_content" class="article_content clearfix">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-211130ba7a.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p>matplotlib绘图基本属性设置:</p>
<pre class="prettyprint" name="code"><code class="hljs python has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-string">"""
Created on Tue Jul 24 15:11:36 2018
@author: Administrator
"""</span>
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
x = np.linspace(-np.pi, np.pi, <span class="hljs-number">1000</span>) <span class="hljs-comment"># 均匀分布的1000个数 endpoint=False</span>
cos_y = np.cos(x) / <span class="hljs-number">2</span>
sin_y = np.sin(x)
<span class="hljs-comment"># 初始值(x0,y0)</span>
x0 = np.pi * <span class="hljs-number">3</span> / <span class="hljs-number">4</span>
x1 = -np.pi * <span class="hljs-number">4</span> / <span class="hljs-number">5</span>
y0_cos = np.cos(x0) / <span class="hljs-number">2</span>
y1_sin = np.sin(x1)
<span class="hljs-comment"># text内容</span>
text_cont = <span class="hljs-string">'This is a test!'</span>
<span class="hljs-comment"># 设置图形对象 :窗口</span>
plt.figure(<span class="hljs-string">'Figure Object 1'</span>, <span class="hljs-comment"># 图形对象名称 窗口左上角显示</span>
figsize = (<span class="hljs-number">8</span>, <span class="hljs-number">6</span>), <span class="hljs-comment"># 窗口大小</span>
dpi = <span class="hljs-number">120</span>, <span class="hljs-comment"># 分辨率</span>
facecolor = <span class="hljs-string">'lightgray'</span>, <span class="hljs-comment"># 背景色</span>
)
<span class="hljs-comment"># 设置坐标轴边界 xlim ylim</span>
plt.xlim(x.min() * <span class="hljs-number">1.1</span>, x.max() * <span class="hljs-number">1.1</span>)
plt.ylim(sin_y.min() * <span class="hljs-number">1.1</span>, sin_y.max() * <span class="hljs-number">1.1</span>)
<span class="hljs-comment"># 设置title</span>
plt.title(<span class="hljs-string">'Function Curve'</span>, fontsize=<span class="hljs-number">14</span>)
<span class="hljs-comment"># ************************ 坐标轴刻度:start *****************************</span>
<span class="hljs-comment"># 方法1:设置刻度值即坐标轴刻度 xticks yticks</span>
plt.xticks([-np.pi, -np.pi / <span class="hljs-number">2</span>, <span class="hljs-number">0</span>, np.pi / <span class="hljs-number">2</span>, np.pi * <span class="hljs-number">3</span> / <span class="hljs-number">4</span>,
np.pi],
[<span class="hljs-string">r'$-pi$'</span>, <span class="hljs-string">r'$-frac{pi}{2}$'</span>, <span class="hljs-string">r'$0$'</span>,
<span class="hljs-string">r'$frac{pi}{2}$'</span>, <span class="hljs-string">r'$frac{3pi}{4}$'</span>,
<span class="hljs-string">r'$pi$'</span>])
<span class="hljs-comment"># 格式化转义 字符串首尾 r'$...$' (matplotlib中)</span>
<span class="hljs-comment"># xticks(loc, labels): labels 格式化转义方法 r'$-frac{pi}{2}$' 留意分数的表示方式</span>
<span class="hljs-comment"># pi需要转义才能显示为字符 pai. 若$-pi$ 则直接显示-pi</span>
<span class="hljs-comment"># 如果没有第二个[]参数,刻度值显示如-3.142, -1.571...等浮点数,而不是-pi</span>
plt.yticks([-<span class="hljs-number">1</span>, -<span class="hljs-number">0.5</span>, <span class="hljs-number">0.5</span>, <span class="hljs-number">1</span>]) <span class="hljs-comment"># == ax.set_yticks([-1, -0.5, 0.5, 1])</span>
<span class="hljs-comment"># 方法2:设置坐标轴刻度</span>
<span class="hljs-comment"># ax.set_yticks([-1, -0.5, 0.5, 1] # 等价于 plt.yticks([-1, -0.5, 0.5, 1])</span>
<span class="hljs-comment"># ax.set_xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi * 3 / 4,</span>
<span class="hljs-comment"># np.pi],[r'$-pi$', r'$-frac{pi}{2}$',r'$0$',</span>
<span class="hljs-comment"># r'$frac{pi}{2}$', r'$frac{3pi}{4}$',#r'$pi$']) # 显示 -3,-2,-1</span>
<span class="hljs-comment"># 获取当前坐标轴 </span>
ax = plt.gca()
<span class="hljs-comment"># 方法3:坐标刻度定位器 axis :ax.x[y]axis.set_major[minor]_locator(locator)</span>
<span class="hljs-comment"># 与(plt.xticks , plt.yticks)和 (ax.set_xticks和ax.set_yticks)一样功能</span>
<span class="hljs-comment"># ax.yaxis.set_major_locator(plt.MaxNLocator(nbins=5, steps=[1, 3, 5, 7, 9])) # 面元划分 4段数据</span>
<span class="hljs-comment"># ax.yaxis.set_minor_locator(plt.MultipleLocator(0.5)) # 次刻度间隔</span>
<span class="hljs-string">'''locator类型:
'plt.NullLocator()',
'plt.MaxNLocator(nbins=5, steps=[1, 3, 5, 7, 9])', # nbin=5:面元边界个数即4个buckt steps不知道啥意思
'plt.FixedLocator(locs=[0, 2.5, 5, 7.5, 10])', # 直接指定刻度值位置
'plt.AutoLocator()', # 自动分配刻度值位置
'plt.IndexLocator(offset=0.5, base=1.5)', # 面元宽度(间隔)1.5,从0.5开始
'plt.MultipleLocator()', # 可以自由自定刻度间隔
'plt.LinearLocator(numticks=21)', # 线性划分20等分,21个刻度
'plt.LogLocator(base=2, subs=[1.0])'] # 对数定位器
'''</span>
<span class="hljs-comment"># 设置坐标轴的刻度参数</span>
plt.tick_params(labelsize=<span class="hljs-number">10</span>) <span class="hljs-comment"># NOTE: 不是fontsize, 是labelsize</span>
<span class="hljs-comment"># ************************ 坐标轴刻度:end *****************************</span>
<span class="hljs-comment"># 移动坐标轴:设置零点在坐标轴的位置 ,set_position(tuple) 元素为tuple</span>
ax.spines[<span class="hljs-string">'left'</span>].set_position((<span class="hljs-string">'data'</span>,<span class="hljs-number">0</span>)) <span class="hljs-comment"># data 表示position是相对于数据坐标系</span>
ax.yaxis.set_ticks_position(<span class="hljs-string">'left'</span>) <span class="hljs-comment"># y轴'right': 刻度值显示在right坐标轴上</span>
ax.spines[<span class="hljs-string">'bottom'</span>].set_position((<span class="hljs-string">'data'</span>,<span class="hljs-number">0</span>))
ax.xaxis.set_ticks_position(<span class="hljs-string">'bottom'</span>) <span class="hljs-comment"># x轴top:ticks值显示在top坐标轴上</span>
<span class="hljs-comment"># 隐藏top和right边框</span>
ax.spines[<span class="hljs-string">'right'</span>].set_color(<span class="hljs-string">'none'</span>)
ax.spines[<span class="hljs-string">'top'</span>].set_color(<span class="hljs-string">'none'</span>)
plt.plot(x, cos_y,
linestyle=<span class="hljs-string">'-'</span>,
linewidth=<span class="hljs-number">2</span>,
color=<span class="hljs-string">'dodgerblue'</span>,
label=<span class="hljs-string">r'$y=frac{1}{2}$cos(x)'</span>) <span class="hljs-comment"># r'$..$'外部不需要引号</span>
plt.plot(x, sin_y,
linestyle=<span class="hljs-string">'-'</span>, <span class="hljs-comment"># 线型</span>
linewidth=<span class="hljs-number">2</span>, <span class="hljs-comment"># 线宽</span>
color=<span class="hljs-string">'orangered'</span>,
label=<span class="hljs-string">r'$y=sin(x)$'</span>) <span class="hljs-comment"># label:legend显示的内容</span>
<span class="hljs-comment"># 设置图例legend, 可以直接设置在plt.plot的label属性中,然后plt.lengend()显示</span>
<span class="hljs-comment"># loc顺序: “先上中下,再左中右” 默认上左, shadow default: False</span>
plt.legend(loc=<span class="hljs-string">'upper left'</span>,shadow=<span class="hljs-keyword">False</span>, fontsize=<span class="hljs-number">12</span>)
<span class="hljs-comment"># 网格 grid参数</span>
plt.grid(color = <span class="hljs-string">'y'</span>, linestyle=<span class="hljs-string">':'</span>, linewidth=<span class="hljs-number">1</span>)
<span class="hljs-comment"># scatter</span>
plt.scatter([x0, x1],[y0_cos, y1_sin], <span class="hljs-comment"># 两个点的坐标</span>
s = <span class="hljs-number">60</span>, <span class="hljs-comment"># fontsize</span>
edgecolor=<span class="hljs-string">'limegreen'</span>, <span class="hljs-comment"># 散点边缘色</span>
facecolor=<span class="hljs-string">'purple'</span>, <span class="hljs-comment"># 散点填充色</span>
zorder=<span class="hljs-number">3</span> <span class="hljs-comment"># Z序 ,图层顺序</span>
)
<span class="hljs-comment"># 两点间的线段</span>
plt.plot([x0, x1],[y0_cos, y1_sin],
linestyle=<span class="hljs-string">'--'</span>,
color=<span class="hljs-string">'limegreen'</span>,
alpha=<span class="hljs-number">0.4</span>, <span class="hljs-comment"># 透明度 0~1</span>
marker=<span class="hljs-string">'o'</span>, <span class="hljs-comment"># 点样式</span>
markersize=<span class="hljs-number">6</span>
)
<span class="hljs-comment"># 对点(x0,y0)备注文本</span>
plt.annotate(
<span class="hljs-string">r'$frac{1}{2}cos(frac{3pi}{4}) = -frac{sqrt{2}}{4}$'</span>, <span class="hljs-comment"># 备注文本</span>
xy = (x0, y0_cos), <span class="hljs-comment"># 目标位置</span>
xycoords = <span class="hljs-string">'data'</span>, <span class="hljs-comment"># 目标坐标系:相对于数据坐标系</span>
xytext = (-<span class="hljs-number">70</span>, -<span class="hljs-number">35</span>), <span class="hljs-comment"># 文本位置,偏移量,textcoords = 'offset points' 相对于目标点</span>
textcoords = <span class="hljs-string">'offset points'</span>, <span class="hljs-comment"># 相对于目标点为原点的坐标系偏移</span>
fontsize = <span class="hljs-number">14</span>,
arrowprops = dict(arrowstyle=<span class="hljs-string">'->'</span>, <span class="hljs-comment"># 箭头样式, '-|>' '->'</span>
connectionstyle=<span class="hljs-string">'arc3, rad=.2'</span>) <span class="hljs-comment"># 箭头属性</span>
)
<span class="hljs-comment"># plt.text: Add text to the axes.</span>
<span class="hljs-comment"># Add the text s to the axes at location x, y in data coordinates</span>
plt.text(-<span class="hljs-number">2</span>,
<span class="hljs-number">0.5</span>,
<span class="hljs-string">'Content:%s'</span> % text_cont, <span class="hljs-comment"># 文本内容 , 可格式化方式</span>
fontsize = <span class="hljs-number">10</span>,
ha=<span class="hljs-string">'center'</span>,
va=<span class="hljs-string">'center'</span>,
alpha=<span class="hljs-number">0.5</span>,
color = <span class="hljs-string">'r'</span>
)
<span class="hljs-comment"># 显示图像</span>
plt.show()
<span class="hljs-string">'''
plot(*args, **kwargs)
Plot y versus x as lines and/or markers.
Call signatures:
plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
'''</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li><li style="color: rgb(153, 153, 153);">67</li><li style="color: rgb(153, 153, 153);">68</li><li style="color: rgb(153, 153, 153);">69</li><li style="color: rgb(153, 153, 153);">70</li><li style="color: rgb(153, 153, 153);">71</li><li style="color: rgb(153, 153, 153);">72</li><li style="color: rgb(153, 153, 153);">73</li><li style="color: rgb(153, 153, 153);">74</li><li style="color: rgb(153, 153, 153);">75</li><li style="color: rgb(153, 153, 153);">76</li><li style="color: rgb(153, 153, 153);">77</li><li style="color: rgb(153, 153, 153);">78</li><li style="color: rgb(153, 153, 153);">79</li><li style="color: rgb(153, 153, 153);">80</li><li style="color: rgb(153, 153, 153);">81</li><li style="color: rgb(153, 153, 153);">82</li><li style="color: rgb(153, 153, 153);">83</li><li style="color: rgb(153, 153, 153);">84</li><li style="color: rgb(153, 153, 153);">85</li><li style="color: rgb(153, 153, 153);">86</li><li style="color: rgb(153, 153, 153);">87</li><li style="color: rgb(153, 153, 153);">88</li><li style="color: rgb(153, 153, 153);">89</li><li style="color: rgb(153, 153, 153);">90</li><li style="color: rgb(153, 153, 153);">91</li><li style="color: rgb(153, 153, 153);">92</li><li style="color: rgb(153, 153, 153);">93</li><li style="color: rgb(153, 153, 153);">94</li><li style="color: rgb(153, 153, 153);">95</li><li style="color: rgb(153, 153, 153);">96</li><li style="color: rgb(153, 153, 153);">97</li><li style="color: rgb(153, 153, 153);">98</li><li style="color: rgb(153, 153, 153);">99</li><li style="color: rgb(153, 153, 153);">100</li><li style="color: rgb(153, 153, 153);">101</li><li style="color: rgb(153, 153, 153);">102</li><li style="color: rgb(153, 153, 153);">103</li><li style="color: rgb(153, 153, 153);">104</li><li style="color: rgb(153, 153, 153);">105</li><li style="color: rgb(153, 153, 153);">106</li><li style="color: rgb(153, 153, 153);">107</li><li style="color: rgb(153, 153, 153);">108</li><li style="color: rgb(153, 153, 153);">109</li><li style="color: rgb(153, 153, 153);">110</li><li style="color: rgb(153, 153, 153);">111</li><li style="color: rgb(153, 153, 153);">112</li><li style="color: rgb(153, 153, 153);">113</li><li style="color: rgb(153, 153, 153);">114</li><li style="color: rgb(153, 153, 153);">115</li><li style="color: rgb(153, 153, 153);">116</li><li style="color: rgb(153, 153, 153);">117</li><li style="color: rgb(153, 153, 153);">118</li><li style="color: rgb(153, 153, 153);">119</li><li style="color: rgb(153, 153, 153);">120</li><li style="color: rgb(153, 153, 153);">121</li><li style="color: rgb(153, 153, 153);">122</li><li style="color: rgb(153, 153, 153);">123</li><li style="color: rgb(153, 153, 153);">124</li><li style="color: rgb(153, 153, 153);">125</li><li style="color: rgb(153, 153, 153);">126</li><li style="color: rgb(153, 153, 153);">127</li><li style="color: rgb(153, 153, 153);">128</li><li style="color: rgb(153, 153, 153);">129</li><li style="color: rgb(153, 153, 153);">130</li><li style="color: rgb(153, 153, 153);">131</li><li style="color: rgb(153, 153, 153);">132</li><li style="color: rgb(153, 153, 153);">133</li><li style="color: rgb(153, 153, 153);">134</li><li style="color: rgb(153, 153, 153);">135</li><li style="color: rgb(153, 153, 153);">136</li><li style="color: rgb(153, 153, 153);">137</li><li style="color: rgb(153, 153, 153);">138</li><li style="color: rgb(153, 153, 153);">139</li><li style="color: rgb(153, 153, 153);">140</li><li style="color: rgb(153, 153, 153);">141</li><li style="color: rgb(153, 153, 153);">142</li><li style="color: rgb(153, 153, 153);">143</li><li style="color: rgb(153, 153, 153);">144</li><li style="color: rgb(153, 153, 153);">145</li><li style="color: rgb(153, 153, 153);">146</li><li style="color: rgb(153, 153, 153);">147</li><li style="color: rgb(153, 153, 153);">148</li><li style="color: rgb(153, 153, 153);">149</li><li style="color: rgb(153, 153, 153);">150</li><li style="color: rgb(153, 153, 153);">151</li><li style="color: rgb(153, 153, 153);">152</li><li style="color: rgb(153, 153, 153);">153</li><li style="color: rgb(153, 153, 153);">154</li><li style="color: rgb(153, 153, 153);">155</li><li style="color: rgb(153, 153, 153);">156</li><li style="color: rgb(153, 153, 153);">157</li><li style="color: rgb(153, 153, 153);">158</li><li style="color: rgb(153, 153, 153);">159</li><li style="color: rgb(153, 153, 153);">160</li><li style="color: rgb(153, 153, 153);">161</li></ul></pre>
<p><img src="https://img-blog.csdn.net/2018072520302276?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDA0MDQwNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述" title=""></p>
<p>NOTE: 注意用法和格式: <br>
1、格式化转义书写格式:</p>
<pre class="prettyprint" name="code"><code class="hljs python has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">mp.xticks([-np.pi, -np.pi / <span class="hljs-number">2</span>, <span class="hljs-number">0</span>, np.pi / <span class="hljs-number">2</span>, np.pi * <span class="hljs-number">3</span> / <span class="hljs-number">4</span>,
np.pi],
[<span class="hljs-string">r'$-pi$'</span>, <span class="hljs-string">r'$-frac{pi}{2}$'</span>, <span class="hljs-string">r'$0$'</span>,
<span class="hljs-string">r'$frac{pi}{2}$'</span>, <span class="hljs-string">r'$frac{3pi}{4}$'</span>,
<span class="hljs-string">r'$pi$'</span>])
<span class="hljs-comment"># 格式化转义 字符串首尾 r'$...$') (matplotlib中)</span>
<span class="hljs-comment"># xticks(loc, labels): labels 格式化转义方法 r'$-frac{pi}{2}$'</span>
<span class="hljs-comment"># pi需要转义才能显示为字符 pai. 若$-pi$ 则直接显示-pi</span>
<span class="hljs-comment"># 如果没有第二个[]参数,刻度值显示如-3.142, -1.571...等浮点数,而不是-pi</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>2、刻度定位器与格式(Tick Locator) <br>
参考链接:<a href="https://blog.csdn.net/helunqu2017/article/details/78736661">matplotlib命令与格式:tick坐标轴主副刻度设置</a></p>
<p><strong>定义主刻度变量</strong></p>
<pre class="prettyprint" name="code"><code class="hljs bash has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">xmajorLocator = MultipleLocator(<span class="hljs-number">20</span>) <span class="hljs-comment">#将x主刻度标签设置为20的倍数</span>
xmajorFormatter = FormatStrFormatter(<span class="hljs-string">'%5.1f'</span>) <span class="hljs-comment">#设置x轴标签文本的格式</span>
ymajorLocator = MultipleLocator(<span class="hljs-number">0.5</span>) <span class="hljs-comment">#将y轴主刻度标签设置为0.5的倍数</span>
ymajorFormatter = FormatStrFormatter(<span class="hljs-string">'%1.1f'</span>) <span class="hljs-comment">#设置y轴标签文本的格式</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p><strong>设置主刻度标签的位置,标签文本的格式</strong></p>
<pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ax<span class="hljs-preprocessor">.xaxis</span><span class="hljs-preprocessor">.set</span>_major_locator(xmajorLocator)
ax<span class="hljs-preprocessor">.xaxis</span><span class="hljs-preprocessor">.set</span>_major_formatter(xmajorFormatter)
ax<span class="hljs-preprocessor">.yaxis</span><span class="hljs-preprocessor">.set</span>_major_locator(ymajorLocator)
ax<span class="hljs-preprocessor">.yaxis</span><span class="hljs-preprocessor">.set</span>_major_formatter(ymajorFormatter)<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre>
<p><strong>修改次刻度</strong></p>
<pre class="prettyprint" name="code"><code class="hljs bash has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">xminorLocator = MultipleLocator(<span class="hljs-number">5</span>) <span class="hljs-comment">#将x轴次刻度标签设置为5的倍数</span>
yminorLocator = MultipleLocator(<span class="hljs-number">0.1</span>) <span class="hljs-comment">#将此y轴次刻度标签设置为0.1的倍数</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<p><strong>设置次刻度标签的位置,没有标签文本格式</strong></p>
<pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ax<span class="hljs-preprocessor">.xaxis</span><span class="hljs-preprocessor">.set</span>_minor_locator(xminorLocator)
ax<span class="hljs-preprocessor">.yaxis</span><span class="hljs-preprocessor">.set</span>_minor_locator(yminorLocator)<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<p><strong>删除坐标轴的刻度显示</strong></p>
<pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ax<span class="hljs-preprocessor">.yaxis</span><span class="hljs-preprocessor">.set</span>_major_locator(plt<span class="hljs-preprocessor">.NullLocator</span>())
ax<span class="hljs-preprocessor">.xaxis</span><span class="hljs-preprocessor">.set</span>_major_formatter(plt<span class="hljs-preprocessor">.NullFormatter</span>()) <div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li></ul></pre>
<p><strong>涉及日期格式的locator简述</strong></p>
<pre class="prettyprint" name="code"><code class="hljs bash has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;">ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday=md.MO))
ax.xaxis.set_minor_locator(md.DayLocator())
ax.xaxis.set_major_formatter(md.DateFormatter(<span class="hljs-string">'%d %b %Y'</span>))<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre>
<p>NOTE1: <br>
Python <code>only supports</code> :mod:<code>datetime</code> :func:<code>strftime formatting</code> for years greater than 1900</p>
<pre class="prettyprint" name="code"><code class="hljs python has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DateFormatter</span><span class="hljs-params">(ticker.Formatter)</span>:</span>
<span class="hljs-string">"""
Tick location is seconds since the epoch. Use a :func:`strftime`
format string.
Python only supports :mod:`datetime` :func:`strftime` formatting
for years greater than 1900. Thanks to Andrew Dalke, Dalke
Scientific Software who contributed the :func:`strftime` code
below to include dates earlier than this year.
"""</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>NOTE2: <br>
Elements of <em><code>byweekday</code></em> must be one of <code>MO</code>, <code>TU</code>, <code>WE</code>, <code>TH</code>, <code>FR</code>, <code>SA</code>, <br>
<code>SU</code>, the constants from:mod:<code>dateutil.rrule</code>, which have been imported into the :mod:<code>matplotlib.dates</code> namespace.</p>
<pre class="prettyprint" name="code"><code class="hljs python has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WeekdayLocator</span><span class="hljs-params">(RRuleLocator)</span>:</span>
<span class="hljs-string">"""
Make ticks on occurrences of each weekday.
"""</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, byweekday=<span class="hljs-number">1</span>, interval=<span class="hljs-number">1</span>, tz=None)</span>:</span>
<span class="hljs-string">"""
Mark every weekday in *byweekday*; *byweekday* can be a number or
sequence.
Elements of *byweekday* must be one of MO, TU, WE, TH, FR, SA,
SU, the constants from :mod:`dateutil.rrule`, which have been
imported into the :mod:`matplotlib.dates` namespace.
*interval* specifies the number of weeks to skip. For example,
``interval=2`` plots every second week.
"""</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li></ul></pre>
<p>NOTE3: <br>
<em>bymonthday</em> can be an <code>int</code> or <code>sequence</code>. Default <code>bymonthday=range(1,32)</code></p>
<pre class="prettyprint" name="code"><code class="hljs python has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DayLocator</span><span class="hljs-params">(RRuleLocator)</span>:</span>
<span class="hljs-string">"""
Make ticks on occurrences of each day of the month. For example,
1, 15, 30.
"""</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, bymonthday=None, interval=<span class="hljs-number">1</span>, tz=None)</span>:</span>
<span class="hljs-string">"""
Mark every day in *bymonthday*; *bymonthday* can be an int or
sequence.
Default is to tick every day of the month: ``bymonthday=range(1,32)``
"""</span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre>
<p><strong>locator属性类型:</strong></p>
<pre class="prettyprint" name="code"><code class="hljs vbnet has-numbering" οnclick="mdcp.copyCode(event)" style="position: unset;"><span class="hljs-comment"><span class="hljs-xmlDocTag">'''</span>locator类型:</span>
<span class="hljs-comment">'plt.NullLocator()',</span>
<span class="hljs-comment">'plt.MaxNLocator(nbins=5, steps=[1, 3, 5, 7, 9])', # nbin=5:面元边界个数即4个buckt steps不知道啥意思</span>
<span class="hljs-comment">'plt.FixedLocator(locs=[0, 2.5, 5, 7.5, 10])', # 直接指定刻度值位置 </span>
<span class="hljs-comment">'plt.AutoLocator()', # 自动分配刻度值位置</span>
<span class="hljs-comment">'plt.IndexLocator(offset=0.5, base=1.5)', # 面元宽度(间隔)1.5,从0.5开始</span>
<span class="hljs-comment">'plt.MultipleLocator()', # 可以自由自定刻度间隔</span>
<span class="hljs-comment">'plt.LinearLocator(numticks=21)', # 线性划分20等分,21个刻度</span>
<span class="hljs-comment">'plt.LogLocator(base=2, subs=[1.0])'] # 对数定位器</span>
<span class="hljs-comment"><span class="hljs-xmlDocTag">'''</span></span><div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre> </div><div><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
</div>
</article>
</div>
最后
以上就是文艺季节为你收集整理的matplotlib基础1:绘图基本属性设置 -- xticks(loc,labels)格式化转义标记的全部内容,希望文章能够帮你解决matplotlib基础1:绘图基本属性设置 -- xticks(loc,labels)格式化转义标记所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复