gpt4 book ai didi

学习python中matplotlib绘图设置坐标轴刻度、文本

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 30 4
gpt4 key购买 nike

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

绘图如下:

学习python中matplotlib绘图设置坐标轴刻度、文本

如果仔细看代码,可以得知,设置坐标轴刻度和文本主要使用了"MultipleLocator"、"FormatStrFormatter"方法.

这两个方法来自matplotlib安装库里面ticker.py文件;"MultipleLocator(Locator)"表示将刻度标签设置为Locator的倍数,"FormatStrFormatter"表示设置标签文本的格式,代码中"%1.1f"表示保留小数点后一位,浮点数显示.

相应的方法还有:

学习python中matplotlib绘图设置坐标轴刻度、文本

除了以上方法,还有另外一种方法,那就是使用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()

绘图如下:

  。

学习python中matplotlib绘图设置坐标轴刻度、文本

上图中使用了"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()

绘图如下:

学习python中matplotlib绘图设置坐标轴刻度、文本

以上就是本次小编整理的全部内容,感谢你对我的支持.

原文链接:http://blog.csdn.net/Fortware/article/details/51934814 。

最后此篇关于学习python中matplotlib绘图设置坐标轴刻度、文本的文章就讲到这里了,如果你想了解更多关于学习python中matplotlib绘图设置坐标轴刻度、文本的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com