6

【matplotlib基础】--刻度 - wang_yb

 1 year ago
source link: https://www.cnblogs.com/wang_yb/p/17678841.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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([0.1, 0.1, 0.8, 0.8])
#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)
image.png

上面的示例中,
设置了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([0.1, 0.1, 0.8, 0.8])

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

ax.plot(x, y, color='g')
image.png

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[0, 0])
ax.plot(x, y)
ax.xaxis.set_major_locator(MultipleLocator(20))
ax.yaxis.set_major_locator(MultipleLocator(50))


ax = plt.subplot(grid[1, :])
ax.plot(x, y)
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_major_locator(MultipleLocator(20))
image.png

上例中,根据图形的大小,我们设置了刻度的不同密度

2.3. 颜色,大小,旋转

为了突出某些刻度值,有时候会需要修改那些刻度值的颜色和大小。

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

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.xaxis.set_major_locator(MultipleLocator(10))

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

ax.plot(x, y, color='g')
image.png

上面示例中,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([0.1, 0.1, 0.8, 0.8])

ax.plot(x, y, color="g")
image.png

由于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([0.1, 0.1, 0.8, 0.8])
plt.xticks(rotation=45) # 旋转45度

ax.plot(x, y, color="g")
image.png

2.4. latex格式

Matplotlib的刻度还支持latex格式,可以显示一些特殊的字符,比如圆周率π
直接显示时:

x = np.array([0, np.pi / 6, np.pi / 4, np.pi/3, np.pi / 2])
x = np.round(x, 2)
y = np.sin(x)

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
plt.xticks(labels=x, ticks=x)
ax.plot(x, y)
image.png

X轴的刻度显示实际的值。
调整为 latex 格式来显示:(调整 plt.xticks() 这个函数)

plt.xticks(labels=[
    "0", "$\pi/6$", "$\pi/4$", "$\pi/3$", "$\pi/2$"
], ticks=x)
image.png

X轴的刻度中显示圆周率π,更易于阅读和理解。

3. 总结回顾

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

  1. 刻度样式,包括是否显示刻度,刻度的密度,颜色,大小,角度以及latex公式的支持。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK