愛在花開的季節 发表于 2023-9-5 10:10:23

【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)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686671423114-0dbff622-6dbc-41b7-9a57-30409744730a.png#averageHue=%23fbfbfb&clientId=u4cddd7fe-1f6d-4&from=paste&height=426&id=ufdb09420&originHeight=426&originWidth=564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=49875&status=done&style=none&taskId=u8c2d4152-0a2b-499d-91da-00eed13a658&title=&width=564
上面的示例中,
设置了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')https://cdn.nlark.com/yuque/0/2023/png/2235414/1686676366720-a0574f9a-2a8b-49eb-a310-ff38795760db.png#averageHue=%23fcfcfc&clientId=u4cddd7fe-1f6d-4&from=paste&height=404&id=ud5464ebc&originHeight=404&originWidth=531&originalType=binary&ratio=1&rotation=0&showTitle=false&size=34111&status=done&style=none&taskId=u4225a664-8889-4023-911f-0e52e53d41f&title=&width=531
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))https://cdn.nlark.com/yuque/0/2023/png/2235414/1686677136002-17b5f140-451a-40d2-a2de-279bcd341811.png#averageHue=%23f9f9f9&clientId=u4cddd7fe-1f6d-4&from=paste&height=412&id=u3b2a35ea&originHeight=412&originWidth=548&originalType=binary&ratio=1&rotation=0&showTitle=false&size=53791&status=done&style=none&taskId=u08ed0ac2-2c91-40a1-a359-53a82422def&title=&width=548
上例中,根据图形的大小,我们设置了刻度的不同密度。
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')https://cdn.nlark.com/yuque/0/2023/png/2235414/1686703444785-e8cf634c-1a5a-45fb-9a09-b48764af8380.png#averageHue=%23fbfbfb&clientId=u068f5055-54f9-4&from=paste&height=438&id=ufef4240e&originHeight=438&originWidth=564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43167&status=done&style=none&taskId=u34fcfdbd-bfc6-4538-9a58-2d816716864&title=&width=564
上面示例中,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")https://cdn.nlark.com/yuque/0/2023/png/2235414/1686705617746-30e56624-3c12-40c7-9fd3-4b26a4e65dba.png#averageHue=%23fafafa&clientId=u068f5055-54f9-4&from=paste&height=426&id=u625c5fd0&originHeight=426&originWidth=579&originalType=binary&ratio=1&rotation=0&showTitle=false&size=21768&status=done&style=none&taskId=u1cf8044f-b6cf-476a-886f-423f190e0bd&title=&width=579
由于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")https://cdn.nlark.com/yuque/0/2023/png/2235414/1686706775812-4cfe2c59-8728-4c3b-8b5f-9db23f2b7965.png#averageHue=%23fafafa&clientId=u068f5055-54f9-4&from=paste&height=480&id=u5b0ab843&originHeight=480&originWidth=572&originalType=binary&ratio=1&rotation=0&showTitle=false&size=27067&status=done&style=none&taskId=u4eb3af3a-f98d-4505-a1a0-f43238c7c8e&title=&width=572
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)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686707491579-4bd85085-ef7b-4015-9b60-c5aa40d40550.png#averageHue=%23fcfcfc&clientId=u068f5055-54f9-4&from=paste&height=426&id=ua09bb61e&originHeight=426&originWidth=564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=12648&status=done&style=none&taskId=ua01b106e-3a34-4ddd-90cc-b679bd3680c&title=&width=564
X轴的刻度显示实际的值。
调整为 latex 格式来显示:(调整 plt.xticks() 这个函数)
plt.xticks(labels=[
    "0", "$\pi/6$", "$\pi/4$", "$\pi/3$", "$\pi/2$"
], ticks=x)https://cdn.nlark.com/yuque/0/2023/png/2235414/1686707578970-605ef5c9-006d-4395-93d3-6357ece5efd3.png#averageHue=%23fcfcfc&clientId=u068f5055-54f9-4&from=paste&height=428&id=u3908eb3a&originHeight=428&originWidth=564&originalType=binary&ratio=1&rotation=0&showTitle=false&size=12610&status=done&style=none&taskId=u8240e2a7-de07-48c1-b042-c64368b7ce1&title=&width=564
X轴的刻度中显示圆周率π,更易于阅读和理解。
3. 总结回顾

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

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【matplotlib基础】--刻度