蒋传领 发表于 2023-9-5 09:39:26

【matplotlib基础】--刻度

Matplotlib中刻度是用于在绘图中表示数据大小的工具。
刻度是坐标轴上的数字或标签,用于指示数据的大小或值,
通常以整数或小数表示,具体取决于坐标轴的类型和限制。
1. 主次刻度

默认的绘制时,坐标轴只有默认的主要刻度,如下所示:
from matplotlib.ticker import MultipleLocator

x = np.array(range(0, 100))
y = np.random.randint(100, 200, 100)

fig = plt.figure()
ax = fig.add_axes()
#X轴的主要和次要刻度
ax.xaxis.set_major_locator(MultipleLocator(20))
ax.xaxis.set_minor_locator(MultipleLocator(2))

#Y轴的主要和次要刻度
ax.yaxis.set_major_locator(MultipleLocator(50))
ax.yaxis.set_minor_locator(MultipleLocator(10))

ax.plot(x, y)
上面的示例中,
设置了X轴的主要刻度间隔20,次要刻度间隔2,也就是每2个主要刻度之间有10个次要刻度。
设置了Y轴的主要刻度间隔50,次要刻度间隔10,也就是每2个主要刻度之间有5个次要刻度。
次要刻度就是上面图中主要刻度之间稍短点的线。
2. 刻度样式

刻度的样式非常灵活,常见的有以下几种设置。
2.1. 隐藏刻度

隐藏刻度,只保留图形,这在做某些示意图的时候可能会用到。
x = np.array(range(0, 100))
y = np.random.randint(100, 200, 100)

fig = plt.figure()
ax = fig.add_axes()

#隐藏刻度
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())

ax.plot(x, y, color='g')
2.2. 密度

密度是指刻度的间隔,如果图比较小,可以设置间隔大一些,反之则设置小一些。
from matplotlib.ticker import MultipleLocator

x = np.array(range(0, 100))
y = np.random.randint(100, 200, 100)

rows, cols = 2, 2
grid = plt.GridSpec(rows, cols)

ax = plt.subplot(grid)
ax.plot(x, y)
ax.xaxis.set_major_locator(MultipleLocator(20))
ax.yaxis.set_major_locator(MultipleLocator(50))


ax = plt.subplot(grid)
ax.plot(x, y)
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_major_locator(MultipleLocator(20))
上例中,根据图形的大小,我们设置了刻度的不同密度。
2.3. 颜色,大小,旋转

为了突出某些刻度值,有时候会需要修改那些刻度值的颜色和大小。
x = np.array(range(0, 100))
y = np.random.randint(100, 200, 100)

fig = plt.figure()
ax = fig.add_axes()
ax.xaxis.set_major_locator(MultipleLocator(10))

obj = ax.get_xticklabels()
obj.set_size(20)
obj.set_color("red")

ax.plot(x, y, color='g')
上面示例中,X轴刻度10被放大并且改成了红色。
刻度的旋转一般用在刻度内容比较长的情况,比如下面的示例:
x = np.array(
    [
      "2022-01-01",
      "2022-02-01",
      "2022-03-01",
      "2022-04-01",
      "2022-05-01",
      "2022-06-01",
      "2022-07-01",
      "2022-08-01",
      "2022-09-01",
      "2022-10-01",
    ]
)
y = np.random.randint(100, 200, 10)

fig = plt.figure()
ax = fig.add_axes()

ax.plot(x, y, color="g")
由于X轴的刻度是日期,因为太长,所以会挤在一起,显示不清。
这时可以调整X轴刻度的角度,避免重合在一起。
x = np.array(
    [
      "2022-01-01",
      "2022-02-01",
      "2022-03-01",
      "2022-04-01",
      "2022-05-01",
      "2022-06-01",
      "2022-07-01",
      "2022-08-01",
      "2022-09-01",
      "2022-10-01",
    ]
)
y = np.random.randint(100, 200, 10)

fig = plt.figure()
ax = fig.add_axes()
plt.xticks(rotation=45) # 旋转45度

ax.plot(x, y, color="g")
2.4. latex格式

Matplotlib的刻度还支持latex格式,可以显示一些特殊的字符,比如圆周率π。
直接显示时:
x = np.array()
x = np.round(x, 2)
y = np.sin(x)

fig = plt.figure()
ax = fig.add_axes()
plt.xticks(labels=x, ticks=x)
ax.plot(x, y)
X轴的刻度显示实际的值。
调整为 latex 格式来显示:(调整 plt.xticks() 这个函数)
plt.xticks(labels=[
    "0", "$\pi/6$", "$\pi/4$", "$\pi/3$", "$\pi/2$"
], ticks=x)
X轴的刻度中显示圆周率π,更易于阅读和理解。
3. 总结回顾

与之前介绍的画布,子图和坐标轴相比,刻度是设置最多也是最复杂的一个容器。
刻度的主要作用是帮助数据可视化更加清晰和易于理解,基于此,本篇主要介绍了:

[*]主次刻度
[*]刻度样式,包括是否显示刻度,刻度的密度,颜色,大小,角度以及latex公式的支持。

来源:https://www.cnblogs.com/wang_yb/p/17678841.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【matplotlib基础】--刻度