- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章学习python中matplotlib绘图设置坐标轴刻度、文本由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
总结matplotlib绘图如何设置坐标轴刻度大小和刻度.
上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
from
pylab
import
*
from
matplotlib.ticker
import
MultipleLocator, FormatStrFormatter
xmajorLocator
=
MultipleLocator(
20
)
#将x主刻度标签设置为20的倍数
xmajorFormatter
=
FormatStrFormatter(
'%1.1f'
)
#设置x轴标签文本的格式
xminorLocator
=
MultipleLocator(
5
)
#将x轴次刻度标签设置为5的倍数
ymajorLocator
=
MultipleLocator(
0.5
)
#将y轴主刻度标签设置为0.5的倍数
ymajorFormatter
=
FormatStrFormatter(
'%1.1f'
)
#设置y轴标签文本的格式
yminorLocator
=
MultipleLocator(
0.1
)
#将此y轴次刻度标签设置为0.1的倍数
t
=
arange(
0.0
,
100.0
,
1
)
s
=
sin(
0.1
*
pi
*
t)
*
exp(
-
t
*
0.01
)
ax
=
subplot(
111
)
#注意:一般都在ax中设置,不再plot中设置
plot(t,s,
'--b*'
)
#设置主刻度标签的位置,标签文本的格式
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)
ax.yaxis.set_major_locator(ymajorLocator)
ax.yaxis.set_major_formatter(ymajorFormatter)
#显示次刻度标签的位置,没有标签文本
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_minor_locator(yminorLocator)
ax.xaxis.grid(
True
, which
=
'major'
)
#x坐标轴的网格使用主刻度
ax.yaxis.grid(
True
, which
=
'minor'
)
#y坐标轴的网格使用次刻度
show()
|
绘图如下:
如果仔细看代码,可以得知,设置坐标轴刻度和文本主要使用了"MultipleLocator"、"FormatStrFormatter"方法.
这两个方法来自matplotlib安装库里面ticker.py文件;"MultipleLocator(Locator)"表示将刻度标签设置为Locator的倍数,"FormatStrFormatter"表示设置标签文本的格式,代码中"%1.1f"表示保留小数点后一位,浮点数显示.
相应的方法还有:
除了以上方法,还有另外一种方法,那就是使用xticks方法(yticks,x,y表示对应坐标轴),xticks用法可在python cmd下输入以下代码查看
1
2
|
import
matplotlib.pyplot as plt
help
(plt.xticks)
|
代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
import
numpy as np
import
matplotlib.pyplot as plt
fig,ax
=
plt.subplots()
x
=
[
1
,
2
,
3
,
4
,
5
]
y
=
[
0
,
2
,
5
,
9
,
15
]
#ax is the axes instance
group_labels
=
[
'a'
,
'b'
,
'c'
,
'd'
,
'e'
]
plt.plot(x,y)
plt.xticks(x, group_labels, rotation
=
0
)
plt.grid()
plt.show()
|
绘图如下:
。
上图中使用了"plt.xticks"方法设置x轴文本,标签文本使用group_labels中的内容,因此可以根据需要修改group_labels中的内容.
网上看到的另一种方法,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import
matplotlib.pyplot as pl
import
numpy as np
from
matplotlib.ticker
import
MultipleLocator, FuncFormatter
x
=
np.arange(
0
,
4
*
np.pi,
0.01
)
y
=
np.sin(x)
pl.figure(figsize
=
(
10
,
6
))
pl.plot(x, y,label
=
"$sin(x)$"
)
ax
=
pl.gca()
def
pi_formatter(x, pos):
"""
比较罗嗦地将数值转换为以pi/4为单位的刻度文本
"""
m
=
np.
round
(x
/
(np.pi
/
4
))
n
=
4
if
m
%
2
=
=
0
: m, n
=
m
/
2
, n
/
2
if
m
%
2
=
=
0
: m, n
=
m
/
2
, n
/
2
if
m
=
=
0
:
return
"0"
if
m
=
=
1
and
n
=
=
1
:
return
"$\pi$"
if
n
=
=
1
:
return
r
"$%d \pi$"
%
m
if
m
=
=
1
:
return
r
"$\frac{\pi}{%d}$"
%
n
return
r
"$\frac{%d \pi}{%d}$"
%
(m,n)
# 设置两个坐标轴的范围
pl.ylim(
-
1.5
,
1.5
)
pl.xlim(
0
, np.
max
(x))
# 设置图的底边距
pl.subplots_adjust(bottom
=
0.15
)
pl.grid()
#开启网格
# 主刻度为pi/4
ax.xaxis.set_major_locator( MultipleLocator(np.pi
/
4
) )
# 主刻度文本用pi_formatter函数计算
ax.xaxis.set_major_formatter( FuncFormatter( pi_formatter ) )
# 副刻度为pi/20
ax.xaxis.set_minor_locator( MultipleLocator(np.pi
/
20
) )
# 设置刻度文本的大小
for
tick
in
ax.xaxis.get_major_ticks():
tick.label1.set_fontsize(
16
)
pl.legend()
pl.show()
|
绘图如下:
以上就是本次小编整理的全部内容,感谢你对我的支持.
原文链接:http://blog.csdn.net/Fortware/article/details/51934814 。
最后此篇关于学习python中matplotlib绘图设置坐标轴刻度、文本的文章就讲到这里了,如果你想了解更多关于学习python中matplotlib绘图设置坐标轴刻度、文本的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
介绍篇 什么是MiniApis? MiniApis的特点和优势 MiniApis的应用场景 环境搭建 系统要求 安装MiniApis 配置开发环境 基础概念 MiniApis架构概述
我正在从“JavaScript 圣经”一书中学习 javascript,但我遇到了一些困难。我试图理解这段代码: function checkIt(evt) { evt = (evt) ? e
package com.fastone.www.javademo.stringintern; /** * * String.intern()是一个Native方法, * 它的作用是:如果字
您会推荐哪些资源来学习 AppleScript。我使用具有 Objective-C 背景的传统 C/C++。 我也在寻找有关如何更好地开发和从脚本编辑器获取更快文档的技巧。示例提示是“查找要编写脚本的
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
关闭。这个问题不符合 Stack Overflow guidelines 。它目前不接受答案。 想改善这个问题吗?更新问题,以便堆栈溢出为 on-topic。 6年前关闭。 Improve this
我是塞内加尔的阿里。我今年60岁(也许这是我真正的问题-笑脸!!!)。 我正在学习Flutter和Dart。今天,我想使用给定数据模型的列表(它的名称是Mortalite,请参见下面的代码)。 我尝试
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
学习 Cappuccino 的最佳来源是什么?我从事“传统”网络开发,但我对这个新框架非常感兴趣。请注意,我对 Objective-C 毫无了解。 最佳答案 如上所述,该网站是一个好地方,但还有一些其
我正在学习如何使用 hashMap,有人可以检查我编写的这段代码并告诉我它是否正确吗?这个想法是有一个在公司工作的员工列表,我想从 hashMap 添加和删除员工。 public class Staf
我正在尝试将 jQuery 与 CoffeScript 一起使用。我按照博客中的说明操作,指示使用 $ -> 或 jQuery -> 而不是 .ready() 。我玩了一下代码,但我似乎无法理解我出错
还在学习,还有很多问题,所以这里有一些。我正在进行 javascript -> PHP 转换,并希望确保这些做法是正确的。是$dailyparams->$calories = $calories;一条
我目前正在学习 SQL,以便从我们的 Magento 数据库制作一个简单的 RFM 报告,我目前可以通过导出两个查询并将它们粘贴到 Excel 模板中来完成此操作,我想摆脱 Excel 模板。 我认为
我知道我很可能会因为这个问题而受到抨击,但没有人问,我求助于你。这是否是一个正确的 javascript > php 转换 - 在我开始不良做法之前,我想知道这是否是解决此问题的正确方法。 JavaS
除了 Ruby-Doc 之外,哪些来源最适合获取一些示例和教程,尤其是关于 Ruby 中的 Tk/Tile?我发现自己更正常了 http://www.tutorialspoint.com/ruby/r
我只在第一次收到警告。这正常吗? >>> cv=LassoCV(cv=10).fit(x,y) C:\Python27\lib\site-packages\scikit_learn-0.14.1-py
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我是一名优秀的程序员,十分优秀!