在 Python-Matplotlib 中格式化轴
Matplotlib是一个用于创建静态、动画和交互式数据可视化的Python库。
注意:更多信息请参考 Matplotlib 简介
什么是轴?
这就是你所认为的“情节”。它是包含数据空间的图像区域。 Axes 包含两个或三个轴(在 3D 的情况下)对象,它们负责数据限制。下面是一张图片,说明了包含图表的图形的不同部分。
轴的不同方面可以根据需要进行更改。
1. 标记 x、y 轴
句法:
for x-axis
Axes.set_xlabel(self, xlabel, fontdict=None, labelpad=None, \*\*kwargs)
for y-axis
Axes.set_ylabel(self, ylabel, fontdict=None, labelpad=None, \*\*kwargs)
这些函数用于命名 x 轴和 y 轴。
例子:
# importing matplotlib module
import matplotlib.pyplot as plt
import numpy as np
# x-axis & y-axis values
x = [3, 2, 7, 4, 9]
y = [10, 4, 7, 1, 2]
# create a figure and axes
fig, ax = plt.subplots()
# setting title to graph
ax.set_title('Example Graph')
# label x-axis and y-axis
ax.set_ylabel('y-AXIS')
ax.set_xlabel('x-AXIS')
# function to plot and show graph
ax.plot(x, y)
plt.show()
输出:
2. x、y 轴的限制
句法:
对于 x 轴:
Axes.set_xlim(self, left=None, right=None, emit=True, auto=False, \*, xmin=None, xmax=None)
Parameters:
- left and right – float, optional
The left xlim(starting point) and right xlim(ending point) in data coordinates. Passing None leaves the limit unchanged. - auto – bool or None, optional
To turn on autoscaling of the x-axis. True turns on, False turns off (default action), None leaves unchanged. - xmin, xmax : They are equivalent to left and right respectively, and it is an error to pass both xmin and left or xmax and right.
Returns:
right, left – (float, float)
对于 y 轴:
Axes.set_ylim(self, bottom=None, top=None, emit=True, auto=False, \*, ymin=None, ymax=None)
Parameters:
- bottom and top – float, optional
The bottom ylim(starting point) and top ylim(ending point) in data coordinates. Passing None leaves the limit unchanged. - auto – bool or None, optional
To turn on autoscaling of the y-axis. True turns on, False turns off (default action), None leaves unchanged. - ymin, ymax : They are equivalent to left and right respectively, and it is an error to pass both ymin and left or ymax and right.
Returns:
bottom, top – (float, float)
例子:
import matplotlib.pyplot as plt
import numpy as np
x = [3, 2, 7, 4, 9]
y = [10, 4, 7, 1, 2]
# create a figure and axes
fig, ax = plt.subplots()
ax.set_title('Example Graph')
ax.set_ylabel('y-AXIS')
ax.set_xlabel('x-AXIS')
# set x, y-axis limits
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
# function to plot and show graph
ax.plot(x, y)
plt.show()
输出:
3. 主要和次要刻度
刻度是 x 和 y 轴的值/大小。次要刻度是主要刻度的划分。有两个类Locator和Formatter 。定位器确定刻度的位置,格式化程序控制刻度的格式。这两个类必须从 matplotlib 导入。
MultipleLocator()将刻度放置在某个基数的倍数上。
- FormatStrFormatter使用格式字符串(例如 '%d' 或 '%1.2f' 或 '%1.1f cm' )来格式化刻度标签。
注意:次要刻度默认为关闭,可以通过设置次要定位器在没有标签的情况下打开它们,次要刻度标签可以通过次要格式化程序打开。
例子:
# importing matplotlib module and respective classes
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import (MultipleLocator,
FormatStrFormatter,
AutoMinorLocator)
x = [3, 2, 7, 4, 9]
y = [10, 4, 7, 1, 2]
fig, ax = plt.subplots()
ax.set_title('Example Graph')
ax.set_ylabel('y-AXIS')
ax.set_xlabel('x-AXIS')
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
# Make x-axis with major ticks that
# are multiples of 11 and Label major
# ticks with '% 1.2f' formatting
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.xaxis.set_major_formatter(FormatStrFormatter('% 1.2f'))
# make x-axis with minor ticks that
# are multiples of 1 and label minor
# ticks with '% 1.2f' formatting
ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.xaxis.set_minor_formatter(FormatStrFormatter('% 1.2f'))
ax.plot(x, y)
plt.show()
输出: