网站搜索

如何调整Matplotlib中轴标签的位置?


当我们使用子图或图在 Matplotlib 中执行数据可视化时,正确标记轴并根据需要调整轴标签的位置非常重要,以便它们不会与图中的其他元素重叠。这可以帮助用户轻松理解所呈现的数据。

为了创建标签和调整标签,我们将使用 Matplotlib 库,该库用于创建高质量的数据可视化。本文将讨论在 Matplotlib 中调整轴标签位置的各种方法。我们将使用同样的子图。

Matplotlib

Matplotlib 是一个主要用于为 Python 编程语言和 NumPy 绘制图形和绘图的库,NumPy 是其数值数学的扩展。 Tkinter、wxPython、Qt 和 GTK GUI 工具包可能包括利用其面向对象 API 的图表。

matplotlib.pyplot 是命令样式方法的集合,使 matplotlib 的功能与 MATLAB 类似。每个 pyplot 函数都会以某种方式更改图形,无论是添加绘图区域、绘制线条、添加标签等。当前图形和绘图区域保存在 matplotlib.pyplot 中的函数调用之间,并且始终应用绘图函数到活动的轴集。

次要情节

Matplotlib 中的子图允许在单个图形中显示多个图或图表。我们可以借助子图同时比较和分析多组数据。这使得发现或识别趋势、模式和关系变得更加容易。

子图是由较小图组成的网格,这些较小图是较大图的一部分。每个子图在网格中都有自己的位置,该位置基于网格中的行数和列数以及子图在该网格中的位置。

Matplotlib 的“子图”方法让我们可以创建子图。该函数返回一个图形对象和一个子图对象数组。我们可以使用这些子图对象在每个子图中绘制数据。

句法

fig,ax=plt.subplots(nrows,ncolumns,index)

解释

  • nrows - 此参数指定网格中子图的行数。

  • ncolumns - 此参数指定网格中子图的列数。

  • index - 此参数指定当前子图的索引。索引从 1 开始并逐行增加。

调整轴标签的位置

Matplotlib 中有多种方法或函数,我们可以使用它们来调整 Matplotlib 图中轴标签的位置,它们是 -

  • .set_label_coords()函数

  • set_label_position()函数

  • set_pad()函数

.set_label_coords()

该方法用于设置子图标签的坐标。

刻度标签边界框确定 y 标签的 x 坐标和 x 标签的 y 坐标的默认值。然而,当有许多轴并且标签必须在它们之间对齐时,就会出现问题。

标签的坐标也可以指定给变换。如果指定 None ,则使用轴坐标系,其中 (0, 0) 是左下角,(0.5, 0.5) 是中间等。

实施例1

import matplotlib.pyplot as p
import numpy as n

# generate some data
x=n.array([11, 22,33, 44, 55,66,77,88,99,100])

# create a subplot and plot the data
f, a = p.subplots(2,2)
a[0,0].plot(x, n.sin(x))
a[0,1].plot(x,n.cos(x))
a[1, 0].plot(x, x)
a[1, 1].plot(x, n.exp(x))
# set the x-axis label and adjust the position
a[0,0].set_xlabel('Sin graph')
a[0,0].xaxis.set_label_coords(0.35, 0)

a[0,1].set_xlabel('Cos graph')
a[0,1].xaxis.set_label_coords(0.65,0)
a[1, 0].set_xlabel('Linear graph')
a[1,0].xaxis.set_label_coords(0.35,-0.24)
a[1, 1].set_xlabel('exponential graph')
a[1,1].xaxis.set_label_coords(0.65,-0.25)

# display the plot
p.show()

输出

set_label_position() 函数

set_position() 函数用于设置子图中轴的标签位置。该方法接受以下参数 -

位置 - ‘左’,‘右’,‘上’,‘下’。

实施例2

import matplotlib.pyplot as p
import numpy as n

# generate some data
x=n.array([11, 22,33, 44, 55,66,77,88,99,100])

# create a subplot and plot the data
f, a = p.subplots(2,2)
a[0,0].plot(x, n.sin(x))
a[0,1].plot(x,n.cos(x))
a[1, 0].plot(x, x)
a[1, 1].plot(x, n.exp(x))
# set the x-axis label and y label and adjust the position
a[0,0].set_xlabel('Sin graph')
a[0,0].xaxis.set_label_position('bottom')
a[0,0].yaxis.set_label_position('left')
a[0,0].xaxis.set_label_coords(0.35, 0)
a[0,0].yaxis.set_label_coords(0.35, 0)
a[0,1].set_xlabel('Cos graph')
a[0,1].xaxis.set_label_position('bottom')
a[0,0].yaxis.set_label_position('left')
a[0,1].xaxis.set_label_coords(0.65,0)
a[0,1].yaxis.set_label_coords(0.65,0)
a[1, 0].set_xlabel('Linear graph')
a[1,0].xaxis.set_label_position('bottom')
a[0,0].yaxis.set_label_position('left')
a[1,0].xaxis.set_label_coords(0.35,-0.24)
a[1,0].yaxis.set_label_coords(0.35,-0.24)
a[1, 1].set_xlabel('exponential graph')
a[1,1].xaxis.set_label_position('bottom')
a[0,0].yaxis.set_label_position('left')
a[1,1].xaxis.set_label_coords(0.65,-0.25)
a[1,1].yaxis.set_label_coords(0.65,-0.25)


# display the plot
p.show()

输出

set_pad(),set_label函数的labelpad参数

使用 set pad() 函数,我们可以更改轴标签和轴刻度标签之间的距离。

例如,我们可以使用以下代码来更改子图中 x 轴标签周围的空间 -

实施例3

import matplotlib.pyplot as plt
import numpy as np

# generate some data
x=np.array([11, 22,33, 44, 55,66,77,88,99,100])

# create a subplot and plot the data
fig, ax = plt.subplots(2,2)
ax[0,0].plot(x, np.sin(x))
ax[0,1].plot(x,np.cos(x))
ax[1, 0].plot(x, x)
ax[1, 1].plot(x, np.exp(x))
# set the x-axis label and adjust the position

ax[1, 0].set_xlabel('Linear graph',labelpad=10)

ax[1, 1].set_xlabel('exponential graph',labelpad=10)


# display the plot
plt.show()

输出

结论

总之,调整轴标签的位置是使用 Matplotlib 制作清晰准确的绘图的重要部分。 set_label_coords()、set_position() 和 set_pad() 是我们可以用来更改绘图或子图中轴标签位置的一些方法。

相关文章: