xticks函数–Matplotlib

yticks使用方法相同

函数功能: Get or set the current tick locations and labels of the x-axis. Pass no arguments to return the current values without modifying them.

函数语法:
yticks(ticks=None, labels=None, **kwargs)

                   获取或设置当前x轴刻度位置和标签。若不传递任何参数,则返回当前刻度值

函数参数:

ticks: array-like, optional。The list of xtick locations. Passing an empty list removes all xticks.
                   可选参数,数组,x轴刻度位置的列表。传入空列表移除所有x轴刻度

可以看到图中默认只给出了偶数刻度及位置
在这里插入图片描述
如果要显示1–10所有的10个整数刻度,可以使用参数 x t i c k s xticks xticks设置,传入每个刻度显示的位置,用数组传入参数

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,1000)
y = np.sin(x)
a = list(range(0,11,1))

plt.xticks(a)

plt.plot(x,y)
plt.show()

在这里插入图片描述
当传入空列表,则移除所有刻度,相当于禁用刻度

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,1000)
y = np.sin(x)
a = []

plt.xticks(a)

plt.plot(x,y)
plt.show()

在这里插入图片描述

labels: array-like, optional.The labels to place at the given ticks locations. This argument can only be passed if ticks is passed as well.
                   可选参数,数组,放在指定刻度位置的标签文本。只有当ticks参数有输入值,该参数才能传入参数

可以看到虽然默认会输出偶数 0 , 2 , 4 , 6 , 8 , 10 0,2,4,6,8,10 0,2,4,6,8,10的横轴标签,但是依然不能直接定义参数 l a b e l s labels labels,必须与参数 t i c k s ticks ticks同时使用
在这里插入图片描述
参数 t i c k s ticks ticks与参数 l a b e l s labels labels同时传入
在这里插入图片描述

**kwargs: Text properties can be used to control the appearance of the labels.
其他关键字参数:文本属性用来控制标签文本的展示:例如字体大小、字体样式等。

若无参数传入则返回当前x轴刻度的位置与刻度标签

当参数无labels是标签而是默认的数字时,返回的是x轴刻度的位置,而相应的标签位置为空
在这里插入图片描述
当参数 t i c k s ticks ticks与参数 l a b e l s labels labels同时传入指定后,xtick()会返回x轴刻度的位置与相应的标签文本。
在这里插入图片描述
官方文档xticks函数

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐